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

3. import java.awt.* ; 

4. import java.awt.event.*; 

5.  

6. public class ListnCombo     extends JApplet 

7.     implements ListSelectionListener

8.                ItemListener 

9.

10.   JList lst ; 

11.   JComboBox cbx ; 

12.   DefaultListModel mod ; 

13.   JScrollPane sp ; 

14.   Container cp ; 

15.  

16.   public ListnCombo ( ) 

17.   { 

18.     mod = new DefaultListModel( ); 

19.     lst = new JList ( mod ); 

20.     lst.setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); 

21.     sp = new JScrollPane ( lst ); 

22.  

23.     mod.addElement ( "Red" ) ; 

24.     mod.addElement ( "Green" ) ; 

25.     mod.addElement ( "Blue" ) ; 

26.  

27.     cbx = new JComboBox ( ) ; 

28.     cbx.addItem ( "Yellow" ) ; 

29.     cbx.addItem ( "Pink" ) ; 

30.     cbx.addItem ( "Orange" ) ; 

31.   } 

32.  

33.   public void valueChanged( ListSelectionEvent ev ) 

34.   { 

35.       int nr ; 

36.       nr =  lst.getSelectedIndex() ; 

37.       switch ( nr ) 

38.       { 

39.       case 0 :  cp.setBackground ( Color.red ) ; 

40.           break

41.       case 1 :  cp.setBackground ( Color.green ) ; 

42.           break

43.       case 2 :  cp.setBackground ( Color.blue ) ; 

44.           break

45.       default :  cp.setBackground ( Color.white ) ; 

46.       } 

47.   } 

48.  

49.   public void itemStateChanged( ItemEvent ev ) 

50.   { 

51.     if ( ev.getSource() == cbx ) 

52.     { 

53.       switch ( cbx.getSelectedIndex() ) 

54.       { 

55.       case 0 :  cp.setBackground ( Color.yellow ) ; 

56.           break

57.       case 1 :  cp.setBackground ( Color.pink ) ; 

58.           break

59.       case 2 :  cp.setBackground ( Color.orange ) ; 

60.           break

61.       default :  cp.setBackground ( Color.lightGray ) ; 

62.       } 

63.     } 

64.   } 

65.   public void init() 

66.   { 

67.     cp = this.getContentPane(); 

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

69.     sp.setPreferredSize (new Dimension( 100, 75 ) ); 

70.     cp.add ( sp ) ; 

71.     cp.add ( cbx ) ; 

72.     lst.addListSelectionListener( this ); 

73.     cbx.addItemListener ( this ) ; 

74.   } 

75.

76. ============ 

77. Some DefaultListModel methods : 

78.  insertElementAt 

79.  clear 

80.  removeElementAt 

81.  removeRange 

82.   etc.. 

83. ============= 

84. JComboBox

85.  removeItemAt 

86.  removeAllItems 

87.   etc...