/* 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.  import javax.swing.*; 

2.  import javax.swing.filechooser.FileFilter

3.  import java.awt.*; 

4.  import java.awt.event.*; 

5.  import java.io.*; 

6.   

7.  public class SwingDialogs 

8.    extends JApplet 

9.    implements ActionListener 

10.

11.   String[] lstcont = 

12.   { "File", "Color", "Message", "Confirm", "Input" }; 

13.   JComboBox cb = new JComboBox ( lstcont ); 

14.   Container cp ; 

15.   JLabel lbl = new JLabel ( "xxxxxxxxxxxxxxxxxxxxxxxxx" ) ; 

16.  

17.   public void 

18.   actionPerformed ( ActionEvent ev ) 

19.   { 

20.     String str = (String) cb.getSelectedItem ( ) ; 

21.  

22.     if ( str.equalsIgnoreCase ( "file" ) ) 

23.     { 

24.       JFileChooser fch  = new JFileChooser ( ); 

25.  

26.       OurFilter filt = new OurFilter ( ) ; 

27.       fch.setFileFilter ( filt ) ; 

28.       fch.setFileSelectionMode 

29.         ( JFileChooser.FILES_AND_DIRECTORIES ); 

30.  

31.  

32.       int result = fch.showOpenDialog ( this ); 

33.  

34.       if ( result ==  JFileChooser.APPROVE_OPTION

35.         lbl.setText ( fch.getSelectedFile ( ) + "" ) ; 

36.  

37.       return

38.     } 

39.  

40.     if ( str.equalsIgnoreCase ( "color" ) ) 

41.     { 

42.       Color col = 

43.        JColorChooser.showDialog 

44.         ( this, "Choose", new Color ( 0,255,0 )  ) ; 

45.       cp.setBackground ( col ) ; 

46.       return

47.     } 

48.  

49.     if ( str.equalsIgnoreCase ( "message" ) ) 

50.     { 

51.       JOptionPane.showMessageDialog ( this, "Hi, man"); 

52.  

53.       JOptionPane.showMessageDialog 

54.        ( this, "Whitney, we have a problem"

55.        "Ze Title", JOptionPane.WARNING_MESSAGE ); 

56.  

57.       return

58.     } 

59.     if ( str.equalsIgnoreCase ( "confirm" ) ) 

60.     { 

61.       int res = 

62.       JOptionPane.showConfirmDialog 

63.        ( this, "Do you agree ?", "???"

64.          JOptionPane.YES_NO_OPTION ); 

65.  

66.       if ( res == JOptionPane.YES_OPTION

67.         lbl.setText ( "=== Y E S ===" ) ; 

68.       else 

69.         lbl.setText ( "=== Nope ===" ) ; 

70.  

71.       return

72.     } 

73.     if ( str.equalsIgnoreCase ( "input" ) ) 

74.     { 

75.       String res = 

76.        JOptionPane.showInputDialog 

77.        ( this, "Please enter something, anything !?!" ); 

78.  

79.       lbl.setText ( res ) ; 

80.  

81.       return

82.     } 

83.   } 

84.  

85.   public void init() 

86.   { 

87.     cp = getContentPane( ) ; 

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

89.  

90.     cp.add ( cb ); 

91.     cp.add ( lbl ) ; 

92.  

93.     cb.addActionListener ( this ); 

94.  

95.     cp.setBackground( Color.yellow ); 

96.  

97.     validate (); 

98.   } 

99.  //================================== 

100.  class OurFilter 

101.      extends FileFilter 

102.  { 

103.    public boolean 

104.    accept ( File f ) 

105.    { 

106.      if ( f == null ) return false

107.   

108.      if ( f.isDirectory()  )  return true

109.   

110.      String nm = f.getName( ); 

111.      if ( nm.endsWith( ".txt" ) ) return true

112.      if ( nm.endsWith( ".TXT" ) ) return true

113.      return false

114.    } 

115.   

116.    public String 

117.    getDescription ( ) 

118.    { 

119.      return "*.txt files"

120.    } 

121.

122.

123.