forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLesson24.java
More file actions
116 lines (60 loc) · 2.47 KB
/
JavaLesson24.java
File metadata and controls
116 lines (60 loc) · 2.47 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import javax.swing.*;
import java.awt.event.*;
public class JavaLesson24 extends JFrame{
JComboBox favoriteShows;
JButton button1;
String infoOnComponent = "";
public static void main(String[] args){
new JavaLesson24();
}
public JavaLesson24(){
this.setSize(400,400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("My Fourth Frame");
JPanel thePanel = new JPanel();
// Create an array that will be added to the combo box
String[] shows = {"Breaking Bad", "Life on Mars", "Doctor Who"};
// Create a combo box and add the array of shows
favoriteShows = new JComboBox(shows);
// Add an item to the combo box
favoriteShows.addItem("Pushing Daisies");
thePanel.add(favoriteShows);
// Create a button
button1 = new JButton("Get Answer");
ListenForButton lForButton = new ListenForButton();
button1.addActionListener(lForButton);
thePanel.add(button1);
this.add(thePanel);
this.setVisible(true);
// Add an item to a combo box at index 1
favoriteShows.insertItemAt("Dexter", 1);
// Only show 3 items at a time
favoriteShows.setMaximumRowCount(3);
// Remove the item named Dexter
//favoriteShows.removeItem("Dexter");
// Remove the item at index 1
//favoriteShows.removeItemAt(1);
// Remove all items
// favoriteShows.removeAllItems();
}
private class ListenForButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == button1){
favoriteShows.setEditable(true);
// Get item at index 0
infoOnComponent = "Item at 0: " + favoriteShows.getItemAt(0) + "\n";
// Get the number of items in the combo box
infoOnComponent += "Num of Shows: " + favoriteShows.getItemCount() + "\n";
// Get the index for the selected item
infoOnComponent += "Selected ID: " + favoriteShows.getSelectedIndex() + "\n";
// Get the value for the selected item
infoOnComponent += "Selected Show: " + favoriteShows.getSelectedItem() + "\n";
// Find out if the values in the combo box are editable
infoOnComponent += "Combo Box Editable: " + favoriteShows.isEditable() + "\n";
JOptionPane.showMessageDialog(JavaLesson24.this, infoOnComponent, "Information", JOptionPane.INFORMATION_MESSAGE);
infoOnComponent = "";
}
}
}
}