-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitPaneDemo.java
More file actions
287 lines (238 loc) · 11 KB
/
SplitPaneDemo.java
File metadata and controls
287 lines (238 loc) · 11 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class SplitPaneDemo extends JFrame {
JSplitPane splitPane;
JPanel leftListPanel, rightListPanel;
Container contentPane;
String addString = "Add";
String removeString = "Remove";
String listData[] = {"duke","duke.running","duke2","dukeMagnify","dukeplug","dukeSnooze","dukeWave","dukeWaveRed","cow"};
SplitPaneDemo(){
super("SplitPane Demo !");
DefaultListModel leftListModel = new DefaultListModel();
DefaultListModel rightListModel = new DefaultListModel();
for (String item : listData) {
leftListModel.addElement(item);
}
JList leftList = new JList(leftListModel);
leftList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
leftList.setSelectedIndex(0);
leftList.setVisibleRowCount(30);
leftList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if(leftList.getSelectedValue() != null) {
rightListModel.removeAllElements();
// TODO: change for the values that you want to enter
rightListModel.addElement(leftList.getSelectedValue().toString());
}
}
});
// Left side panel
JScrollPane leftListScrollPane = new JScrollPane(leftList);
// Add new item to the left list button
JButton leftAddButton = new JButton(addString);
JTextField leftValueName = new JTextField(10);
AddListener leftAddListener = new AddListener(leftAddButton, leftList, leftListModel, leftValueName);
leftAddButton.setActionCommand(addString);
leftAddButton.addActionListener(leftAddListener);
leftAddButton.setEnabled(false);
// Remove item to the left list button
JButton leftRemoveButton = new JButton(removeString);
RemoveListener leftRemoveListener = new RemoveListener(leftList, leftListModel, leftRemoveButton);
leftRemoveButton.setActionCommand(removeString);
leftRemoveButton.addActionListener(leftRemoveListener);
leftValueName.addActionListener(leftAddListener);
leftValueName.getDocument().addDocumentListener(leftAddListener);
// Right side panel
JList rightList = new JList(rightListModel);
JScrollPane rightListScrollPane = new JScrollPane(rightList);
// Add button to add new item to right list
JButton rightAddButton = new JButton(addString);
JTextField rightValueName = new JTextField(10);
AddListener rightAddListener = new AddListener(rightAddButton, rightList, rightListModel,rightValueName);
rightAddButton.setActionCommand(addString);
rightAddButton.addActionListener(rightAddListener);
rightAddButton.setEnabled(false);
// Remove item to the right list button
JButton rightRemoveButton = new JButton(removeString);
RemoveListener rightRemoveListener = new RemoveListener(rightList, rightListModel, rightRemoveButton);
rightRemoveButton.setActionCommand(removeString);
rightRemoveButton.addActionListener(rightRemoveListener);
rightValueName.addActionListener(rightAddListener);
rightValueName.getDocument().addDocumentListener(rightAddListener);
// Create a panel that uses BoxLayout for the Left side.
JPanel leftButtonPane = new JPanel();
leftButtonPane.setLayout(new BoxLayout(leftButtonPane, BoxLayout.LINE_AXIS));
leftButtonPane.add(leftRemoveButton);
leftButtonPane.add(Box.createHorizontalStrut(5));
leftButtonPane.add(new JSeparator(SwingConstants.VERTICAL));
leftButtonPane.add(Box.createHorizontalStrut(5));
leftButtonPane.add(leftValueName);
leftButtonPane.add(leftAddButton);
leftButtonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// Create a panel that uses BoxLayout for the Right side.
JPanel rightButtonPane = new JPanel();
rightButtonPane.setLayout(new BoxLayout(rightButtonPane, BoxLayout.LINE_AXIS));
rightButtonPane.add(rightRemoveButton);
rightButtonPane.add(Box.createHorizontalStrut(5));
rightButtonPane.add(new JSeparator(SwingConstants.VERTICAL));
rightButtonPane.add(Box.createHorizontalStrut(5));
rightButtonPane.add(rightValueName);
rightButtonPane.add(rightAddButton);
rightButtonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// Adding Left ListPanel
leftListPanel = new JPanel(new BorderLayout());
leftListPanel.add(leftListScrollPane);
leftListPanel.add(leftButtonPane, BorderLayout.PAGE_END);
// Adding Right Pannel
rightListPanel = new JPanel(new BorderLayout());
rightListPanel.add(rightListScrollPane);
rightListPanel.add(rightButtonPane, BorderLayout.PAGE_END);
// Creating SplitPanel
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,false,new JScrollPane(leftListPanel),rightListPanel);
splitPane.setDividerSize(10);
splitPane.setOneTouchExpandable(true);
contentPane = getContentPane();
contentPane.add(splitPane);
setSize(800, 600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class RemoveListener implements ActionListener {
private JList list;
private DefaultListModel listModel;
private JButton removeButton;
public RemoveListener(JList remList, DefaultListModel remListModel, JButton removeButton) {
this.list = remList;
this.listModel = remListModel;
this.removeButton = removeButton;
}
public void actionPerformed(ActionEvent e) {
// This method can be called only if
// there's a valid selection
// so go ahead and remove whatever's selected.
int index = list.getSelectedIndex();
listModel.remove(index);
int size = listModel.getSize();
if (size == 0) { // Nobody's left, disable firing.
removeButton.setEnabled(false);
} else { // Select an index.
if (index == listModel.getSize()) {
// removed item in last position
index--;
}
list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
}
}
// This listener is shared by the text field and the add button.
class AddListener implements ActionListener, DocumentListener {
private boolean alreadyEnabled = false;
private JButton button;
private JList list;
private DefaultListModel listModel;
private JTextField valueName;
public AddListener(JButton button, JList listAdd, DefaultListModel listModelAdd, JTextField valueName) {
this.button = button;
this.list = listAdd;
this.listModel = listModelAdd;
this.valueName = valueName;
}
// Required by ActionListener.
public void actionPerformed(ActionEvent e) {
String name = valueName.getText();
// User didn't type in a unique name...
if (name.equals("") || alreadyInList(name)) {
// Toolkit.getDefaultToolkit().beep();
valueName.requestFocusInWindow();
valueName.selectAll();
return;
}
int index = list.getSelectedIndex(); // get selected index
if (index == -1) { // no selection, so insert at beginning
index = 0;
} else { // add after the selected item
index++;
}
// listModel.insertElementAt(valueName.getText(), index);
// If we just wanted to add to the end, we'd do this:
listModel.addElement(valueName.getText());
// Reset the text field.
valueName.requestFocusInWindow();
valueName.setText("");
// Select the new item and make it visible.
list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
// This method tests for string equality. You could certainly
// get more sophisticated about the algorithm. For example,
// you might want to ignore white space and capitalization.
protected boolean alreadyInList(String name) {
return listModel.contains(name);
}
// Required by DocumentListener.
public void insertUpdate(DocumentEvent e) {
enableButton();
}
// Required by DocumentListener.
public void removeUpdate(DocumentEvent e) {
handleEmptyTextField(e);
}
// Required by DocumentListener.
public void changedUpdate(DocumentEvent e) {
if (!handleEmptyTextField(e)) {
enableButton();
}
}
private void enableButton() {
if (!alreadyEnabled) {
button.setEnabled(true);
}
}
private boolean handleEmptyTextField(DocumentEvent e) {
if (e.getDocument().getLength() <= 0) {
button.setEnabled(false);
alreadyEnabled = false;
return true;
}
return false;
}
}
// This method is required by ListSelectionListener.
// public void valueChanged(ListSelectionEvent e) {
// if (e.getValueIsAdjusting() == false) {
// if (list.getSelectedIndex() == -1) {
// // No selection, disable fire button.
// removeButton.setEnabled(false);
// } else {
// // Selection, enable the fire button.
// removeButton.setEnabled(true);
// }
// }
// }
public static void main(String[] args) {
new SplitPaneDemo();
}
}