-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathList.java
More file actions
86 lines (65 loc) · 2.07 KB
/
List.java
File metadata and controls
86 lines (65 loc) · 2.07 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
package gui;
/**
* RUN:
* javac -cp .; gui/List.java && java -cp .; gui.List
* OUTPUT:
*
*/
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import net.mindview.util.*;
public class List extends JFrame {
private static final int WIDTH = 250;
private static final int HEIHGT = 375;
private String[] flavors = {
"Chocolate", "Strawberry", "Vanilla Fudge Swirl",
"Mint Chip", "Mocha Almond Fudge", "Rum Raisin",
"Praline Cream", "Mud Pie",
};
private DefaultListModel lItems = new DefaultListModel();
private JList lst = new JList(lItems);
private JTextArea t = new JTextArea(flavors.length, 20);
private JButton b = new JButton("Add item");
private ActionListener bl = new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (count < flavors.length) {
lItems.add(0, flavors[count++]);
return;
}
b.setEnabled(false);
}
};
private ListSelectionListener ll = new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
return;
}
t.setText("");
for (Object item : lst.getSelectedValues()) {
t.append(item + "\n");
}
}
};
private int count = 0;
public List() {
t.setEditable(false);
setLayout(new FlowLayout());
Border brd = BorderFactory.createMatteBorder(1, 1, 2, 2, Color.BLACK);
// lst.setBorder(brd);
// t.setBorder(brd);
for (int i = 0; i < 4; i++) {
lItems.addElement(flavors[count++]);
}
add(t);
add(new JScrollPane(lst));
add(b);
lst.addListSelectionListener(ll);
b.addActionListener(bl);
}
public static void main(String[] args) {
SwingConsole.run(new List(), WIDTH, HEIHGT);
}
}