-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_combobox.java
More file actions
40 lines (30 loc) · 1.13 KB
/
08_combobox.java
File metadata and controls
40 lines (30 loc) · 1.13 KB
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
import javax.swing.*;
import java.awt.event.*;
public class GUIApp {
public static void main(String[] args) {
JFrame frame = new JFrame("Cupertinii Swing GUI");
frame.setSize(400, 500);
frame.setLayout(null);
JLabel label = new JLabel("Select a weekday");
label.setBounds(100, 20, 200, 20);
// Create array with all the options
String days[] = { "Monday", "Teusday", "Wednesday", "Thursday", "Friday" };
// Pass the array to ComboBox constructor
JComboBox cmbDays = new JComboBox(days);
cmbDays.setBounds(100, 50, 200, 20);
// This is how I can set the default entry
cmbDays.setSelectedItem( "Friday" );
System.out.println("Currently selected item is " + cmbDays.getSelectedIndex());
// Adding action listener
cmbDays.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
String message = "Selected : " + cmbDays.getItemAt( cmbDays.getSelectedIndex() );
System.out.println(message);
}
});
// Add all the components to application frame
frame.add(label);
frame.add(cmbDays);
frame.setVisible(true);
}
}