Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sanity/client/lib/SwingSet2/src/OptionPaneDemo.java
41161 views
1
/*
2
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.awt.Dimension;
25
import java.awt.event.ActionEvent;
26
import java.net.URL;
27
28
import javax.swing.AbstractAction;
29
import javax.swing.Action;
30
import javax.swing.Box;
31
import javax.swing.BoxLayout;
32
import javax.swing.JButton;
33
import javax.swing.JComboBox;
34
import javax.swing.JOptionPane;
35
import javax.swing.JPanel;
36
import javax.swing.JTextField;
37
38
/**
39
* JOptionPaneDemo
40
*
41
* @author Jeff Dinkins
42
*/
43
public class OptionPaneDemo extends DemoModule {
44
45
public static final String DEMO_NAME = "OptionPaneDemo";
46
public static final String INPUT_BUTTON = "Show Input Dialog";
47
48
/**
49
* main method allows us to run as a standalone demo.
50
*/
51
public static void main(String[] args) {
52
OptionPaneDemo demo = new OptionPaneDemo(null);
53
demo.mainImpl();
54
}
55
56
/**
57
* OptionPaneDemo Constructor
58
*/
59
public OptionPaneDemo(SwingSet2 swingset) {
60
// Set the title for this demo, and an icon used to represent this
61
// demo inside the SwingSet2 app.
62
super(swingset, DEMO_NAME, "toolbar/JOptionPane.gif");
63
64
JPanel demo = getDemoPanel();
65
66
demo.setLayout(new BoxLayout(demo, BoxLayout.X_AXIS));
67
68
JPanel bp = new JPanel() {
69
public Dimension getMaximumSize() {
70
return new Dimension(getPreferredSize().width, super.getMaximumSize().height);
71
}
72
};
73
bp.setLayout(new BoxLayout(bp, BoxLayout.Y_AXIS));
74
75
bp.add(Box.createRigidArea(VGAP30));
76
bp.add(Box.createRigidArea(VGAP30));
77
78
bp.add(createInputDialogButton()); bp.add(Box.createRigidArea(VGAP15));
79
bp.add(createWarningDialogButton()); bp.add(Box.createRigidArea(VGAP15));
80
bp.add(createMessageDialogButton()); bp.add(Box.createRigidArea(VGAP15));
81
bp.add(createComponentDialogButton()); bp.add(Box.createRigidArea(VGAP15));
82
bp.add(createConfirmDialogButton()); bp.add(Box.createVerticalGlue());
83
84
demo.add(Box.createHorizontalGlue());
85
demo.add(bp);
86
demo.add(Box.createHorizontalGlue());
87
}
88
89
public JButton createWarningDialogButton() {
90
Action a = new AbstractAction(getString("OptionPaneDemo.warningbutton")) {
91
public void actionPerformed(ActionEvent e) {
92
JOptionPane.showMessageDialog(
93
getDemoPanel(),
94
getString("OptionPaneDemo.warningtext"),
95
getString("OptionPaneDemo.warningtitle"),
96
JOptionPane.WARNING_MESSAGE
97
);
98
}
99
};
100
return createButton(a);
101
}
102
103
public JButton createMessageDialogButton() {
104
Action a = new AbstractAction(getString("OptionPaneDemo.messagebutton")) {
105
URL img = getClass().getResource("/resources/images/optionpane/bottle.gif");
106
String imagesrc = "<img src=\"" + img + "\" width=\"284\" height=\"100\">";
107
String message = getString("OptionPaneDemo.messagetext");
108
public void actionPerformed(ActionEvent e) {
109
JOptionPane.showMessageDialog(
110
getDemoPanel(),
111
"<html>" + imagesrc + "<br><center>" + message + "</center><br></html>"
112
);
113
}
114
};
115
return createButton(a);
116
}
117
118
public JButton createConfirmDialogButton() {
119
Action a = new AbstractAction(getString("OptionPaneDemo.confirmbutton")) {
120
public void actionPerformed(ActionEvent e) {
121
int result = JOptionPane.showConfirmDialog(getDemoPanel(), getString("OptionPaneDemo.confirmquestion"));
122
if(result == JOptionPane.YES_OPTION) {
123
JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.confirmyes"));
124
} else if(result == JOptionPane.NO_OPTION) {
125
JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.confirmno"));
126
}
127
}
128
};
129
return createButton(a);
130
}
131
132
public JButton createInputDialogButton() {
133
Action a = new AbstractAction(getString("OptionPaneDemo.inputbutton")) {
134
public void actionPerformed(ActionEvent e) {
135
String result = JOptionPane.showInputDialog(getDemoPanel(), getString("OptionPaneDemo.inputquestion"));
136
if ((result != null) && (result.length() > 0)) {
137
JOptionPane.showMessageDialog(getDemoPanel(),
138
result + ": " +
139
getString("OptionPaneDemo.inputresponse"));
140
}
141
}
142
};
143
return createButton(a);
144
}
145
146
public JButton createComponentDialogButton() {
147
Action a = new AbstractAction(getString("OptionPaneDemo.componentbutton")) {
148
public void actionPerformed(ActionEvent e) {
149
// In a ComponentDialog, you can show as many message components and
150
// as many options as you want:
151
152
// Messages
153
Object[] message = new Object[4];
154
message[0] = getString("OptionPaneDemo.componentmessage");
155
message[1] = new JTextField(getString("OptionPaneDemo.componenttextfield"));
156
157
JComboBox cb = new JComboBox();
158
cb.addItem(getString("OptionPaneDemo.component_cb1"));
159
cb.addItem(getString("OptionPaneDemo.component_cb2"));
160
cb.addItem(getString("OptionPaneDemo.component_cb3"));
161
message[2] = cb;
162
163
message[3] = getString("OptionPaneDemo.componentmessage2");
164
165
// Options
166
String[] options = {
167
getString("OptionPaneDemo.component_op1"),
168
getString("OptionPaneDemo.component_op2"),
169
getString("OptionPaneDemo.component_op3"),
170
getString("OptionPaneDemo.component_op4"),
171
getString("OptionPaneDemo.component_op5")
172
};
173
int result = JOptionPane.showOptionDialog(
174
getDemoPanel(), // the parent that the dialog blocks
175
message, // the dialog message array
176
getString("OptionPaneDemo.componenttitle"), // the title of the dialog window
177
JOptionPane.DEFAULT_OPTION, // option type
178
JOptionPane.INFORMATION_MESSAGE, // message type
179
null, // optional icon, use null to use the default icon
180
options, // options string array, will be made into buttons
181
options[3] // option that should be made into a default button
182
);
183
switch(result) {
184
case 0: // yes
185
JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r1"));
186
break;
187
case 1: // no
188
JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r2"));
189
break;
190
case 2: // maybe
191
JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r3"));
192
break;
193
case 3: // probably
194
JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r4"));
195
break;
196
default:
197
break;
198
}
199
200
}
201
};
202
return createButton(a);
203
}
204
205
public JButton createButton(Action a) {
206
JButton b = new JButton() {
207
public Dimension getMaximumSize() {
208
int width = Short.MAX_VALUE;
209
int height = super.getMaximumSize().height;
210
return new Dimension(width, height);
211
}
212
};
213
// setting the following client property informs the button to show
214
// the action text as it's name. The default is to not show the
215
// action text.
216
b.putClientProperty("displayActionText", Boolean.TRUE);
217
b.setAction(a);
218
// b.setAlignmentX(JButton.CENTER_ALIGNMENT);
219
return b;
220
}
221
222
}
223