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 * JOptionPaneDemo 52 * 53 * @author Jeff Dinkins 54 */ 55 public class OptionPaneDemo extends DemoModule { 56 57 /** 58 * main method allows us to run as a standalone demo. 59 */ 60 public static void main(String[] args) { 61 OptionPaneDemo demo = new OptionPaneDemo(null); 62 demo.mainImpl(); 63 } 64 65 /** 66 * OptionPaneDemo Constructor 67 */ 68 public OptionPaneDemo(SwingSet2 swingset) { 69 // Set the title for this demo, and an icon used to represent this 70 // demo inside the SwingSet2 app. 71 super(swingset, "OptionPaneDemo", "toolbar/JOptionPane.gif"); 72 73 JPanel demo = getDemoPanel(); 74 75 demo.setLayout(new BoxLayout(demo, BoxLayout.X_AXIS)); 76 77 JPanel bp = new JPanel() { 78 public Dimension getMaximumSize() { 79 return new Dimension(getPreferredSize().width, super.getMaximumSize().height); 80 } 81 }; 82 bp.setLayout(new BoxLayout(bp, BoxLayout.Y_AXIS)); 83 84 bp.add(Box.createRigidArea(VGAP30)); 85 bp.add(Box.createRigidArea(VGAP30)); 86 87 bp.add(createInputDialogButton()); bp.add(Box.createRigidArea(VGAP15)); 88 bp.add(createWarningDialogButton()); bp.add(Box.createRigidArea(VGAP15)); 89 bp.add(createMessageDialogButton()); bp.add(Box.createRigidArea(VGAP15)); 90 bp.add(createComponentDialogButton()); bp.add(Box.createRigidArea(VGAP15)); 91 bp.add(createConfirmDialogButton()); bp.add(Box.createVerticalGlue()); 92 93 demo.add(Box.createHorizontalGlue()); 94 demo.add(bp); 95 demo.add(Box.createHorizontalGlue()); 96 } 97 98 public JButton createWarningDialogButton() { 99 Action a = new AbstractAction(getString("OptionPaneDemo.warningbutton")) { 100 public void actionPerformed(ActionEvent e) { 101 JOptionPane.showMessageDialog( 102 getDemoPanel(), 103 getString("OptionPaneDemo.warningtext"), 104 getString("OptionPaneDemo.warningtitle"), 105 JOptionPane.WARNING_MESSAGE 106 ); 107 } 108 }; 109 return createButton(a); 110 } 111 112 public JButton createMessageDialogButton() { 113 Action a = new AbstractAction(getString("OptionPaneDemo.messagebutton")) { 114 URL img = getClass().getResource("/resources/images/optionpane/bottle.gif"); 115 String imagesrc = "<img src=\"" + img + "\" width=\"284\" height=\"100\">"; 116 String message = getString("OptionPaneDemo.messagetext"); 117 public void actionPerformed(ActionEvent e) { 118 JOptionPane.showMessageDialog( 119 getDemoPanel(), 120 "<html>" + imagesrc + "<br><center>" + message + "</center><br></html>" 121 ); 122 } 123 }; 124 return createButton(a); 125 } 126 127 public JButton createConfirmDialogButton() { 128 Action a = new AbstractAction(getString("OptionPaneDemo.confirmbutton")) { 129 public void actionPerformed(ActionEvent e) { 130 int result = JOptionPane.showConfirmDialog(getDemoPanel(), getString("OptionPaneDemo.confirmquestion")); 131 if(result == JOptionPane.YES_OPTION) { 132 JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.confirmyes")); 133 } else if(result == JOptionPane.NO_OPTION) { 134 JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.confirmno")); 135 } 136 } 137 }; 138 return createButton(a); 139 } 140 141 public JButton createInputDialogButton() { 142 Action a = new AbstractAction(getString("OptionPaneDemo.inputbutton")) { 143 public void actionPerformed(ActionEvent e) { 144 String result = JOptionPane.showInputDialog(getDemoPanel(), getString("OptionPaneDemo.inputquestion")); 145 if ((result != null) && (result.length() > 0)) { 146 JOptionPane.showMessageDialog(getDemoPanel(), 147 result + ": " + 148 getString("OptionPaneDemo.inputresponse")); 149 } 150 } 151 }; 152 return createButton(a); 153 } 154 155 public JButton createComponentDialogButton() { 156 Action a = new AbstractAction(getString("OptionPaneDemo.componentbutton")) { 157 public void actionPerformed(ActionEvent e) { 158 // In a ComponentDialog, you can show as many message components and 159 // as many options as you want: 160 161 // Messages 162 Object[] message = new Object[4]; 163 message[0] = getString("OptionPaneDemo.componentmessage"); 164 message[1] = new JTextField(getString("OptionPaneDemo.componenttextfield")); 165 166 JComboBox cb = new JComboBox(); 167 cb.addItem(getString("OptionPaneDemo.component_cb1")); 168 cb.addItem(getString("OptionPaneDemo.component_cb2")); 169 cb.addItem(getString("OptionPaneDemo.component_cb3")); 170 message[2] = cb; 171 172 message[3] = getString("OptionPaneDemo.componentmessage2"); 173 174 // Options 175 String[] options = { 176 getString("OptionPaneDemo.component_op1"), 177 getString("OptionPaneDemo.component_op2"), 178 getString("OptionPaneDemo.component_op3"), 179 getString("OptionPaneDemo.component_op4"), 180 getString("OptionPaneDemo.component_op5") 181 }; 182 int result = JOptionPane.showOptionDialog( 183 getDemoPanel(), // the parent that the dialog blocks 184 message, // the dialog message array 185 getString("OptionPaneDemo.componenttitle"), // the title of the dialog window 186 JOptionPane.DEFAULT_OPTION, // option type 187 JOptionPane.INFORMATION_MESSAGE, // message type 188 null, // optional icon, use null to use the default icon 189 options, // options string array, will be made into buttons 190 options[3] // option that should be made into a default button 191 ); 192 switch(result) { 193 case 0: // yes 194 JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r1")); 195 break; 196 case 1: // no 197 JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r2")); 198 break; 199 case 2: // maybe 200 JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r3")); 201 break; 202 case 3: // probably 203 JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r4")); 204 break; 205 default: 206 break; 207 } 208 209 } 210 }; 211 return createButton(a); 212 } 213 214 public JButton createButton(Action a) { 215 JButton b = new JButton() { 216 public Dimension getMaximumSize() { 217 int width = Short.MAX_VALUE; 218 int height = super.getMaximumSize().height; 219 return new Dimension(width, height); 220 } 221 }; 222 // setting the following client property informs the button to show 223 // the action text as it's name. The default is to not show the 224 // action text. 225 b.putClientProperty("displayActionText", Boolean.TRUE); 226 b.setAction(a); 227 // b.setAlignmentX(JButton.CENTER_ALIGNMENT); 228 return b; 229 } 230 231 }