KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > swingset > ButtonDemo


1 /*
2  * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * -Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * -Redistribution in binary form must reproduct the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the distribution.
14  *
15  * Neither the name of Sun Microsystems, Inc. or the names of contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * This software is provided "AS IS," without a warranty of any kind. ALL
20  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30  *
31  * You acknowledge that Software is not designed, licensed or intended for
32  * use in the design, construction, operation or maintenance of any nuclear
33  * facility.
34  */

35
36 /*
37  * @(#)ButtonDemo.java 1.10 03/01/23
38  */

39 package demo.swingset;
40
41 import swingwtx.swing.*;
42 import swingwtx.swing.event.*;
43 import swingwtx.swing.text.*;
44 import swingwtx.swing.border.*;
45 import swingwtx.swing.colorchooser.*;
46 import swingwtx.swing.filechooser.*;
47 import javax.accessibility.*;
48
49 import swingwt.awt.*;
50 import swingwt.awt.event.*;
51 import java.beans.*;
52 import java.util.*;
53 import java.io.*;
54 import java.applet.*;
55 import java.net.*;
56
57 /**
58  * JButton, JRadioButton, JToggleButton, JCheckBox Demos
59  *
60  * @version 1.10 01/23/03
61  * @author Jeff Dinkins
62  */

63 public class ButtonDemo extends DemoModule implements ChangeListener {
64
65     JTabbedPane tab;
66
67     JPanel buttonPanel = new JPanel();
68     JPanel checkboxPanel = new JPanel();
69     JPanel radioButtonPanel = new JPanel();
70     JPanel toggleButtonPanel = new JPanel();
71
72     Vector buttons = new Vector();
73     Vector checkboxes = new Vector();
74     Vector radiobuttons = new Vector();
75     Vector togglebuttons = new Vector();
76
77     Vector currentControls = buttons;
78
79     JButton button;
80     JCheckBox check;
81     JRadioButton radio;
82     JToggleButton toggle;
83
84     EmptyBorder border5 = new EmptyBorder(5,5,5,5);
85     EmptyBorder border10 = new EmptyBorder(10,10,10,10);
86
87     ItemListener buttonDisplayListener = null;
88     ItemListener buttonPadListener = null;
89
90     Insets insets0 = new Insets(0,0,0,0);
91     Insets insets10 = new Insets(10,10,10,10);
92
93     /**
94      * main method allows us to run as a standalone demo.
95      */

96     public static void main(String JavaDoc[] args) {
97     ButtonDemo demo = new ButtonDemo(null);
98     demo.mainImpl();
99     }
100
101     /**
102      * ButtonDemo Constructor
103      */

104     public ButtonDemo(SwingSet2 swingset) {
105     // Set the title for this demo, and an icon used to represent this
106
// demo inside the SwingSet2 app.
107
super(swingset, "ButtonDemo", "toolbar/JButton.gif");
108
109     tab = new JTabbedPane();
110     tab.addChangeListener(this);
111
112     JPanel demo = getDemoPanel();
113     demo.setLayout(new BoxLayout(demo, BoxLayout.Y_AXIS));
114     demo.add(tab);
115
116     addButtons();
117     addRadioButtons();
118     addCheckBoxes();
119     //addToggleButtons(); -- this doesn't do anything in SwingSet2 - why?
120
currentControls = buttons;
121     }
122
123     public void addButtons() {
124     tab.addTab(getString("ButtonDemo.buttons"), buttonPanel);
125     buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
126         buttonPanel.setBorder(border5);
127
128     JPanel p1 = createVerticalPanel(true);
129     p1.setAlignmentY(TOP_ALIGNMENT);
130     buttonPanel.add(p1);
131
132     // Text Buttons
133
JPanel p2 = createHorizontalPanel(false);
134     p1.add(p2);
135     p2.setBorder(new CompoundBorder(new TitledBorder(null, getString("ButtonDemo.textbuttons"),
136                               TitledBorder.LEFT, TitledBorder.TOP), border5));
137
138     buttons.add(p2.add(new JButton(getString("ButtonDemo.button1"))));
139     p2.add(Box.createRigidArea(HGAP10));
140
141     buttons.add(p2.add(new JButton(getString("ButtonDemo.button2"))));
142     p2.add(Box.createRigidArea(HGAP10));
143
144     buttons.add(p2.add(new JButton(getString("ButtonDemo.button3"))));
145
146
147     // Image Buttons
148
p1.add(Box.createRigidArea(VGAP30));
149     JPanel p3 = createHorizontalPanel(false);
150     p1.add(p3);
151     p3.setLayout(new BoxLayout(p3, BoxLayout.X_AXIS));
152     p3.setBorder(new TitledBorder(null, getString("ButtonDemo.imagebuttons"),
153                      TitledBorder.LEFT, TitledBorder.TOP));
154
155     // home image button
156
String JavaDoc description = getString("ButtonDemo.phone");
157     button = new JButton(createImageIcon("buttons/b1.gif", description));
158     button.setPressedIcon(createImageIcon("buttons/b1p.gif", description));
159     button.setRolloverIcon(createImageIcon("buttons/b1r.gif", description));
160     button.setDisabledIcon(createImageIcon("buttons/b1d.gif", description));
161     button.setMargin(new Insets(0,0,0,0));
162     p3.add(button);
163     buttons.add(button);
164     p3.add(Box.createRigidArea(HGAP10));
165
166     // write image button
167
description = getString("ButtonDemo.write");
168     button = new JButton(createImageIcon("buttons/b2.gif", description));
169     button.setPressedIcon(createImageIcon("buttons/b2p.gif", description));
170     button.setRolloverIcon(createImageIcon("buttons/b2r.gif", description));
171     button.setDisabledIcon(createImageIcon("buttons/b2d.gif", description));
172     button.setMargin(new Insets(0,0,0,0));
173     p3.add(button);
174     buttons.add(button);
175     p3.add(Box.createRigidArea(HGAP10));
176
177     // write image button
178
description = getString("ButtonDemo.peace");
179     button = new JButton(createImageIcon("buttons/b3.gif", description));
180     button.setPressedIcon(createImageIcon("buttons/b3p.gif", description));
181     button.setRolloverIcon(createImageIcon("buttons/b3r.gif", description));
182     button.setDisabledIcon(createImageIcon("buttons/b3d.gif", description));
183     button.setMargin(new Insets(0,0,0,0));
184     p3.add(button);
185     buttons.add(button);
186
187     p1.add(Box.createVerticalGlue());
188
189     buttonPanel.add(Box.createRigidArea(HGAP10));
190     currentControls = buttons;
191     buttonPanel.add(createControls());
192     }
193
194     public void addRadioButtons() {
195     ButtonGroup group = new ButtonGroup();
196
197     tab.addTab(getString("ButtonDemo.radiobuttons"), radioButtonPanel);
198     radioButtonPanel.setLayout(new BoxLayout(radioButtonPanel, BoxLayout.X_AXIS));
199         radioButtonPanel.setBorder(border5);
200
201     JPanel p1 = createVerticalPanel(true);
202     p1.setAlignmentY(TOP_ALIGNMENT);
203     radioButtonPanel.add(p1);
204
205     // Text Radio Buttons
206
JPanel p2 = createHorizontalPanel(false);
207     p1.add(p2);
208     p2.setBorder(new CompoundBorder(
209                       new TitledBorder(
210             null, getString("ButtonDemo.textradiobuttons"),
211             TitledBorder.LEFT, TitledBorder.TOP), border5)
212     );
213
214         radio = (JRadioButton)p2.add(
215                 new JRadioButton(getString("ButtonDemo.radio1")));
216         group.add(radio);
217     radiobuttons.add(radio);
218     p2.add(Box.createRigidArea(HGAP10));
219
220     radio = (JRadioButton)p2.add(
221                 new JRadioButton(getString("ButtonDemo.radio2")));
222         group.add(radio);
223     radiobuttons.add(radio);
224     p2.add(Box.createRigidArea(HGAP10));
225
226     radio = (JRadioButton)p2.add(
227                 new JRadioButton(getString("ButtonDemo.radio3")));
228         group.add(radio);
229     radiobuttons.add(radio);
230
231     // Image Radio Buttons
232
group = new ButtonGroup();
233     p1.add(Box.createRigidArea(VGAP30));
234     JPanel p3 = createHorizontalPanel(false);
235     p1.add(p3);
236     p3.setLayout(new BoxLayout(p3, BoxLayout.X_AXIS));
237     p3.setBorder(new TitledBorder(null, getString("ButtonDemo.imageradiobuttons"),
238                      TitledBorder.LEFT, TitledBorder.TOP));
239
240     // image radio button 1
241
String JavaDoc description = getString("ButtonDemo.customradio");
242     String JavaDoc text = getString("ButtonDemo.radio1");
243     radio = new JRadioButton(text, createImageIcon("buttons/rb.gif", description));
244     radio.setPressedIcon(createImageIcon("buttons/rbp.gif", description));
245     radio.setRolloverIcon(createImageIcon("buttons/rbr.gif", description));
246     radio.setRolloverSelectedIcon(createImageIcon("buttons/rbrs.gif", description));
247     radio.setSelectedIcon(createImageIcon("buttons/rbs.gif", description));
248     radio.setMargin(new Insets(0,0,0,0));
249     group.add(radio);
250     p3.add(radio);
251     radiobuttons.add(radio);
252     p3.add(Box.createRigidArea(HGAP20));
253
254     // image radio button 2
255
text = getString("ButtonDemo.radio2");
256     radio = new JRadioButton(text, createImageIcon("buttons/rb.gif", description));
257     radio.setPressedIcon(createImageIcon("buttons/rbp.gif", description));
258     radio.setRolloverIcon(createImageIcon("buttons/rbr.gif", description));
259     radio.setRolloverSelectedIcon(createImageIcon("buttons/rbrs.gif", description));
260     radio.setSelectedIcon(createImageIcon("buttons/rbs.gif", description));
261     radio.setMargin(new Insets(0,0,0,0));
262     group.add(radio);
263     p3.add(radio);
264     radiobuttons.add(radio);
265     p3.add(Box.createRigidArea(HGAP20));
266
267     // image radio button 3
268
text = getString("ButtonDemo.radio3");
269     radio = new JRadioButton(text, createImageIcon("buttons/rb.gif", description));
270     radio.setPressedIcon(createImageIcon("buttons/rbp.gif", description));
271     radio.setRolloverIcon(createImageIcon("buttons/rbr.gif", description));
272     radio.setRolloverSelectedIcon(createImageIcon("buttons/rbrs.gif", description));
273     radio.setSelectedIcon(createImageIcon("buttons/rbs.gif", description));
274     radio.setMargin(new Insets(0,0,0,0));
275     group.add(radio);
276     radiobuttons.add(radio);
277     p3.add(radio);
278
279     // verticaly glue fills out the rest of the box
280
p1.add(Box.createVerticalGlue());
281
282     radioButtonPanel.add(Box.createRigidArea(HGAP10));
283     currentControls = radiobuttons;
284     radioButtonPanel.add(createControls());
285     }
286
287
288     public void addCheckBoxes() {
289     tab.addTab(getString("ButtonDemo.checkboxes"), checkboxPanel);
290     checkboxPanel.setLayout(new BoxLayout(checkboxPanel, BoxLayout.X_AXIS));
291         checkboxPanel.setBorder(border5);
292
293     JPanel p1 = createVerticalPanel(true);
294     p1.setAlignmentY(TOP_ALIGNMENT);
295     checkboxPanel.add(p1);
296
297     // Text Radio Buttons
298
JPanel p2 = createHorizontalPanel(false);
299     p1.add(p2);
300     p2.setBorder(new CompoundBorder(
301                       new TitledBorder(
302             null, getString("ButtonDemo.textcheckboxes"),
303             TitledBorder.LEFT, TitledBorder.TOP), border5)
304     );
305
306     checkboxes.add(p2.add(new JCheckBox(getString("ButtonDemo.check1"))));
307     p2.add(Box.createRigidArea(HGAP10));
308
309     checkboxes.add(p2.add(new JCheckBox(getString("ButtonDemo.check2"))));
310     p2.add(Box.createRigidArea(HGAP10));
311
312     checkboxes.add(p2.add(new JCheckBox(getString("ButtonDemo.check3"))));
313
314     // Image Radio Buttons
315
p1.add(Box.createRigidArea(VGAP30));
316     JPanel p3 = createHorizontalPanel(false);
317     p1.add(p3);
318     p3.setLayout(new BoxLayout(p3, BoxLayout.X_AXIS));
319     p3.setBorder(new TitledBorder(null, getString("ButtonDemo.imagecheckboxes"),
320                      TitledBorder.LEFT, TitledBorder.TOP));
321
322     // image checkbox 1
323
String JavaDoc description = getString("ButtonDemo.customcheck");
324     String JavaDoc text = getString("ButtonDemo.check1");
325     check = new JCheckBox(text, createImageIcon("buttons/cb.gif", description));
326     check.setRolloverIcon(createImageIcon("buttons/cbr.gif", description));
327     check.setRolloverSelectedIcon(createImageIcon("buttons/cbrs.gif", description));
328     check.setSelectedIcon(createImageIcon("buttons/cbs.gif", description));
329     check.setMargin(new Insets(0,0,0,0));
330     p3.add(check);
331     checkboxes.add(check);
332     p3.add(Box.createRigidArea(HGAP20));
333
334     // image checkbox 2
335
text = getString("ButtonDemo.check2");
336     check = new JCheckBox(text, createImageIcon("buttons/cb.gif", description));
337     check.setRolloverIcon(createImageIcon("buttons/cbr.gif", description));
338     check.setRolloverSelectedIcon(createImageIcon("buttons/cbrs.gif", description));
339     check.setSelectedIcon(createImageIcon("buttons/cbs.gif", description));
340     check.setMargin(new Insets(0,0,0,0));
341     p3.add(check);
342     checkboxes.add(check);
343     p3.add(Box.createRigidArea(HGAP20));
344
345     // image checkbox 3
346
text = getString("ButtonDemo.check3");
347     check = new JCheckBox(text, createImageIcon("buttons/cb.gif", description));
348     check.setRolloverIcon(createImageIcon("buttons/cbr.gif", description));
349     check.setRolloverSelectedIcon(createImageIcon("buttons/cbrs.gif", description));
350     check.setSelectedIcon(createImageIcon("buttons/cbs.gif", description));
351     check.setMargin(new Insets(0,0,0,0));
352     p3.add(check);
353     checkboxes.add(check);
354
355     // verticaly glue fills out the rest of the box
356
p1.add(Box.createVerticalGlue());
357
358     checkboxPanel.add(Box.createRigidArea(HGAP10));
359     currentControls = checkboxes;
360     checkboxPanel.add(createControls());
361     }
362
363     public void addToggleButtons() {
364     tab.addTab(getString("ButtonDemo.togglebuttons"), toggleButtonPanel);
365     }
366
367     public JPanel createControls() {
368         JPanel controls = new JPanel() {
369             public Dimension getMaximumSize() {
370                 return new Dimension(300, super.getMaximumSize().height);
371             }
372         };
373         controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
374         controls.setAlignmentY(TOP_ALIGNMENT);
375         controls.setAlignmentX(LEFT_ALIGNMENT);
376
377         JPanel buttonControls = createHorizontalPanel(true);
378         buttonControls.setAlignmentY(TOP_ALIGNMENT);
379         buttonControls.setAlignmentX(LEFT_ALIGNMENT);
380
381         JPanel leftColumn = createVerticalPanel(false);
382         leftColumn.setAlignmentX(LEFT_ALIGNMENT);
383         leftColumn.setAlignmentY(TOP_ALIGNMENT);
384
385         JPanel rightColumn = new LayoutControlPanel(this);
386
387         buttonControls.add(leftColumn);
388         buttonControls.add(Box.createRigidArea(HGAP20));
389         buttonControls.add(rightColumn);
390         buttonControls.add(Box.createRigidArea(HGAP20));
391
392         controls.add(buttonControls);
393
394     createListeners();
395
396         // Display Options
397
JLabel l = new JLabel("Display Options:");
398         leftColumn.add(l);
399
400         JCheckBox bordered = new JCheckBox("Paint Border");
401         bordered.setToolTipText("Click here to turn border painting on or off.");
402         bordered.setMnemonic('b');
403     if (currentControls == buttons) {
404             bordered.setSelected(true);
405     }
406         bordered.addItemListener(buttonDisplayListener);
407         leftColumn.add(bordered);
408
409         JCheckBox focused = new JCheckBox("Paint Focus");
410         focused.setToolTipText("Click here to turn focus painting on or off.");
411         focused.setMnemonic('f');
412         focused.setSelected(true);
413         focused.addItemListener(buttonDisplayListener);
414         leftColumn.add(focused);
415
416         JCheckBox enabled = new JCheckBox("Enabled");
417         enabled.setToolTipText("Click here to enable or disable the buttons.");
418         enabled.setSelected(true);
419         enabled.addItemListener(buttonDisplayListener);
420         enabled.setMnemonic('e');
421         leftColumn.add(enabled);
422
423         JCheckBox filled = new JCheckBox("Content Filled");
424         filled.setToolTipText("Click here to control the filling of the content area.");
425         filled.setSelected(true);
426         filled.addItemListener(buttonDisplayListener);
427         filled.setMnemonic('i');
428         leftColumn.add(filled);
429
430         leftColumn.add(Box.createRigidArea(VGAP20));
431
432         l = new JLabel("Pad Amount:");
433         leftColumn.add(l);
434         ButtonGroup group = new ButtonGroup();
435         JRadioButton defaultPad = new JRadioButton("Default");
436         defaultPad.setToolTipText("Uses the default padding between the border and label.");
437         defaultPad.setMnemonic('d');
438         defaultPad.addItemListener(buttonPadListener);
439         group.add(defaultPad);
440         defaultPad.setSelected(true);
441         leftColumn.add(defaultPad);
442
443         JRadioButton zeroPad = new JRadioButton("0");
444         zeroPad.setToolTipText("Uses no padding between the border and label.");
445         zeroPad.addItemListener(buttonPadListener);
446         zeroPad.setMnemonic('0');
447         group.add(zeroPad);
448         leftColumn.add(zeroPad);
449
450         JRadioButton tenPad = new JRadioButton("10");
451         tenPad.setMnemonic('1');
452         tenPad.setToolTipText("Uses a 10 pixel pad between the border and label.");
453         tenPad.addItemListener(buttonPadListener);
454         group.add(tenPad);
455         leftColumn.add(tenPad);
456
457         leftColumn.add(Box.createRigidArea(VGAP20));
458     return controls;
459     }
460     
461     public void createListeners() {
462     buttonDisplayListener = new ItemListener() {
463         Component c;
464         AbstractButton b;
465         
466         public void itemStateChanged(ItemEvent e) {
467             JCheckBox cb = (JCheckBox) e.getSource();
468             if(cb.getText().equals("Enabled")) {
469             for(int i = 0; i < currentControls.size(); i++) {
470                 c = (Component) currentControls.elementAt(i);
471                 c.setEnabled(cb.isSelected());
472                 c.invalidate();
473             }
474             } else if(cb.getText().equals("Paint Border")) {
475             c = (Component) currentControls.elementAt(0);
476             if(c instanceof AbstractButton) {
477                 for(int i = 0; i < currentControls.size(); i++) {
478                 b = (AbstractButton) currentControls.elementAt(i);
479                 b.setBorderPainted(cb.isSelected());
480                 b.invalidate();
481                 }
482             }
483             } else if(cb.getText().equals("Paint Focus")) {
484             c = (Component) currentControls.elementAt(0);
485             if(c instanceof AbstractButton) {
486                 for(int i = 0; i < currentControls.size(); i++) {
487                 b = (AbstractButton) currentControls.elementAt(i);
488                 b.setFocusPainted(cb.isSelected());
489                 b.invalidate();
490                 }
491             }
492             } else if(cb.getText().equals("Content Filled")) {
493             c = (Component) currentControls.elementAt(0);
494             if(c instanceof AbstractButton) {
495                 for(int i = 0; i < currentControls.size(); i++) {
496                 b = (AbstractButton) currentControls.elementAt(i);
497                 b.setContentAreaFilled(cb.isSelected());
498                 b.invalidate();
499                 }
500             }
501             }
502             invalidate();
503             validate();
504             repaint();
505         }
506     };
507
508     buttonPadListener = new ItemListener() {
509         Component c;
510         AbstractButton b;
511         
512         public void itemStateChanged(ItemEvent e) {
513             // *** pad = 0
514
int pad = -1;
515             JRadioButton rb = (JRadioButton) e.getSource();
516             if(rb.getText().equals("0") && rb.isSelected()) {
517             pad = 0;
518             } else if(rb.getText().equals("10") && rb.isSelected()) {
519             pad = 10;
520             }
521             
522             for(int i = 0; i < currentControls.size(); i++) {
523             b = (AbstractButton) currentControls.elementAt(i);
524             if(pad == -1) {
525                 b.setMargin(null);
526             } else if(pad == 0) {
527                 b.setMargin(insets0);
528             } else {
529                 b.setMargin(insets10);
530             }
531             }
532             invalidate();
533             validate();
534             repaint();
535         }
536     };
537     }
538     
539     public void stateChanged(ChangeEvent e) {
540     /*SingleSelectionModel model = (SingleSelectionModel) e.getSource();
541     if(model.getSelectedIndex() == 0) {
542         currentControls = buttons;
543     } else if(model.getSelectedIndex() == 1) {
544         currentControls = radiobuttons;
545     } else if(model.getSelectedIndex() == 2) {
546         currentControls = checkboxes;
547     } else {
548         currentControls = togglebuttons;
549     }*/

550     }
551
552     public Vector getCurrentControls() {
553     return currentControls;
554     }
555 }
556
Popular Tags