forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLesson25.java
More file actions
176 lines (100 loc) · 4.3 KB
/
JavaLesson25.java
File metadata and controls
176 lines (100 loc) · 4.3 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import javax.swing.*;
import java.awt.event.*;
public class JavaLesson25 extends JFrame{
JButton button1;
String infoOnComponent = "";
JList favoriteMovies, favoriteColors;
DefaultListModel defListModel = new DefaultListModel();
JScrollPane scroller;
public static void main(String[] args){
new JavaLesson25();
}
public JavaLesson25(){
this.setSize(400,400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("My Fifth Frame");
JPanel thePanel = new JPanel();
// Create a button
button1 = new JButton("Get Answer");
ListenForButton lForButton = new ListenForButton();
button1.addActionListener(lForButton);
thePanel.add(button1);
String[] movies = {"Matrix", "Minority Report", "Big"};
// Creating a List Box
favoriteMovies = new JList(movies);
// Define the height of each cell
favoriteMovies.setFixedCellHeight(30);
// Define the width of each cell
favoriteMovies.setFixedCellWidth(150);
// Define how many selections can be made
// MULTIPLE_INTERVAL_SELECTION: Select what ever you want
// SINGLE_SELECTION: Select only one
// SINGLE_INTERVAL_SELECTION: Select as many as you want if in order
favoriteMovies.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
// All the methods for lists
/*
* getSelectedIndex(): returns the index for the first selected item
* getSelectedIndexes(): returns every selection in a list
* getSelectedValue(): returns the value of the first selected
* getSelectedValues(): returns an array of all values
* isSelectedIndex(): returns true if index is selected
*/
// You can't change items in a list unless you store the items
// in a DefaultListModel
String[] colors = {"Black", "Blue", "White", "Green", "Orange", "Gray", "Pink"};
// How to load a String array into a DefaultListModel
for(String color: colors){
defListModel.addElement(color);
}
// Add item named Purple to index number 2
defListModel.add(2, "Purple");
// Create a List box filled with items in the DefaultListModel
favoriteColors = new JList(defListModel);
// Only display 4 items at a time
favoriteColors.setVisibleRowCount(4);
// Create a scroll bar panel to hold the list box
scroller = new JScrollPane(favoriteColors,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// Define the height of each cell
favoriteColors.setFixedCellHeight(30);
// Define the width of each cell
favoriteColors.setFixedCellWidth(150);
thePanel.add(favoriteMovies);
// You add the scroll bar container, not the list
thePanel.add(scroller);
this.add(thePanel);
this.setVisible(true);
}
private class ListenForButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == button1){
// contains returns true if the item is in the list
if(defListModel.contains("Black")) infoOnComponent += "Black is here\n";
// Check if the list isn't empty
if(!defListModel.isEmpty()) infoOnComponent += "Isn't Empty\n";
// return the number of items in the DefaultListModel
infoOnComponent += "Elements in the list " + defListModel.size() + "\n";
// return the first element in the list
infoOnComponent += "Last Element " + defListModel.firstElement() + "\n";
// return the last element in the list
infoOnComponent += "Last Element " + defListModel.lastElement() + "\n";
// return the last element in the list
infoOnComponent += "Element in index 1 " + defListModel.get(1) + "\n";
// Remove the item in index 0
defListModel.remove(0);
// Remove the item named Big
defListModel.removeElement("Blue");
// Create an array filled with the list items
Object[] arrayOfList = defListModel.toArray();
// Iterate through the array
for(Object color: arrayOfList){
infoOnComponent += color + "\n";
}
JOptionPane.showMessageDialog(JavaLesson25.this, infoOnComponent, "Information", JOptionPane.INFORMATION_MESSAGE);
infoOnComponent = "";
}
}
}
}