/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. ===  ODAppletSw.java  ========== 

2. import javax.swing.* ; 

3. import java.awt.* ; 

4. import java.awt.event.*; 

5.  

6. public class ODAppletSw 

7.     extends JApplet 

8.     implements ActionListener 

9.

10.   OwnDialogSw od = null

11.   JButton btshow, btgetc ; 

12.   JTextArea ta ; 

13.   JScrollPane sp ; 

14.   JFrame fr ; 

15.   Container cp; 

16.  

17.   public void 

18.   actionPerformed( ActionEvent ev ) 

19.   { 

20.     if ( ev.getSource () == btshow ) 

21.     { 

22.       if ( od == null

23.       { 

24.         fr = new JFrame(); 

25.         od = new OwnDialogSw ( fr ) ; 

26.       } 

27.       od.setVisible ( true ) ; 

28.       return

29.     } 

30.     if ( ev.getSource () != btgetc ) 

31.       return

32.     if ( od == null

33.       return

34.  

35.     ta.append ( od.getlst () + "\n" ) ; 

36.     ta.append ( od.getcb1 () + "\n" ) ; 

37.     ta.append ( od.getcb2 () + "\n" ) ; 

38.     ta.append ( od.gettf () + "\n" ) ; 

39.     ta.append ( "=================\n" ) ; 

40.   } 

41.   public 

42.   ODAppletSw ( ) 

43.   { 

44.     btshow = new JButton ( "Show the dialog, please" ) ; 

45.     btgetc = new JButton ( "Get the dialog contents" ) ; 

46.     ta = new JTextArea ( 10,35 ) ; 

47.     sp = new JScrollPane( ta ); 

48.   } 

49.   public void 

50.   init() 

51.   { 

52.     cp = getContentPane(); 

53.     cp.setLayout( new FlowLayout() ); 

54.     cp.add ( btshow ) ; 

55.     cp.add ( btgetc ) ; 

56.     cp.add ( sp ) ; 

57.     btshow.addActionListener ( this ) ; 

58.     btgetc.addActionListener ( this ) ; 

59.   } 

60.

61. ====  OwnDialogSw.java  ======== 

62. import javax.swing.* ; 

63. import java.awt.*; 

64. import java.awt.event.*; 

65.  

66. public class OwnDialogSw 

67.     extends JDialog 

68.

69.   JList lst ; 

70.   JCheckBox cb1, cb2 ; 

71.   JTextField tf ; 

72.   Container cp ; 

73.  

74.   public OwnDialogSw ( JFrame fr ) 

75.   { 

76.     super ( fr, "The Dialog Title", false ) ; 

77.  

78.     cp = getContentPane(); 

79.  

80.      DefaultListModel mod = new DefaultListModel(); 

81.  

82.     lst = new JList (mod) ; 

83.     mod.addElement( "Espresso" ) ; 

84.     mod.addElement ( "Coffee" ) ; 

85.     mod.addElement ( "Tea" ) ; 

86.     lst.setSelectionMode ( ListSelectionModel.SINGLE_SELECTION ); 

87.  

88.     cb1 = new JCheckBox ( "With milk" ); 

89.     cb2 = new JCheckBox ( "With sugar" ); 

90.  

91.     tf = new JTextField ( 25 ) ; 

92.  

93.     setSize ( 350, 200 ) ; 

94.     cp.setLayout (  new FlowLayout ()  ) ; 

95.     cp.add ( lst ) ; 

96.     cp.add ( cb1) ; 

97.     cp.add ( cb2 ) ; 

98.     cp.add ( tf ) ; 

99.     addWindowListener ( new WA () ) ; 

100.   } 

101.   int 

102.   getlst () 

103.   { 

104.     return lst.getSelectedIndex (); 

105.   } 

106.   boolean 

107.   getcb1 () 

108.   { 

109.     return cb1.isSelected(); 

110.   } 

111.   boolean 

112.   getcb2 () 

113.   { 

114.     return cb2.isSelected(); 

115.   } 

116.   String 

117.   gettf () 

118.   { 

119.     return tf.getText (); 

120.   } 

121.  

122.   class WA extends WindowAdapter 

123.   { 

124.     public void windowClosing ( WindowEvent ev ) 

125.     { 

126.       setVisible ( false ) ; 

127.     } 

128.   } 

129.

130.