1/* 2 * Copyright (c) 2007, 2016, 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 */ 23package com.sun.swingset3.demos.optionpane; 24 25import java.awt.*; 26import java.awt.event.ActionEvent; 27import java.net.URL; 28import javax.swing.*; 29 30import com.sun.swingset3.demos.ResourceManager; 31import com.sun.swingset3.DemoProperties; 32 33/** 34 * JOptionPaneDemo 35 * 36 * @author Jeff Dinkins 37 * @version 1.11 11/17/05 38 */ 39@DemoProperties( 40 value = "JOptionPane Demo", 41 category = "Choosers", 42 description = "Demonstrates JOptionPane, a component which displays standard message dialogs (question, warning, error, etc).", 43 sourceFiles = { 44 "com/sun/swingset3/demos/optionpane/OptionPaneDemo.java", 45 "com/sun/swingset3/demos/ResourceManager.java", 46 "com/sun/swingset3/demos/optionpane/resources/OptionPaneDemo.properties", 47 "com/sun/swingset3/demos/optionpane/resources/images/bottle.gif", 48 "com/sun/swingset3/demos/optionpane/resources/images/OptionPaneDemo.gif" 49 } 50) 51public class OptionPaneDemo extends JPanel { 52 53 private static final Dimension VGAP15 = new Dimension(1, 15); 54 private static final Dimension VGAP30 = new Dimension(1, 30); 55 56 private static final ResourceManager resourceManager = new ResourceManager(OptionPaneDemo.class); 57 public static final String WARNING_TITLE = resourceManager.getString("OptionPaneDemo.warningtitle"); 58 public static final String WARNING_TEXT = resourceManager.getString("OptionPaneDemo.warningtext"); 59 public static final String WARNING_BUTTON = resourceManager.getString("OptionPaneDemo.warningbutton"); 60 public static final String CONFIRM_NO = resourceManager.getString("OptionPaneDemo.confirmno"); 61 public static final String CONFIRM_YES = resourceManager.getString("OptionPaneDemo.confirmyes"); 62 public static final String CONFIRM_QUESTION = resourceManager.getString("OptionPaneDemo.confirmquestion"); 63 public static final String CONFIRM_BUTTON = resourceManager.getString("OptionPaneDemo.confirmbutton"); 64 public static final String MESSAGE_TEXT = resourceManager.getString("OptionPaneDemo.messagetext"); 65 public static final String MESSAGE_BUTTON = resourceManager.getString("OptionPaneDemo.messagebutton"); 66 public static final String INPUT_QUESTION = resourceManager.getString("OptionPaneDemo.inputquestion"); 67 public static final String INPUT_RESPONSE = ": " + resourceManager.getString("OptionPaneDemo.inputresponse"); 68 public static final String INPUT_BUTTON = resourceManager.getString("OptionPaneDemo.inputbutton"); 69 public static final String COMPONENT_R4 = resourceManager.getString("OptionPaneDemo.component_r4"); 70 public static final String COMPONENT_R3 = resourceManager.getString("OptionPaneDemo.component_r3"); 71 public static final String COMPONENT_R2 = resourceManager.getString("OptionPaneDemo.component_r2"); 72 public static final String COMPONENT_R1 = resourceManager.getString("OptionPaneDemo.component_r1"); 73 public static final String COMPONENT_TITLE = resourceManager.getString("OptionPaneDemo.componenttitle"); 74 public static final String COMPONENT_OP5 = resourceManager.getString("OptionPaneDemo.component_op5"); 75 public static final String COMPONENT_OP4 = resourceManager.getString("OptionPaneDemo.component_op4"); 76 public static final String COMPONENT_OP3 = resourceManager.getString("OptionPaneDemo.component_op3"); 77 public static final String COMPONENT_OP2 = resourceManager.getString("OptionPaneDemo.component_op2"); 78 public static final String COMPONENT_OP1 = resourceManager.getString("OptionPaneDemo.component_op1"); 79 public static final String COMPONENT_MESSAGE_2 = resourceManager.getString("OptionPaneDemo.componentmessage2"); 80 public static final String COMPONENT_CB3 = resourceManager.getString("OptionPaneDemo.component_cb3"); 81 public static final String COMPONENT_CB2 = resourceManager.getString("OptionPaneDemo.component_cb2"); 82 public static final String COMPONENT_CB1 = resourceManager.getString("OptionPaneDemo.component_cb1"); 83 public static final String COMPONENT_BUTTON = resourceManager.getString("OptionPaneDemo.componentbutton"); 84 public static final String COMPONENT_TEXT_FIELD = resourceManager.getString("OptionPaneDemo.componenttextfield"); 85 public static final String COMPONENT_MESSAGE = resourceManager.getString("OptionPaneDemo.componentmessage"); 86 public static final String DEMO_TITLE = OptionPaneDemo.class.getAnnotation(DemoProperties.class).value(); 87 88 /** 89 * main method allows us to run as a standalone demo. 90 * 91 * @param args 92 */ 93 public static void main(String[] args) { 94 JFrame frame = new JFrame(DEMO_TITLE); 95 96 frame.getContentPane().add(new OptionPaneDemo()); 97 frame.setPreferredSize(new Dimension(800, 600)); 98 frame.pack(); 99 frame.setLocationRelativeTo(null); 100 frame.setVisible(true); 101 } 102 103 /** 104 * OptionPaneDemo Constructor 105 */ 106 public OptionPaneDemo() { 107 setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); 108 109 JPanel bp = new JPanel() { 110 @Override 111 public Dimension getMaximumSize() { 112 return new Dimension(getPreferredSize().width, super.getMaximumSize().height); 113 } 114 }; 115 bp.setLayout(new BoxLayout(bp, BoxLayout.Y_AXIS)); 116 117 bp.add(Box.createRigidArea(VGAP30)); 118 bp.add(Box.createRigidArea(VGAP30)); 119 120 bp.add(createInputDialogButton()); 121 bp.add(Box.createRigidArea(VGAP15)); 122 bp.add(createWarningDialogButton()); 123 bp.add(Box.createRigidArea(VGAP15)); 124 bp.add(createMessageDialogButton()); 125 bp.add(Box.createRigidArea(VGAP15)); 126 bp.add(createComponentDialogButton()); 127 bp.add(Box.createRigidArea(VGAP15)); 128 bp.add(createConfirmDialogButton()); 129 bp.add(Box.createVerticalGlue()); 130 131 add(Box.createHorizontalGlue()); 132 add(bp); 133 add(Box.createHorizontalGlue()); 134 } 135 136 private JButton createWarningDialogButton() { 137 Action a = new AbstractAction(WARNING_BUTTON) { 138 @Override 139 public void actionPerformed(ActionEvent e) { 140 JOptionPane.showMessageDialog(OptionPaneDemo.this, 141 WARNING_TEXT, 142 WARNING_TITLE, 143 JOptionPane.WARNING_MESSAGE 144 ); 145 } 146 }; 147 return createButton(a); 148 } 149 150 private JButton createMessageDialogButton() { 151 Action a = new AbstractAction(MESSAGE_BUTTON) { 152 final URL img = getClass().getResource("resources/images/bottle.gif"); 153 final String imagesrc = "<img src=\"" + img + "\" width=\"284\" height=\"100\">"; 154 final String message = MESSAGE_TEXT; 155 156 @Override 157 public void actionPerformed(ActionEvent e) { 158 JOptionPane.showMessageDialog( 159 OptionPaneDemo.this, 160 "<html>" + imagesrc + "<br><center>" + message + "</center><br></html>" 161 ); 162 } 163 }; 164 return createButton(a); 165 } 166 167 private JButton createConfirmDialogButton() { 168 Action a = new AbstractAction(CONFIRM_BUTTON) { 169 @Override 170 public void actionPerformed(ActionEvent e) { 171 int result = JOptionPane.showConfirmDialog(OptionPaneDemo.this, CONFIRM_QUESTION); 172 if (result == JOptionPane.YES_OPTION) { 173 JOptionPane.showMessageDialog(OptionPaneDemo.this, CONFIRM_YES); 174 } else if (result == JOptionPane.NO_OPTION) { 175 JOptionPane.showMessageDialog(OptionPaneDemo.this, CONFIRM_NO); 176 } 177 } 178 }; 179 return createButton(a); 180 } 181 182 private JButton createInputDialogButton() { 183 Action a = new AbstractAction(INPUT_BUTTON) { 184 @Override 185 public void actionPerformed(ActionEvent e) { 186 String result = JOptionPane.showInputDialog(OptionPaneDemo.this, INPUT_QUESTION); 187 if ((result != null) && (result.length() > 0)) { 188 JOptionPane.showMessageDialog(OptionPaneDemo.this, 189 result + INPUT_RESPONSE); 190 } 191 } 192 }; 193 return createButton(a); 194 } 195 196 private JButton createComponentDialogButton() { 197 Action a = new AbstractAction(COMPONENT_BUTTON) { 198 @Override 199 public void actionPerformed(ActionEvent e) { 200 // In a ComponentDialog, you can show as many message components and 201 // as many options as you want: 202 203 // Messages 204 Object[] message = new Object[4]; 205 message[0] = COMPONENT_MESSAGE; 206 message[1] = new JTextField(COMPONENT_TEXT_FIELD); 207 208 JComboBox<String> cb = new JComboBox<>(); 209 cb.addItem(COMPONENT_CB1); 210 cb.addItem(COMPONENT_CB2); 211 cb.addItem(COMPONENT_CB3); 212 message[2] = cb; 213 214 message[3] = COMPONENT_MESSAGE_2; 215 216 // Options 217 String[] options = { 218 COMPONENT_OP1, COMPONENT_OP2, COMPONENT_OP3, COMPONENT_OP4, COMPONENT_OP5}; 219 int result = JOptionPane.showOptionDialog( 220 OptionPaneDemo.this, // the parent that the dialog blocks 221 message, // the dialog message array 222 COMPONENT_TITLE, // the title of the dialog window 223 JOptionPane.DEFAULT_OPTION, // option type 224 JOptionPane.INFORMATION_MESSAGE, // message type 225 null, // optional icon, use null to use the default icon 226 options, // options string array, will be made into buttons 227 options[3] // option that should be made into a default button 228 ); 229 switch (result) { 230 case 0: // yes 231 JOptionPane.showMessageDialog(OptionPaneDemo.this, COMPONENT_R1); 232 break; 233 case 1: // no 234 JOptionPane.showMessageDialog(OptionPaneDemo.this, COMPONENT_R2); 235 break; 236 case 2: // maybe 237 JOptionPane.showMessageDialog(OptionPaneDemo.this, COMPONENT_R3); 238 break; 239 case 3: // probably 240 JOptionPane.showMessageDialog(OptionPaneDemo.this, COMPONENT_R4); 241 break; 242 default: 243 break; 244 } 245 246 } 247 }; 248 return createButton(a); 249 } 250 251 private JButton createButton(Action a) { 252 JButton b = new JButton() { 253 @Override 254 public Dimension getMaximumSize() { 255 int width = Short.MAX_VALUE; 256 int height = super.getMaximumSize().height; 257 return new Dimension(width, height); 258 } 259 }; 260 // setting the following client property informs the button to show 261 // the action text as it's name. The default is to not show the 262 // action text. 263 b.putClientProperty("displayActionText", Boolean.TRUE); 264 b.setAction(a); 265 // b.setAlignmentX(JButton.CENTER_ALIGNMENT); 266 return b; 267 } 268 269} 270