Path: blob/master/test/jdk/sanity/client/lib/SwingSet2/src/OptionPaneDemo.java
41161 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Dimension;24import java.awt.event.ActionEvent;25import java.net.URL;2627import javax.swing.AbstractAction;28import javax.swing.Action;29import javax.swing.Box;30import javax.swing.BoxLayout;31import javax.swing.JButton;32import javax.swing.JComboBox;33import javax.swing.JOptionPane;34import javax.swing.JPanel;35import javax.swing.JTextField;3637/**38* JOptionPaneDemo39*40* @author Jeff Dinkins41*/42public class OptionPaneDemo extends DemoModule {4344public static final String DEMO_NAME = "OptionPaneDemo";45public static final String INPUT_BUTTON = "Show Input Dialog";4647/**48* main method allows us to run as a standalone demo.49*/50public static void main(String[] args) {51OptionPaneDemo demo = new OptionPaneDemo(null);52demo.mainImpl();53}5455/**56* OptionPaneDemo Constructor57*/58public OptionPaneDemo(SwingSet2 swingset) {59// Set the title for this demo, and an icon used to represent this60// demo inside the SwingSet2 app.61super(swingset, DEMO_NAME, "toolbar/JOptionPane.gif");6263JPanel demo = getDemoPanel();6465demo.setLayout(new BoxLayout(demo, BoxLayout.X_AXIS));6667JPanel bp = new JPanel() {68public Dimension getMaximumSize() {69return new Dimension(getPreferredSize().width, super.getMaximumSize().height);70}71};72bp.setLayout(new BoxLayout(bp, BoxLayout.Y_AXIS));7374bp.add(Box.createRigidArea(VGAP30));75bp.add(Box.createRigidArea(VGAP30));7677bp.add(createInputDialogButton()); bp.add(Box.createRigidArea(VGAP15));78bp.add(createWarningDialogButton()); bp.add(Box.createRigidArea(VGAP15));79bp.add(createMessageDialogButton()); bp.add(Box.createRigidArea(VGAP15));80bp.add(createComponentDialogButton()); bp.add(Box.createRigidArea(VGAP15));81bp.add(createConfirmDialogButton()); bp.add(Box.createVerticalGlue());8283demo.add(Box.createHorizontalGlue());84demo.add(bp);85demo.add(Box.createHorizontalGlue());86}8788public JButton createWarningDialogButton() {89Action a = new AbstractAction(getString("OptionPaneDemo.warningbutton")) {90public void actionPerformed(ActionEvent e) {91JOptionPane.showMessageDialog(92getDemoPanel(),93getString("OptionPaneDemo.warningtext"),94getString("OptionPaneDemo.warningtitle"),95JOptionPane.WARNING_MESSAGE96);97}98};99return createButton(a);100}101102public JButton createMessageDialogButton() {103Action a = new AbstractAction(getString("OptionPaneDemo.messagebutton")) {104URL img = getClass().getResource("/resources/images/optionpane/bottle.gif");105String imagesrc = "<img src=\"" + img + "\" width=\"284\" height=\"100\">";106String message = getString("OptionPaneDemo.messagetext");107public void actionPerformed(ActionEvent e) {108JOptionPane.showMessageDialog(109getDemoPanel(),110"<html>" + imagesrc + "<br><center>" + message + "</center><br></html>"111);112}113};114return createButton(a);115}116117public JButton createConfirmDialogButton() {118Action a = new AbstractAction(getString("OptionPaneDemo.confirmbutton")) {119public void actionPerformed(ActionEvent e) {120int result = JOptionPane.showConfirmDialog(getDemoPanel(), getString("OptionPaneDemo.confirmquestion"));121if(result == JOptionPane.YES_OPTION) {122JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.confirmyes"));123} else if(result == JOptionPane.NO_OPTION) {124JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.confirmno"));125}126}127};128return createButton(a);129}130131public JButton createInputDialogButton() {132Action a = new AbstractAction(getString("OptionPaneDemo.inputbutton")) {133public void actionPerformed(ActionEvent e) {134String result = JOptionPane.showInputDialog(getDemoPanel(), getString("OptionPaneDemo.inputquestion"));135if ((result != null) && (result.length() > 0)) {136JOptionPane.showMessageDialog(getDemoPanel(),137result + ": " +138getString("OptionPaneDemo.inputresponse"));139}140}141};142return createButton(a);143}144145public JButton createComponentDialogButton() {146Action a = new AbstractAction(getString("OptionPaneDemo.componentbutton")) {147public void actionPerformed(ActionEvent e) {148// In a ComponentDialog, you can show as many message components and149// as many options as you want:150151// Messages152Object[] message = new Object[4];153message[0] = getString("OptionPaneDemo.componentmessage");154message[1] = new JTextField(getString("OptionPaneDemo.componenttextfield"));155156JComboBox cb = new JComboBox();157cb.addItem(getString("OptionPaneDemo.component_cb1"));158cb.addItem(getString("OptionPaneDemo.component_cb2"));159cb.addItem(getString("OptionPaneDemo.component_cb3"));160message[2] = cb;161162message[3] = getString("OptionPaneDemo.componentmessage2");163164// Options165String[] options = {166getString("OptionPaneDemo.component_op1"),167getString("OptionPaneDemo.component_op2"),168getString("OptionPaneDemo.component_op3"),169getString("OptionPaneDemo.component_op4"),170getString("OptionPaneDemo.component_op5")171};172int result = JOptionPane.showOptionDialog(173getDemoPanel(), // the parent that the dialog blocks174message, // the dialog message array175getString("OptionPaneDemo.componenttitle"), // the title of the dialog window176JOptionPane.DEFAULT_OPTION, // option type177JOptionPane.INFORMATION_MESSAGE, // message type178null, // optional icon, use null to use the default icon179options, // options string array, will be made into buttons180options[3] // option that should be made into a default button181);182switch(result) {183case 0: // yes184JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r1"));185break;186case 1: // no187JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r2"));188break;189case 2: // maybe190JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r3"));191break;192case 3: // probably193JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r4"));194break;195default:196break;197}198199}200};201return createButton(a);202}203204public JButton createButton(Action a) {205JButton b = new JButton() {206public Dimension getMaximumSize() {207int width = Short.MAX_VALUE;208int height = super.getMaximumSize().height;209return new Dimension(width, height);210}211};212// setting the following client property informs the button to show213// the action text as it's name. The default is to not show the214// action text.215b.putClientProperty("displayActionText", Boolean.TRUE);216b.setAction(a);217// b.setAlignmentX(JButton.CENTER_ALIGNMENT);218return b;219}220221}222223