-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathListFrame.java
More file actions
86 lines (75 loc) · 2.62 KB
/
Copy pathListFrame.java
File metadata and controls
86 lines (75 loc) · 2.62 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 list;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* This frame contains a word list and a label that shows a sentence made up from the chosen words.
* Note that you can select multiple words with Ctrl+click and Shift+click.
*/
class ListFrame extends JFrame
{
private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 300;
private JPanel listPanel;
private JList<String> wordList;
private JLabel label;
private JPanel buttonPanel;
private ButtonGroup group;
private String prefix = "The ";
private String suffix = "fox jumps over the lazy dog.";
public ListFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
String[] words = { "quick", "brown", "hungry", "wild", "silent", "huge", "private",
"abstract", "static", "final" };
wordList = new JList<>(words);
wordList.setVisibleRowCount(4);
JScrollPane scrollPane = new JScrollPane(wordList);
listPanel = new JPanel();
listPanel.add(scrollPane);
wordList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent event)
{
StringBuilder text = new StringBuilder(prefix);
for (String value : wordList.getSelectedValuesList())
{
text.append(value);
text.append(" ");
}
text.append(suffix);
label.setText(text.toString());
}
});
buttonPanel = new JPanel();
group = new ButtonGroup();
makeButton("Vertical", JList.VERTICAL);
makeButton("Vertical Wrap", JList.VERTICAL_WRAP);
makeButton("Horizontal Wrap", JList.HORIZONTAL_WRAP);
add(listPanel, BorderLayout.NORTH);
label = new JLabel(prefix + suffix);
add(label, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
/**
* Makes a radio button to set the layout orientation.
* @param label the button label
* @param orientation the orientation for the list
*/
private void makeButton(String label, final int orientation)
{
JRadioButton button = new JRadioButton(label);
buttonPanel.add(button);
if (group.getButtonCount() == 0) button.setSelected(true);
group.add(button);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
wordList.setLayoutOrientation(orientation);
listPanel.revalidate();
}
});
}
}