forked from stiers/COPRO2_SampleJavaPrograms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ComboBoxDemo.java
60 lines (41 loc) · 1.35 KB
/
ComboBoxDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @author Ephramar Telog, CK
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ComboBoxDemo extends JFrame implements ItemListener {
String course[] = { "Math", "Science", "English", "Filipino" };
JLabel label;
JComboBox subject;
JTextField subjectSelection;
public ComboBoxDemo() {
super( "This is an exam" );
label = new JLabel( "Choose a subject: " );
subject = new JComboBox( course );
subjectSelection = new JTextField( 10 );
Container pane = getContentPane();
pane.setBackground( Color.WHITE );
pane.setLayout( new FlowLayout() );
pane.add( label );
pane.add( subject );
pane.add( subjectSelection );
subject.addItemListener( this );
subject.setForeground( Color.YELLOW );
subject.setBackground( Color.BLUE );
subjectSelection.setText( course[0] );
}
public void itemStateChanged( ItemEvent e ) {
if( e.getSource() == subject )
subjectSelection.setText( course[subject.getSelectedIndex()] );
}
public static void main(String [] args) {
ComboBoxDemo exam = new ComboBoxDemo ();
System.out.println( "Creating instance of Combo Box" );
exam.setVisible( true );
System.out.println( "Combo Box set to be visible" );
exam.setSize( 350, 150 );
System.out.println( "Combo Box dimention set to 350 x 150 pixels" );
exam.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}