Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/demo/share/jfc/SwingSet2/ButtonDemo.java
41152 views
1
/*
2
*
3
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
*
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
*
12
* - Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
*
16
* - Neither the name of Oracle nor the names of its
17
* contributors may be used to endorse or promote products derived
18
* from this software without specific prior written permission.
19
*
20
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
*/
32
33
34
import javax.swing.*;
35
import javax.swing.event.*;
36
import javax.swing.text.*;
37
import javax.swing.border.*;
38
import javax.swing.colorchooser.*;
39
import javax.swing.filechooser.*;
40
import javax.accessibility.*;
41
42
import java.awt.*;
43
import java.awt.event.*;
44
import java.beans.*;
45
import java.util.*;
46
import java.io.*;
47
import java.applet.*;
48
import java.net.*;
49
50
/**
51
* JButton, JRadioButton, JToggleButton, JCheckBox Demos
52
*
53
* @author Jeff Dinkins
54
*/
55
public class ButtonDemo extends DemoModule implements ChangeListener {
56
57
JTabbedPane tab;
58
59
JPanel buttonPanel = new JPanel();
60
JPanel checkboxPanel = new JPanel();
61
JPanel radioButtonPanel = new JPanel();
62
JPanel toggleButtonPanel = new JPanel();
63
64
Vector<Component> buttons = new Vector<>();
65
Vector<Component> checkboxes = new Vector<>();
66
Vector<Component> radiobuttons = new Vector<>();
67
Vector<Component> togglebuttons = new Vector<>();
68
69
Vector<Component> currentControls = buttons;
70
71
JButton button;
72
JCheckBox check;
73
JRadioButton radio;
74
JToggleButton toggle;
75
76
EmptyBorder border5 = new EmptyBorder(5,5,5,5);
77
EmptyBorder border10 = new EmptyBorder(10,10,10,10);
78
79
ItemListener buttonDisplayListener = null;
80
ItemListener buttonPadListener = null;
81
82
Insets insets0 = new Insets(0,0,0,0);
83
Insets insets10 = new Insets(10,10,10,10);
84
85
/**
86
* main method allows us to run as a standalone demo.
87
*/
88
public static void main(String[] args) {
89
ButtonDemo demo = new ButtonDemo(null);
90
demo.mainImpl();
91
}
92
93
/**
94
* ButtonDemo Constructor
95
*/
96
public ButtonDemo(SwingSet2 swingset) {
97
// Set the title for this demo, and an icon used to represent this
98
// demo inside the SwingSet2 app.
99
super(swingset, "ButtonDemo", "toolbar/JButton.gif");
100
101
tab = new JTabbedPane();
102
tab.getModel().addChangeListener(this);
103
104
JPanel demo = getDemoPanel();
105
demo.setLayout(new BoxLayout(demo, BoxLayout.Y_AXIS));
106
demo.add(tab);
107
108
addButtons();
109
addRadioButtons();
110
addCheckBoxes();
111
// addToggleButtons();
112
currentControls = buttons;
113
}
114
115
public void addButtons() {
116
tab.addTab(getString("ButtonDemo.buttons"), buttonPanel);
117
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
118
buttonPanel.setBorder(border5);
119
120
JPanel p1 = createVerticalPanel(true);
121
p1.setAlignmentY(TOP_ALIGNMENT);
122
buttonPanel.add(p1);
123
124
// Text Buttons
125
JPanel p2 = createHorizontalPanel(false);
126
p1.add(p2);
127
p2.setBorder(new CompoundBorder(new TitledBorder(null, getString("ButtonDemo.textbuttons"),
128
TitledBorder.LEFT, TitledBorder.TOP), border5));
129
130
buttons.add(p2.add(new JButton(getString("ButtonDemo.button1"))));
131
p2.add(Box.createRigidArea(HGAP10));
132
133
buttons.add(p2.add(new JButton(getString("ButtonDemo.button2"))));
134
p2.add(Box.createRigidArea(HGAP10));
135
136
buttons.add(p2.add(new JButton(getString("ButtonDemo.button3"))));
137
138
139
// Image Buttons
140
p1.add(Box.createRigidArea(VGAP30));
141
JPanel p3 = createHorizontalPanel(false);
142
p1.add(p3);
143
p3.setLayout(new BoxLayout(p3, BoxLayout.X_AXIS));
144
p3.setBorder(new TitledBorder(null, getString("ButtonDemo.imagebuttons"),
145
TitledBorder.LEFT, TitledBorder.TOP));
146
147
// home image button
148
String description = getString("ButtonDemo.phone");
149
button = new JButton(createImageIcon("buttons/b1.gif", description));
150
button.setPressedIcon(createImageIcon("buttons/b1p.gif", description));
151
button.setRolloverIcon(createImageIcon("buttons/b1r.gif", description));
152
button.setDisabledIcon(createImageIcon("buttons/b1d.gif", description));
153
button.setMargin(new Insets(0,0,0,0));
154
p3.add(button);
155
buttons.add(button);
156
p3.add(Box.createRigidArea(HGAP10));
157
158
// write image button
159
description = getString("ButtonDemo.write");
160
button = new JButton(createImageIcon("buttons/b2.gif", description));
161
button.setPressedIcon(createImageIcon("buttons/b2p.gif", description));
162
button.setRolloverIcon(createImageIcon("buttons/b2r.gif", description));
163
button.setDisabledIcon(createImageIcon("buttons/b2d.gif", description));
164
button.setMargin(new Insets(0,0,0,0));
165
p3.add(button);
166
buttons.add(button);
167
p3.add(Box.createRigidArea(HGAP10));
168
169
// write image button
170
description = getString("ButtonDemo.peace");
171
button = new JButton(createImageIcon("buttons/b3.gif", description));
172
button.setPressedIcon(createImageIcon("buttons/b3p.gif", description));
173
button.setRolloverIcon(createImageIcon("buttons/b3r.gif", description));
174
button.setDisabledIcon(createImageIcon("buttons/b3d.gif", description));
175
button.setMargin(new Insets(0,0,0,0));
176
p3.add(button);
177
buttons.add(button);
178
179
p1.add(Box.createVerticalGlue());
180
181
buttonPanel.add(Box.createHorizontalGlue());
182
currentControls = buttons;
183
buttonPanel.add(createControls());
184
}
185
186
public void addRadioButtons() {
187
ButtonGroup group = new ButtonGroup();
188
189
tab.addTab(getString("ButtonDemo.radiobuttons"), radioButtonPanel);
190
radioButtonPanel.setLayout(new BoxLayout(radioButtonPanel, BoxLayout.X_AXIS));
191
radioButtonPanel.setBorder(border5);
192
193
JPanel p1 = createVerticalPanel(true);
194
p1.setAlignmentY(TOP_ALIGNMENT);
195
radioButtonPanel.add(p1);
196
197
// Text Radio Buttons
198
JPanel p2 = createHorizontalPanel(false);
199
p1.add(p2);
200
p2.setBorder(new CompoundBorder(
201
new TitledBorder(
202
null, getString("ButtonDemo.textradiobuttons"),
203
TitledBorder.LEFT, TitledBorder.TOP), border5)
204
);
205
206
radio = (JRadioButton)p2.add(
207
new JRadioButton(getString("ButtonDemo.radio1")));
208
group.add(radio);
209
radiobuttons.add(radio);
210
p2.add(Box.createRigidArea(HGAP10));
211
212
radio = (JRadioButton)p2.add(
213
new JRadioButton(getString("ButtonDemo.radio2")));
214
group.add(radio);
215
radiobuttons.add(radio);
216
p2.add(Box.createRigidArea(HGAP10));
217
218
radio = (JRadioButton)p2.add(
219
new JRadioButton(getString("ButtonDemo.radio3")));
220
group.add(radio);
221
radiobuttons.add(radio);
222
223
// Image Radio Buttons
224
group = new ButtonGroup();
225
p1.add(Box.createRigidArea(VGAP30));
226
JPanel p3 = createHorizontalPanel(false);
227
p1.add(p3);
228
p3.setLayout(new BoxLayout(p3, BoxLayout.X_AXIS));
229
p3.setBorder(new TitledBorder(null, getString("ButtonDemo.imageradiobuttons"),
230
TitledBorder.LEFT, TitledBorder.TOP));
231
232
// image radio button 1
233
String description = getString("ButtonDemo.customradio");
234
String text = getString("ButtonDemo.radio1");
235
radio = new JRadioButton(text, createImageIcon("buttons/rb.gif", description));
236
radio.setPressedIcon(createImageIcon("buttons/rbp.gif", description));
237
radio.setRolloverIcon(createImageIcon("buttons/rbr.gif", description));
238
radio.setRolloverSelectedIcon(createImageIcon("buttons/rbrs.gif", description));
239
radio.setSelectedIcon(createImageIcon("buttons/rbs.gif", description));
240
radio.setMargin(new Insets(0,0,0,0));
241
group.add(radio);
242
p3.add(radio);
243
radiobuttons.add(radio);
244
p3.add(Box.createRigidArea(HGAP20));
245
246
// image radio button 2
247
text = getString("ButtonDemo.radio2");
248
radio = new JRadioButton(text, createImageIcon("buttons/rb.gif", description));
249
radio.setPressedIcon(createImageIcon("buttons/rbp.gif", description));
250
radio.setRolloverIcon(createImageIcon("buttons/rbr.gif", description));
251
radio.setRolloverSelectedIcon(createImageIcon("buttons/rbrs.gif", description));
252
radio.setSelectedIcon(createImageIcon("buttons/rbs.gif", description));
253
radio.setMargin(new Insets(0,0,0,0));
254
group.add(radio);
255
p3.add(radio);
256
radiobuttons.add(radio);
257
p3.add(Box.createRigidArea(HGAP20));
258
259
// image radio button 3
260
text = getString("ButtonDemo.radio3");
261
radio = new JRadioButton(text, createImageIcon("buttons/rb.gif", description));
262
radio.setPressedIcon(createImageIcon("buttons/rbp.gif", description));
263
radio.setRolloverIcon(createImageIcon("buttons/rbr.gif", description));
264
radio.setRolloverSelectedIcon(createImageIcon("buttons/rbrs.gif", description));
265
radio.setSelectedIcon(createImageIcon("buttons/rbs.gif", description));
266
radio.setMargin(new Insets(0,0,0,0));
267
group.add(radio);
268
radiobuttons.add(radio);
269
p3.add(radio);
270
271
// verticaly glue fills out the rest of the box
272
p1.add(Box.createVerticalGlue());
273
274
radioButtonPanel.add(Box.createHorizontalGlue());
275
currentControls = radiobuttons;
276
radioButtonPanel.add(createControls());
277
}
278
279
280
public void addCheckBoxes() {
281
tab.addTab(getString("ButtonDemo.checkboxes"), checkboxPanel);
282
checkboxPanel.setLayout(new BoxLayout(checkboxPanel, BoxLayout.X_AXIS));
283
checkboxPanel.setBorder(border5);
284
285
JPanel p1 = createVerticalPanel(true);
286
p1.setAlignmentY(TOP_ALIGNMENT);
287
checkboxPanel.add(p1);
288
289
// Text Radio Buttons
290
JPanel p2 = createHorizontalPanel(false);
291
p1.add(p2);
292
p2.setBorder(new CompoundBorder(
293
new TitledBorder(
294
null, getString("ButtonDemo.textcheckboxes"),
295
TitledBorder.LEFT, TitledBorder.TOP), border5)
296
);
297
298
checkboxes.add(p2.add(new JCheckBox(getString("ButtonDemo.check1"))));
299
p2.add(Box.createRigidArea(HGAP10));
300
301
checkboxes.add(p2.add(new JCheckBox(getString("ButtonDemo.check2"))));
302
p2.add(Box.createRigidArea(HGAP10));
303
304
checkboxes.add(p2.add(new JCheckBox(getString("ButtonDemo.check3"))));
305
306
// Image Radio Buttons
307
p1.add(Box.createRigidArea(VGAP30));
308
JPanel p3 = createHorizontalPanel(false);
309
p1.add(p3);
310
p3.setLayout(new BoxLayout(p3, BoxLayout.X_AXIS));
311
p3.setBorder(new TitledBorder(null, getString("ButtonDemo.imagecheckboxes"),
312
TitledBorder.LEFT, TitledBorder.TOP));
313
314
// image checkbox 1
315
String description = getString("ButtonDemo.customcheck");
316
String text = getString("ButtonDemo.check1");
317
check = new JCheckBox(text, createImageIcon("buttons/cb.gif", description));
318
check.setRolloverIcon(createImageIcon("buttons/cbr.gif", description));
319
check.setRolloverSelectedIcon(createImageIcon("buttons/cbrs.gif", description));
320
check.setSelectedIcon(createImageIcon("buttons/cbs.gif", description));
321
check.setMargin(new Insets(0,0,0,0));
322
p3.add(check);
323
checkboxes.add(check);
324
p3.add(Box.createRigidArea(HGAP20));
325
326
// image checkbox 2
327
text = getString("ButtonDemo.check2");
328
check = new JCheckBox(text, createImageIcon("buttons/cb.gif", description));
329
check.setRolloverIcon(createImageIcon("buttons/cbr.gif", description));
330
check.setRolloverSelectedIcon(createImageIcon("buttons/cbrs.gif", description));
331
check.setSelectedIcon(createImageIcon("buttons/cbs.gif", description));
332
check.setMargin(new Insets(0,0,0,0));
333
p3.add(check);
334
checkboxes.add(check);
335
p3.add(Box.createRigidArea(HGAP20));
336
337
// image checkbox 3
338
text = getString("ButtonDemo.check3");
339
check = new JCheckBox(text, createImageIcon("buttons/cb.gif", description));
340
check.setRolloverIcon(createImageIcon("buttons/cbr.gif", description));
341
check.setRolloverSelectedIcon(createImageIcon("buttons/cbrs.gif", description));
342
check.setSelectedIcon(createImageIcon("buttons/cbs.gif", description));
343
check.setMargin(new Insets(0,0,0,0));
344
p3.add(check);
345
checkboxes.add(check);
346
347
// verticaly glue fills out the rest of the box
348
p1.add(Box.createVerticalGlue());
349
350
checkboxPanel.add(Box.createHorizontalGlue());
351
currentControls = checkboxes;
352
checkboxPanel.add(createControls());
353
}
354
355
public void addToggleButtons() {
356
tab.addTab(getString("ButtonDemo.togglebuttons"), toggleButtonPanel);
357
}
358
359
public JPanel createControls() {
360
JPanel controls = new JPanel() {
361
public Dimension getMaximumSize() {
362
return new Dimension(300, super.getMaximumSize().height);
363
}
364
};
365
controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
366
controls.setAlignmentY(TOP_ALIGNMENT);
367
controls.setAlignmentX(LEFT_ALIGNMENT);
368
369
JPanel buttonControls = createHorizontalPanel(true);
370
buttonControls.setAlignmentY(TOP_ALIGNMENT);
371
buttonControls.setAlignmentX(LEFT_ALIGNMENT);
372
373
JPanel leftColumn = createVerticalPanel(false);
374
leftColumn.setAlignmentX(LEFT_ALIGNMENT);
375
leftColumn.setAlignmentY(TOP_ALIGNMENT);
376
377
JPanel rightColumn = new LayoutControlPanel(this);
378
379
buttonControls.add(leftColumn);
380
buttonControls.add(Box.createRigidArea(HGAP20));
381
buttonControls.add(rightColumn);
382
buttonControls.add(Box.createRigidArea(HGAP20));
383
384
controls.add(buttonControls);
385
386
createListeners();
387
388
// Display Options
389
JLabel l = new JLabel(getString("ButtonDemo.controlpanel_label"));
390
leftColumn.add(l);
391
392
JCheckBox bordered = new JCheckBox(getString("ButtonDemo.paintborder"));
393
bordered.setActionCommand("PaintBorder");
394
bordered.setToolTipText(getString("ButtonDemo.paintborder_tooltip"));
395
bordered.setMnemonic(getMnemonic("ButtonDemo.paintborder_mnemonic"));
396
if (currentControls == buttons) {
397
bordered.setSelected(true);
398
}
399
bordered.addItemListener(buttonDisplayListener);
400
leftColumn.add(bordered);
401
402
JCheckBox focused = new JCheckBox(getString("ButtonDemo.paintfocus"));
403
focused.setActionCommand("PaintFocus");
404
focused.setToolTipText(getString("ButtonDemo.paintfocus_tooltip"));
405
focused.setMnemonic(getMnemonic("ButtonDemo.paintfocus_mnemonic"));
406
focused.setSelected(true);
407
focused.addItemListener(buttonDisplayListener);
408
leftColumn.add(focused);
409
410
JCheckBox enabled = new JCheckBox(getString("ButtonDemo.enabled"));
411
enabled.setActionCommand("Enabled");
412
enabled.setToolTipText(getString("ButtonDemo.enabled_tooltip"));
413
enabled.setSelected(true);
414
enabled.addItemListener(buttonDisplayListener);
415
enabled.setMnemonic(getMnemonic("ButtonDemo.enabled_mnemonic"));
416
leftColumn.add(enabled);
417
418
JCheckBox filled = new JCheckBox(getString("ButtonDemo.contentfilled"));
419
filled.setActionCommand("ContentFilled");
420
filled.setToolTipText(getString("ButtonDemo.contentfilled_tooltip"));
421
filled.setSelected(true);
422
filled.addItemListener(buttonDisplayListener);
423
filled.setMnemonic(getMnemonic("ButtonDemo.contentfilled_mnemonic"));
424
leftColumn.add(filled);
425
426
leftColumn.add(Box.createRigidArea(VGAP20));
427
428
l = new JLabel(getString("ButtonDemo.padamount_label"));
429
leftColumn.add(l);
430
ButtonGroup group = new ButtonGroup();
431
JRadioButton defaultPad = new JRadioButton(getString("ButtonDemo.default"));
432
defaultPad.setToolTipText(getString("ButtonDemo.default_tooltip"));
433
defaultPad.setMnemonic(getMnemonic("ButtonDemo.default_mnemonic"));
434
defaultPad.addItemListener(buttonPadListener);
435
group.add(defaultPad);
436
defaultPad.setSelected(true);
437
leftColumn.add(defaultPad);
438
439
JRadioButton zeroPad = new JRadioButton(getString("ButtonDemo.zero"));
440
zeroPad.setActionCommand("ZeroPad");
441
zeroPad.setToolTipText(getString("ButtonDemo.zero_tooltip"));
442
zeroPad.addItemListener(buttonPadListener);
443
zeroPad.setMnemonic(getMnemonic("ButtonDemo.zero_mnemonic"));
444
group.add(zeroPad);
445
leftColumn.add(zeroPad);
446
447
JRadioButton tenPad = new JRadioButton(getString("ButtonDemo.ten"));
448
tenPad.setActionCommand("TenPad");
449
tenPad.setMnemonic(getMnemonic("ButtonDemo.ten_mnemonic"));
450
tenPad.setToolTipText(getString("ButtonDemo.ten_tooltip"));
451
tenPad.addItemListener(buttonPadListener);
452
group.add(tenPad);
453
leftColumn.add(tenPad);
454
455
leftColumn.add(Box.createRigidArea(VGAP20));
456
return controls;
457
}
458
459
public void createListeners() {
460
buttonDisplayListener = new ItemListener() {
461
Component c;
462
AbstractButton b;
463
464
public void itemStateChanged(ItemEvent e) {
465
JCheckBox cb = (JCheckBox) e.getSource();
466
String command = cb.getActionCommand();
467
if(command == "Enabled") {
468
for(int i = 0; i < currentControls.size(); i++) {
469
c = currentControls.elementAt(i);
470
c.setEnabled(cb.isSelected());
471
c.invalidate();
472
}
473
} else if(command == "PaintBorder") {
474
c = currentControls.elementAt(0);
475
if(c instanceof AbstractButton) {
476
for(int i = 0; i < currentControls.size(); i++) {
477
b = (AbstractButton) currentControls.elementAt(i);
478
b.setBorderPainted(cb.isSelected());
479
b.invalidate();
480
}
481
}
482
} else if(command == "PaintFocus") {
483
c = 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.setFocusPainted(cb.isSelected());
488
b.invalidate();
489
}
490
}
491
} else if(command == "ContentFilled") {
492
c = 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.setContentAreaFilled(cb.isSelected());
497
b.invalidate();
498
}
499
}
500
}
501
invalidate();
502
validate();
503
repaint();
504
}
505
};
506
507
buttonPadListener = new ItemListener() {
508
Component c;
509
AbstractButton b;
510
511
public void itemStateChanged(ItemEvent e) {
512
// *** pad = 0
513
int pad = -1;
514
JRadioButton rb = (JRadioButton) e.getSource();
515
String command = rb.getActionCommand();
516
if(command == "ZeroPad" && rb.isSelected()) {
517
pad = 0;
518
} else if(command == "TenPad" && 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<Component> getCurrentControls() {
553
return currentControls;
554
}
555
}
556
557