OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [examples/] [gnu/] [classpath/] [examples/] [swing/] [ComboBoxDemo.java] - Blame information for rev 781

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 781 jeremybenn
/* ComboBoxDemo.java -- An example showing various combo boxes in Swing.
2
   Copyright (C) 2005, 2006,  Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath examples.
5
 
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)
9
any later version.
10
 
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.
15
 
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
19
02110-1301 USA.
20
*/
21
 
22
 
23
package gnu.classpath.examples.swing;
24
 
25
import java.awt.BorderLayout;
26
import java.awt.Color;
27
import java.awt.Component;
28
import java.awt.Dimension;
29
import java.awt.Font;
30
import java.awt.GridLayout;
31
import java.awt.event.ActionEvent;
32
import java.awt.event.ActionListener;
33
 
34
import javax.swing.BorderFactory;
35
import javax.swing.DefaultListCellRenderer;
36
import javax.swing.Icon;
37
import javax.swing.JButton;
38
import javax.swing.JCheckBox;
39
import javax.swing.JComboBox;
40
import javax.swing.JComponent;
41
import javax.swing.JFrame;
42
import javax.swing.JList;
43
import javax.swing.JPanel;
44
import javax.swing.SwingUtilities;
45
import javax.swing.plaf.metal.MetalIconFactory;
46
 
47
/**
48
 * A simple demo showing various combo boxes in different states.
49
 */
50
public class ComboBoxDemo
51
  extends JPanel
52
  implements ActionListener
53
{
54
 
55
  class CustomCellRenderer extends DefaultListCellRenderer
56
  {
57
    public Component getListCellRendererComponent(JList list,
58
                                              Object value,
59
                                              int index,
60
                                              boolean isSelected,
61
                                              boolean cellHasFocus)
62
    {
63
      DefaultListCellRenderer result = (DefaultListCellRenderer)
64
          super.getListCellRendererComponent(list, value, index, isSelected,
65
          cellHasFocus);
66
      Icon icon = (Icon) value;
67
      result.setIcon(icon);
68
      result.setText("Index = " + index);
69
      return result;
70
    }
71
  }
72
 
73
  private JCheckBox comboState1;
74
  private JComboBox combo1;
75
  private JComboBox combo2;
76
 
77
  private JCheckBox comboState2;
78
  private JComboBox combo3;
79
  private JComboBox combo4;
80
 
81
  private JCheckBox comboState3;
82
  private JComboBox combo5;
83
  private JComboBox combo6;
84
 
85
  private JCheckBox comboState4;
86
  private JComboBox combo7;
87
  private JComboBox combo8;
88
 
89
  private JCheckBox comboState5;
90
  private JComboBox combo9;
91
  private JComboBox combo10;
92
 
93
  private JCheckBox comboState6;
94
  private JComboBox combo11;
95
  private JComboBox combo12;
96
 
97
  /**
98
   * Creates a new demo instance.
99
   */
100
  public ComboBoxDemo()
101
  {
102
    super();
103
    createContent();
104
  }
105
 
106
  /**
107
   * When the demo is run independently, the frame is displayed, so we should
108
   * initialise the content panel (including the demo content and a close
109
   * button).  But when the demo is run as part of the Swing activity board,
110
   * only the demo content panel is used, the frame itself is never displayed,
111
   * so we can avoid this step.
112
   */
113
  void initFrameContent()
114
  {
115
    JPanel closePanel = new JPanel();
116
    JButton closeButton = new JButton("Close");
117
    closeButton.setActionCommand("CLOSE");
118
    closeButton.addActionListener(this);
119
    closePanel.add(closeButton);
120
    add(closePanel, BorderLayout.SOUTH);
121
  }
122
 
123
  /**
124
   * Returns a panel with the demo content.  The panel
125
   * uses a BorderLayout(), and the BorderLayout.SOUTH area
126
   * is empty, to allow callers to add controls to the
127
   * bottom of the panel if they want to (a close button is
128
   * added if this demo is being run as a standalone demo).
129
   */
130
  private void createContent()
131
  {
132
    setLayout(new BorderLayout());
133
    JPanel panel = new JPanel(new GridLayout(6, 1));
134
    panel.add(createPanel1());
135
    panel.add(createPanel2());
136
    panel.add(createPanel3());
137
    panel.add(createPanel4());
138
    panel.add(createPanel5());
139
    panel.add(createPanel6());
140
    add(panel);
141
  }
142
 
143
  private JPanel createPanel1()
144
  {
145
    JPanel panel = new JPanel(new BorderLayout());
146
    this.comboState1 = new JCheckBox("Enabled", true);
147
    this.comboState1.setActionCommand("COMBO_STATE1");
148
    this.comboState1.addActionListener(this);
149
    panel.add(this.comboState1, BorderLayout.EAST);
150
 
151
    JPanel controlPanel = new JPanel();
152
    controlPanel.setBorder(BorderFactory.createTitledBorder("Regular: "));
153
    this.combo1 = new JComboBox(new Object[] {"Australia", "New Zealand",
154
            "England"});
155
 
156
    this.combo2 = new JComboBox(new Object[] {"Australia", "New Zealand",
157
            "England"});
158
    this.combo2.setEditable(true);
159
 
160
    controlPanel.add(combo1);
161
    controlPanel.add(combo2);
162
 
163
    panel.add(controlPanel);
164
 
165
    return panel;
166
  }
167
 
168
  private JPanel createPanel2()
169
  {
170
    JPanel panel = new JPanel(new BorderLayout());
171
    this.comboState2 = new JCheckBox("Enabled", true);
172
    this.comboState2.setActionCommand("COMBO_STATE2");
173
    this.comboState2.addActionListener(this);
174
    panel.add(this.comboState2, BorderLayout.EAST);
175
 
176
    JPanel controlPanel = new JPanel();
177
    controlPanel.setBorder(BorderFactory.createTitledBorder("Large Font: "));
178
    this.combo3 = new JComboBox(new Object[] {"Australia", "New Zealand",
179
            "England"});
180
    this.combo3.setFont(new Font("Dialog", Font.PLAIN, 20));
181
 
182
    this.combo4 = new JComboBox(new Object[] {"Australia", "New Zealand",
183
            "England"});
184
    this.combo4.setEditable(true);
185
    this.combo4.setFont(new Font("Dialog", Font.PLAIN, 20));
186
 
187
    controlPanel.add(combo3);
188
    controlPanel.add(combo4);
189
 
190
    panel.add(controlPanel);
191
 
192
    return panel;
193
  }
194
 
195
  private JPanel createPanel3()
196
  {
197
    JPanel panel = new JPanel(new BorderLayout());
198
    this.comboState3 = new JCheckBox("Enabled", true);
199
    this.comboState3.setActionCommand("COMBO_STATE3");
200
    this.comboState3.addActionListener(this);
201
    panel.add(this.comboState3, BorderLayout.EAST);
202
 
203
    JPanel controlPanel = new JPanel();
204
    controlPanel.setBorder(BorderFactory.createTitledBorder("Colored Background: "));
205
    this.combo5 = new JComboBox(new Object[] {"Australia", "New Zealand",
206
            "England"});
207
    this.combo5.setBackground(Color.yellow);
208
 
209
    this.combo6 = new JComboBox(new Object[] {"Australia", "New Zealand",
210
            "England"});
211
    this.combo6.setEditable(true);
212
    this.combo6.setBackground(Color.yellow);
213
 
214
    controlPanel.add(combo5);
215
    controlPanel.add(combo6);
216
 
217
    panel.add(controlPanel);
218
 
219
    return panel;
220
  }
221
 
222
  /**
223
   * This panel contains combo boxes that are empty.
224
   *
225
   * @return A panel.
226
   */
227
  private JPanel createPanel4()
228
  {
229
    JPanel panel = new JPanel(new BorderLayout());
230
    this.comboState4 = new JCheckBox("Enabled", true);
231
    this.comboState4.setActionCommand("COMBO_STATE4");
232
    this.comboState4.addActionListener(this);
233
    panel.add(this.comboState4, BorderLayout.EAST);
234
 
235
    JPanel controlPanel = new JPanel();
236
    controlPanel.setBorder(BorderFactory.createTitledBorder("Empty: "));
237
    this.combo7 = new JComboBox();
238
    this.combo8 = new JComboBox();
239
    this.combo8.setEditable(true);
240
 
241
    controlPanel.add(combo7);
242
    controlPanel.add(combo8);
243
 
244
    panel.add(controlPanel);
245
 
246
    return panel;
247
  }
248
 
249
  /**
250
   * This panel contains combo boxes that are narrow but contain long text
251
   * items.
252
   *
253
   * @return A panel.
254
   */
255
  private JPanel createPanel5()
256
  {
257
    JPanel panel = new JPanel(new BorderLayout());
258
    this.comboState5 = new JCheckBox("Enabled", true);
259
    this.comboState5.setActionCommand("COMBO_STATE5");
260
    this.comboState5.addActionListener(this);
261
    panel.add(this.comboState5, BorderLayout.EAST);
262
 
263
    JPanel controlPanel = new JPanel();
264
    controlPanel.setBorder(BorderFactory.createTitledBorder("Narrow: "));
265
    this.combo9 = new JComboBox(new Object[] {
266
            "A really long item that will be truncated when displayed"});
267
    this.combo9.setPreferredSize(new Dimension(100, 30));
268
    this.combo10 = new JComboBox(new Object[] {
269
            "A really long item that will be truncated when displayed"});
270
    this.combo10.setPreferredSize(new Dimension(100, 30));
271
    this.combo10.setEditable(true);
272
 
273
    controlPanel.add(combo9);
274
    controlPanel.add(combo10);
275
 
276
    panel.add(controlPanel);
277
 
278
    return panel;
279
  }
280
 
281
  /**
282
   * This panel contains combo boxes with a custom renderer.
283
   *
284
   * @return A panel.
285
   */
286
  private JPanel createPanel6()
287
  {
288
    JPanel panel = new JPanel(new BorderLayout());
289
    this.comboState6 = new JCheckBox("Enabled", true);
290
    this.comboState6.setActionCommand("COMBO_STATE6");
291
    this.comboState6.addActionListener(this);
292
    panel.add(this.comboState6, BorderLayout.EAST);
293
 
294
    JPanel controlPanel = new JPanel();
295
    controlPanel.setBorder(BorderFactory.createTitledBorder("Custom Renderer: "));
296
    this.combo11 = new JComboBox(new Object[] {
297
            MetalIconFactory.getFileChooserHomeFolderIcon(),
298
            MetalIconFactory.getFileChooserNewFolderIcon()});
299
    this.combo11.setPreferredSize(new Dimension(100, 30));
300
    this.combo11.setRenderer(new CustomCellRenderer());
301
    this.combo12 = new JComboBox(new Object[] {
302
            MetalIconFactory.getFileChooserHomeFolderIcon(),
303
            MetalIconFactory.getFileChooserNewFolderIcon()});
304
    this.combo12.setPreferredSize(new Dimension(100, 30));
305
    this.combo12.setRenderer(new CustomCellRenderer());
306
    this.combo12.setEditable(true);
307
 
308
    controlPanel.add(combo11);
309
    controlPanel.add(combo12);
310
 
311
    panel.add(controlPanel);
312
 
313
    return panel;
314
  }
315
 
316
  public void actionPerformed(ActionEvent e)
317
  {
318
    if (e.getActionCommand().equals("COMBO_STATE1"))
319
    {
320
      combo1.setEnabled(comboState1.isSelected());
321
      combo2.setEnabled(comboState1.isSelected());
322
    }
323
    else if (e.getActionCommand().equals("COMBO_STATE2"))
324
    {
325
      combo3.setEnabled(comboState2.isSelected());
326
      combo4.setEnabled(comboState2.isSelected());
327
    }
328
    else if (e.getActionCommand().equals("COMBO_STATE3"))
329
    {
330
      combo5.setEnabled(comboState3.isSelected());
331
      combo6.setEnabled(comboState3.isSelected());
332
    }
333
    else if (e.getActionCommand().equals("COMBO_STATE4"))
334
    {
335
      combo7.setEnabled(comboState4.isSelected());
336
      combo8.setEnabled(comboState4.isSelected());
337
    }
338
    else if (e.getActionCommand().equals("COMBO_STATE5"))
339
    {
340
      combo9.setEnabled(comboState5.isSelected());
341
      combo10.setEnabled(comboState5.isSelected());
342
    }
343
    else if (e.getActionCommand().equals("COMBO_STATE6"))
344
    {
345
      combo11.setEnabled(comboState6.isSelected());
346
      combo12.setEnabled(comboState6.isSelected());
347
    }
348
    else if (e.getActionCommand().equals("CLOSE"))
349
    {
350
      System.exit(0);
351
    }
352
  }
353
 
354
  public static void main(String[] args)
355
  {
356
    SwingUtilities.invokeLater
357
    (new Runnable()
358
     {
359
       public void run()
360
       {
361
         ComboBoxDemo app = new ComboBoxDemo();
362
         app.initFrameContent();
363
         JFrame frame = new JFrame();
364
         frame.getContentPane().add(app);
365
         frame.pack();
366
         frame.setVisible(true);
367
       }
368
     });
369
  }
370
 
371
  /**
372
   * Returns a DemoFactory that creates a ComboBoxDemo.
373
   *
374
   * @return a DemoFactory that creates a ComboBoxDemo
375
   */
376
  public static DemoFactory createDemoFactory()
377
  {
378
    return new DemoFactory()
379
    {
380
      public JComponent createDemo()
381
      {
382
        return new ComboBoxDemo();
383
      }
384
    };
385
  }
386
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.