1
36
37
40
41
42 import javax.swing.*;
43 import javax.swing.event.*;
44 import javax.swing.text.*;
45 import javax.swing.border.*;
46 import javax.swing.colorchooser.*;
47 import javax.swing.filechooser.*;
48 import javax.accessibility.*;
49
50 import java.awt.*;
51 import java.awt.event.*;
52 import java.beans.*;
53 import java.util.*;
54 import java.io.*;
55 import java.applet.*;
56 import java.net.*;
57
58
64 public class ButtonDemo extends DemoModule implements ChangeListener {
65
66 JTabbedPane tab;
67
68 JPanel buttonPanel = new JPanel();
69 JPanel checkboxPanel = new JPanel();
70 JPanel radioButtonPanel = new JPanel();
71 JPanel toggleButtonPanel = new JPanel();
72
73 Vector buttons = new Vector();
74 Vector checkboxes = new Vector();
75 Vector radiobuttons = new Vector();
76 Vector togglebuttons = new Vector();
77
78 Vector currentControls = buttons;
79
80 JButton button;
81 JCheckBox check;
82 JRadioButton radio;
83 JToggleButton toggle;
84
85 EmptyBorder border5 = new EmptyBorder(5,5,5,5);
86 EmptyBorder border10 = new EmptyBorder(10,10,10,10);
87
88 ItemListener buttonDisplayListener = null;
89 ItemListener buttonPadListener = null;
90
91 Insets insets0 = new Insets(0,0,0,0);
92 Insets insets10 = new Insets(10,10,10,10);
93
94
97 public static void main(String[] args) {
98 ButtonDemo demo = new ButtonDemo(null);
99 demo.mainImpl();
100 }
101
102
105 public ButtonDemo(SwingSet2 swingset) {
106 super(swingset, "ButtonDemo", "toolbar/JButton.gif");
109
110 tab = new JTabbedPane();
111 tab.getModel().addChangeListener(this);
112
113 JPanel demo = getDemoPanel();
114 demo.setLayout(new BoxLayout(demo, BoxLayout.Y_AXIS));
115 demo.add(tab);
116
117 addButtons();
118 addRadioButtons();
119 addCheckBoxes();
120 currentControls = buttons;
122 }
123
124 public void addButtons() {
125 tab.addTab(getString("ButtonDemo.buttons"), buttonPanel);
126 buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
127 buttonPanel.setBorder(border5);
128
129 JPanel p1 = createVerticalPanel(true);
130 p1.setAlignmentY(TOP_ALIGNMENT);
131 buttonPanel.add(p1);
132
133 JPanel p2 = createHorizontalPanel(false);
135 p1.add(p2);
136 p2.setBorder(new CompoundBorder(new TitledBorder(null, getString("ButtonDemo.textbuttons"),
137 TitledBorder.LEFT, TitledBorder.TOP), border5));
138
139 buttons.add(p2.add(new JButton(getString("ButtonDemo.button1"))));
140 p2.add(Box.createRigidArea(HGAP10));
141
142 buttons.add(p2.add(new JButton(getString("ButtonDemo.button2"))));
143 p2.add(Box.createRigidArea(HGAP10));
144
145 buttons.add(p2.add(new JButton(getString("ButtonDemo.button3"))));
146
147
148 p1.add(Box.createRigidArea(VGAP30));
150 JPanel p3 = createHorizontalPanel(false);
151 p1.add(p3);
152 p3.setLayout(new BoxLayout(p3, BoxLayout.X_AXIS));
153 p3.setBorder(new TitledBorder(null, getString("ButtonDemo.imagebuttons"),
154 TitledBorder.LEFT, TitledBorder.TOP));
155
156 String description = getString("ButtonDemo.phone");
158 button = new JButton(createImageIcon("buttons/b1.gif", description));
159 button.setPressedIcon(createImageIcon("buttons/b1p.gif", description));
160 button.setRolloverIcon(createImageIcon("buttons/b1r.gif", description));
161 button.setDisabledIcon(createImageIcon("buttons/b1d.gif", description));
162 button.setMargin(new Insets(0,0,0,0));
163 p3.add(button);
164 buttons.add(button);
165 p3.add(Box.createRigidArea(HGAP10));
166
167 description = getString("ButtonDemo.write");
169 button = new JButton(createImageIcon("buttons/b2.gif", description));
170 button.setPressedIcon(createImageIcon("buttons/b2p.gif", description));
171 button.setRolloverIcon(createImageIcon("buttons/b2r.gif", description));
172 button.setDisabledIcon(createImageIcon("buttons/b2d.gif", description));
173 button.setMargin(new Insets(0,0,0,0));
174 p3.add(button);
175 buttons.add(button);
176 p3.add(Box.createRigidArea(HGAP10));
177
178 description = getString("ButtonDemo.peace");
180 button = new JButton(createImageIcon("buttons/b3.gif", description));
181 button.setPressedIcon(createImageIcon("buttons/b3p.gif", description));
182 button.setRolloverIcon(createImageIcon("buttons/b3r.gif", description));
183 button.setDisabledIcon(createImageIcon("buttons/b3d.gif", description));
184 button.setMargin(new Insets(0,0,0,0));
185 p3.add(button);
186 buttons.add(button);
187
188 p1.add(Box.createVerticalGlue());
189
190 buttonPanel.add(Box.createHorizontalGlue());
191 currentControls = buttons;
192 buttonPanel.add(createControls());
193 }
194
195 public void addRadioButtons() {
196 ButtonGroup group = new ButtonGroup();
197
198 tab.addTab(getString("ButtonDemo.radiobuttons"), radioButtonPanel);
199 radioButtonPanel.setLayout(new BoxLayout(radioButtonPanel, BoxLayout.X_AXIS));
200 radioButtonPanel.setBorder(border5);
201
202 JPanel p1 = createVerticalPanel(true);
203 p1.setAlignmentY(TOP_ALIGNMENT);
204 radioButtonPanel.add(p1);
205
206 JPanel p2 = createHorizontalPanel(false);
208 p1.add(p2);
209 p2.setBorder(new CompoundBorder(
210 new TitledBorder(
211 null, getString("ButtonDemo.textradiobuttons"),
212 TitledBorder.LEFT, TitledBorder.TOP), border5)
213 );
214
215 radio = (JRadioButton)p2.add(
216 new JRadioButton(getString("ButtonDemo.radio1")));
217 group.add(radio);
218 radiobuttons.add(radio);
219 p2.add(Box.createRigidArea(HGAP10));
220
221 radio = (JRadioButton)p2.add(
222 new JRadioButton(getString("ButtonDemo.radio2")));
223 group.add(radio);
224 radiobuttons.add(radio);
225 p2.add(Box.createRigidArea(HGAP10));
226
227 radio = (JRadioButton)p2.add(
228 new JRadioButton(getString("ButtonDemo.radio3")));
229 group.add(radio);
230 radiobuttons.add(radio);
231
232 group = new ButtonGroup();
234 p1.add(Box.createRigidArea(VGAP30));
235 JPanel p3 = createHorizontalPanel(false);
236 p1.add(p3);
237 p3.setLayout(new BoxLayout(p3, BoxLayout.X_AXIS));
238 p3.setBorder(new TitledBorder(null, getString("ButtonDemo.imageradiobuttons"),
239 TitledBorder.LEFT, TitledBorder.TOP));
240
241 String description = getString("ButtonDemo.customradio");
243 String text = getString("ButtonDemo.radio1");
244 radio = new JRadioButton(text, createImageIcon("buttons/rb.gif", description));
245 radio.setPressedIcon(createImageIcon("buttons/rbp.gif", description));
246 radio.setRolloverIcon(createImageIcon("buttons/rbr.gif", description));
247 radio.setRolloverSelectedIcon(createImageIcon("buttons/rbrs.gif", description));
248 radio.setSelectedIcon(createImageIcon("buttons/rbs.gif", description));
249 radio.setMargin(new Insets(0,0,0,0));
250 group.add(radio);
251 p3.add(radio);
252 radiobuttons.add(radio);
253 p3.add(Box.createRigidArea(HGAP20));
254
255 text = getString("ButtonDemo.radio2");
257 radio = new JRadioButton(text, createImageIcon("buttons/rb.gif", description));
258 radio.setPressedIcon(createImageIcon("buttons/rbp.gif", description));
259 radio.setRolloverIcon(createImageIcon("buttons/rbr.gif", description));
260 radio.setRolloverSelectedIcon(createImageIcon("buttons/rbrs.gif", description));
261 radio.setSelectedIcon(createImageIcon("buttons/rbs.gif", description));
262 radio.setMargin(new Insets(0,0,0,0));
263 group.add(radio);
264 p3.add(radio);
265 radiobuttons.add(radio);
266 p3.add(Box.createRigidArea(HGAP20));
267
268 text = getString("ButtonDemo.radio3");
270 radio = new JRadioButton(text, createImageIcon("buttons/rb.gif", description));
271 radio.setPressedIcon(createImageIcon("buttons/rbp.gif", description));
272 radio.setRolloverIcon(createImageIcon("buttons/rbr.gif", description));
273 radio.setRolloverSelectedIcon(createImageIcon("buttons/rbrs.gif", description));
274 radio.setSelectedIcon(createImageIcon("buttons/rbs.gif", description));
275 radio.setMargin(new Insets(0,0,0,0));
276 group.add(radio);
277 radiobuttons.add(radio);
278 p3.add(radio);
279
280 p1.add(Box.createVerticalGlue());
282
283 radioButtonPanel.add(Box.createHorizontalGlue());
284 currentControls = radiobuttons;
285 radioButtonPanel.add(createControls());
286 }
287
288
289 public void addCheckBoxes() {
290 tab.addTab(getString("ButtonDemo.checkboxes"), checkboxPanel);
291 checkboxPanel.setLayout(new BoxLayout(checkboxPanel, BoxLayout.X_AXIS));
292 checkboxPanel.setBorder(border5);
293
294 JPanel p1 = createVerticalPanel(true);
295 p1.setAlignmentY(TOP_ALIGNMENT);
296 checkboxPanel.add(p1);
297
298 JPanel p2 = createHorizontalPanel(false);
300 p1.add(p2);
301 p2.setBorder(new CompoundBorder(
302 new TitledBorder(
303 null, getString("ButtonDemo.textcheckboxes"),
304 TitledBorder.LEFT, TitledBorder.TOP), border5)
305 );
306
307 checkboxes.add(p2.add(new JCheckBox(getString("ButtonDemo.check1"))));
308 p2.add(Box.createRigidArea(HGAP10));
309
310 checkboxes.add(p2.add(new JCheckBox(getString("ButtonDemo.check2"))));
311 p2.add(Box.createRigidArea(HGAP10));
312
313 checkboxes.add(p2.add(new JCheckBox(getString("ButtonDemo.check3"))));
314
315 p1.add(Box.createRigidArea(VGAP30));
317 JPanel p3 = createHorizontalPanel(false);
318 p1.add(p3);
319 p3.setLayout(new BoxLayout(p3, BoxLayout.X_AXIS));
320 p3.setBorder(new TitledBorder(null, getString("ButtonDemo.imagecheckboxes"),
321 TitledBorder.LEFT, TitledBorder.TOP));
322
323 String description = getString("ButtonDemo.customcheck");
325 String text = getString("ButtonDemo.check1");
326 check = new JCheckBox(text, createImageIcon("buttons/cb.gif", description));
327 check.setRolloverIcon(createImageIcon("buttons/cbr.gif", description));
328 check.setRolloverSelectedIcon(createImageIcon("buttons/cbrs.gif", description));
329 check.setSelectedIcon(createImageIcon("buttons/cbs.gif", description));
330 check.setMargin(new Insets(0,0,0,0));
331 p3.add(check);
332 checkboxes.add(check);
333 p3.add(Box.createRigidArea(HGAP20));
334
335 text = getString("ButtonDemo.check2");
337 check = new JCheckBox(text, createImageIcon("buttons/cb.gif", description));
338 check.setRolloverIcon(createImageIcon("buttons/cbr.gif", description));
339 check.setRolloverSelectedIcon(createImageIcon("buttons/cbrs.gif", description));
340 check.setSelectedIcon(createImageIcon("buttons/cbs.gif", description));
341 check.setMargin(new Insets(0,0,0,0));
342 p3.add(check);
343 checkboxes.add(check);
344 p3.add(Box.createRigidArea(HGAP20));
345
346 text = getString("ButtonDemo.check3");
348 check = new JCheckBox(text, createImageIcon("buttons/cb.gif", description));
349 check.setRolloverIcon(createImageIcon("buttons/cbr.gif", description));
350 check.setRolloverSelectedIcon(createImageIcon("buttons/cbrs.gif", description));
351 check.setSelectedIcon(createImageIcon("buttons/cbs.gif", description));
352 check.setMargin(new Insets(0,0,0,0));
353 p3.add(check);
354 checkboxes.add(check);
355
356 p1.add(Box.createVerticalGlue());
358
359 checkboxPanel.add(Box.createHorizontalGlue());
360 currentControls = checkboxes;
361 checkboxPanel.add(createControls());
362 }
363
364 public void addToggleButtons() {
365 tab.addTab(getString("ButtonDemo.togglebuttons"), toggleButtonPanel);
366 }
367
368 public JPanel createControls() {
369 JPanel controls = new JPanel() {
370 public Dimension getMaximumSize() {
371 return new Dimension(300, super.getMaximumSize().height);
372 }
373 };
374 controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
375 controls.setAlignmentY(TOP_ALIGNMENT);
376 controls.setAlignmentX(LEFT_ALIGNMENT);
377
378 JPanel buttonControls = createHorizontalPanel(true);
379 buttonControls.setAlignmentY(TOP_ALIGNMENT);
380 buttonControls.setAlignmentX(LEFT_ALIGNMENT);
381
382 JPanel leftColumn = createVerticalPanel(false);
383 leftColumn.setAlignmentX(LEFT_ALIGNMENT);
384 leftColumn.setAlignmentY(TOP_ALIGNMENT);
385
386 JPanel rightColumn = new LayoutControlPanel(this);
387
388 buttonControls.add(leftColumn);
389 buttonControls.add(Box.createRigidArea(HGAP20));
390 buttonControls.add(rightColumn);
391 buttonControls.add(Box.createRigidArea(HGAP20));
392
393 controls.add(buttonControls);
394
395 createListeners();
396
397 JLabel l = new JLabel(getString("ButtonDemo.controlpanel_label"));
399 leftColumn.add(l);
400
401 JCheckBox bordered = new JCheckBox(getString("ButtonDemo.paintborder"));
402 bordered.setActionCommand("PaintBorder");
403 bordered.setToolTipText(getString("ButtonDemo.paintborder_tooltip"));
404 bordered.setMnemonic(getMnemonic("ButtonDemo.paintborder_mnemonic"));
405 if (currentControls == buttons) {
406 bordered.setSelected(true);
407 }
408 bordered.addItemListener(buttonDisplayListener);
409 leftColumn.add(bordered);
410
411 JCheckBox focused = new JCheckBox(getString("ButtonDemo.paintfocus"));
412 focused.setActionCommand("PaintFocus");
413 focused.setToolTipText(getString("ButtonDemo.paintfocus_tooltip"));
414 focused.setMnemonic(getMnemonic("ButtonDemo.paintfocus_mnemonic"));
415 focused.setSelected(true);
416 focused.addItemListener(buttonDisplayListener);
417 leftColumn.add(focused);
418
419 JCheckBox enabled = new JCheckBox(getString("ButtonDemo.enabled"));
420 enabled.setActionCommand("Enabled");
421 enabled.setToolTipText(getString("ButtonDemo.enabled_tooltip"));
422 enabled.setSelected(true);
423 enabled.addItemListener(buttonDisplayListener);
424 enabled.setMnemonic(getMnemonic("ButtonDemo.enabled_mnemonic"));
425 leftColumn.add(enabled);
426
427 JCheckBox filled = new JCheckBox(getString("ButtonDemo.contentfilled"));
428 filled.setActionCommand("ContentFilled");
429 filled.setToolTipText(getString("ButtonDemo.contentfilled_tooltip"));
430 filled.setSelected(true);
431 filled.addItemListener(buttonDisplayListener);
432 filled.setMnemonic(getMnemonic("ButtonDemo.contentfilled_mnemonic"));
433 leftColumn.add(filled);
434
435 leftColumn.add(Box.createRigidArea(VGAP20));
436
437 l = new JLabel(getString("ButtonDemo.padamount_label"));
438 leftColumn.add(l);
439 ButtonGroup group = new ButtonGroup();
440 JRadioButton defaultPad = new JRadioButton(getString("ButtonDemo.default"));
441 defaultPad.setToolTipText(getString("ButtonDemo.default_tooltip"));
442 defaultPad.setMnemonic(getMnemonic("ButtonDemo.default_mnemonic"));
443 defaultPad.addItemListener(buttonPadListener);
444 group.add(defaultPad);
445 defaultPad.setSelected(true);
446 leftColumn.add(defaultPad);
447
448 JRadioButton zeroPad = new JRadioButton(getString("ButtonDemo.zero"));
449 zeroPad.setActionCommand("ZeroPad");
450 zeroPad.setToolTipText(getString("ButtonDemo.zero_tooltip"));
451 zeroPad.addItemListener(buttonPadListener);
452 zeroPad.setMnemonic(getMnemonic("ButtonDemo.zero_mnemonic"));
453 group.add(zeroPad);
454 leftColumn.add(zeroPad);
455
456 JRadioButton tenPad = new JRadioButton(getString("ButtonDemo.ten"));
457 tenPad.setActionCommand("TenPad");
458 tenPad.setMnemonic(getMnemonic("ButtonDemo.ten_mnemonic"));
459 tenPad.setToolTipText(getString("ButtonDemo.ten_tooltip"));
460 tenPad.addItemListener(buttonPadListener);
461 group.add(tenPad);
462 leftColumn.add(tenPad);
463
464 leftColumn.add(Box.createRigidArea(VGAP20));
465 return controls;
466 }
467
468 public void createListeners() {
469 buttonDisplayListener = new ItemListener() {
470 Component c;
471 AbstractButton b;
472
473 public void itemStateChanged(ItemEvent e) {
474 JCheckBox cb = (JCheckBox) e.getSource();
475 String command = cb.getActionCommand();
476 if(command == "Enabled") {
477 for(int i = 0; i < currentControls.size(); i++) {
478 c = (Component) currentControls.elementAt(i);
479 c.setEnabled(cb.isSelected());
480 c.invalidate();
481 }
482 } else if(command == "PaintBorder") {
483 c = (Component) currentControls.elementAt(0);
484 if(c instanceof AbstractButton) {
485 for(int i = 0; i < currentControls.size(); i++) {
486 b = (AbstractButton) currentControls.elementAt(i);
487 b.setBorderPainted(cb.isSelected());
488 b.invalidate();
489 }
490 }
491 } else if(command == "PaintFocus") {
492 c = (Component) currentControls.elementAt(0);
493 if(c instanceof AbstractButton) {
494 for(int i = 0; i < currentControls.size(); i++) {
495 b = (AbstractButton) currentControls.elementAt(i);
496 b.setFocusPainted(cb.isSelected());
497 b.invalidate();
498 }
499 }
500 } else if(command == "ContentFilled") {
501 c = (Component) currentControls.elementAt(0);
502 if(c instanceof AbstractButton) {
503 for(int i = 0; i < currentControls.size(); i++) {
504 b = (AbstractButton) currentControls.elementAt(i);
505 b.setContentAreaFilled(cb.isSelected());
506 b.invalidate();
507 }
508 }
509 }
510 invalidate();
511 validate();
512 repaint();
513 }
514 };
515
516 buttonPadListener = new ItemListener() {
517 Component c;
518 AbstractButton b;
519
520 public void itemStateChanged(ItemEvent e) {
521 int pad = -1;
523 JRadioButton rb = (JRadioButton) e.getSource();
524 String command = rb.getActionCommand();
525 if(command == "ZeroPad" && rb.isSelected()) {
526 pad = 0;
527 } else if(command == "TenPad" && rb.isSelected()) {
528 pad = 10;
529 }
530
531 for(int i = 0; i < currentControls.size(); i++) {
532 b = (AbstractButton) currentControls.elementAt(i);
533 if(pad == -1) {
534 b.setMargin(null);
535 } else if(pad == 0) {
536 b.setMargin(insets0);
537 } else {
538 b.setMargin(insets10);
539 }
540 }
541 invalidate();
542 validate();
543 repaint();
544 }
545 };
546 }
547
548 public void stateChanged(ChangeEvent e) {
549 SingleSelectionModel model = (SingleSelectionModel) e.getSource();
550 if(model.getSelectedIndex() == 0) {
551 currentControls = buttons;
552 } else if(model.getSelectedIndex() == 1) {
553 currentControls = radiobuttons;
554 } else if(model.getSelectedIndex() == 2) {
555 currentControls = checkboxes;
556 } else {
557 currentControls = togglebuttons;
558 }
559 }
560
561 public Vector getCurrentControls() {
562 return currentControls;
563 }
564 }
565