/* 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 java.awt.* ; 

3. import java.awt.event.*; 

4.  

5. public class StateSw 

6.   extends JApplet 

7.   implements ActionListener 

8.

9.   JButton btn ; 

10.   JButton c1 ; 

11.   JTextField c2 ; 

12.   JComboBox c3 ; 

13.   JComponent cp ; 

14.  

15.   boolean shown = true

16.   boolean enabled = true

17.  

18.   public void 

19.   actionPerformed ( ActionEvent ev ) 

20.   { 

21.     if ( enabled ) 

22.     { 

23.       c1.setEnabled ( false ) ; 

24.       c2.setEnabled ( false ) ; 

25.       c3.setEnabled ( false ) ; 

26.       enabled = false

27.       return

28.     } 

29.     if ( shown ) 

30.     { 

31.       c1.setVisible ( false ) ; 

32.       c2.setVisible ( false ) ; 

33.       c3.setVisible ( false ) ; 

34.       shown = false

35.       cp.revalidate(); 

36.       return

37.     } 

38.     c1.setEnabled ( true ) ; 

39.     c2.setEnabled ( true ) ; 

40.     c3.setEnabled ( true ) ; 

41.     enabled = true

42.     c1.setVisible ( true ) ; 

43.     c2.setVisible ( true ) ; 

44.     c3.setVisible ( true ) ; 

45.     cp.revalidate() ; 

46.     shown = true

47.   } 

48.   public void 

49.   init() 

50.   { 

51.     cp = (JComponent)getContentPane(); 

52.     c1 = new JButton ( "Now you see me" ) ; 

53.     c2 = new JTextField ( "See me ..." ) ; 

54.     c3 = new JComboBox (); 

55.     c3.addItem ( "La primera vez" ) ; 

56.     c3.addItem ( "Hasta luego" ) ; 

57.  

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

59.     cp.add ( c1 ) ; cp.add ( c2 ) ; cp.add ( c3 ) ; 

60.  

61.     btn = new JButton ( "Push me gently" ) ; 

62.     btn.addActionListener ( this ) ; 

63.     cp.add ( btn ) ; 

64.     cp.setBackground ( Color.pink ) ; 

65.   } 

66.

67.