1
/* ListDemo.java -- Demostrates JList
2
Copyright (C) 2006 Free Software Foundation, Inc.
4
This file is part of GNU Classpath.
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING. If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library. Thus, the terms and
23
conditions of the GNU General Public License cover the whole
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module. An independent module is a module which is not derived from
33
or based on this library. If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so. If you do not wish to do so, delete this
36
exception statement from your version. */
39
package gnu.classpath.examples.swing;
41
import java.awt.BorderLayout;
42
import java.awt.Component;
43
import java.awt.GridLayout;
44
import java.awt.event.ActionEvent;
45
import java.awt.event.ActionListener;
47
import javax.swing.DefaultListCellRenderer;
48
import javax.swing.DefaultListModel;
49
import javax.swing.JButton;
50
import javax.swing.JCheckBox;
51
import javax.swing.JComponent;
52
import javax.swing.JFrame;
53
import javax.swing.JList;
54
import javax.swing.JPanel;
55
import javax.swing.JScrollPane;
56
import javax.swing.JSplitPane;
57
import javax.swing.ListCellRenderer;
58
import javax.swing.SwingUtilities;
62
implements ActionListener
65
private static class LabelCellRenderer
66
extends DefaultListCellRenderer
68
public Component getListCellRendererComponent(JList list,
74
Component c = super.getListCellRendererComponent(list, value, index,
81
private static class CheckCellRenderer
83
implements ListCellRenderer
85
public Component getListCellRendererComponent(JList list,
91
setSelected(isSelected);
92
setText(value.toString());
104
private void createContent()
107
String foo[] = new String[] {
117
String bar[] = new String[] {
125
final DefaultListModel mod = new DefaultListModel();
126
final JList list1 = new JList(mod);
127
final JList list2 = new JList(mod);
129
list2.setSelectionModel(list1.getSelectionModel());
130
for (int i = 0; i < bar.length; ++i)
131
for (int j = 0; j < foo.length; ++j)
132
mod.addElement(foo[j] + bar[i]);
134
list1.setCellRenderer(new LabelCellRenderer());
135
list2.setCellRenderer(new CheckCellRenderer());
137
JButton add = new JButton("add element");
138
add.addActionListener(new ActionListener()
141
public void actionPerformed(ActionEvent e)
143
mod.addElement("new element " + i);
148
JButton del = new JButton("delete selected");
149
del.addActionListener(new ActionListener()
151
public void actionPerformed(ActionEvent e)
153
for (int i = 0; i < mod.getSize(); ++i)
154
if (list1.isSelectedIndex(i))
160
JSplitPane splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
161
splitter.add(new JScrollPane(list1), JSplitPane.LEFT);
162
splitter.add(new JScrollPane(list2), JSplitPane.RIGHT);
164
setLayout(new BorderLayout());
165
JPanel p2 = new JPanel();
166
p2.setLayout(new GridLayout(1, 2));
170
add(p2, BorderLayout.NORTH);
171
add(splitter, BorderLayout.CENTER);
174
public void actionPerformed(ActionEvent e)
176
if (e.getActionCommand().equals("CLOSE"))
183
* When the demo is run independently, the frame is displayed, so we should
184
* initialise the content panel (including the demo content and a close
185
* button). But when the demo is run as part of the Swing activity board,
186
* only the demo content panel is used, the frame itself is never displayed,
187
* so we can avoid this step.
189
void initFrameContent()
191
JPanel closePanel = new JPanel();
192
JButton closeButton = new JButton("Close");
193
closeButton.setActionCommand("CLOSE");
194
closeButton.addActionListener(this);
195
closePanel.add(closeButton);
196
add(closePanel, BorderLayout.SOUTH);
199
public static void main(String[] args)
201
SwingUtilities.invokeLater
206
ListDemo app = new ListDemo();
207
app.initFrameContent();
208
JFrame frame = new JFrame("List Demo");
209
frame.getContentPane().add(app);
211
frame.setVisible(true);
217
* Returns a DemoFactory that creates a ListDemo.
219
* @return a DemoFactory that creates a ListDemo
221
public static DemoFactory createDemoFactory()
223
return new DemoFactory()
225
public JComponent createDemo()
227
return new ListDemo();