Path: blob/master/src/demo/share/jfc/SwingSet2/OptionPaneDemo.java
41149 views
/*1*2* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233import javax.swing.*;34import javax.swing.event.*;35import javax.swing.text.*;36import javax.swing.border.*;37import javax.swing.colorchooser.*;38import javax.swing.filechooser.*;39import javax.accessibility.*;4041import java.awt.*;42import java.awt.event.*;43import java.beans.*;44import java.util.*;45import java.io.*;46import java.applet.*;47import java.net.*;4849/**50* JOptionPaneDemo51*52* @author Jeff Dinkins53*/54public class OptionPaneDemo extends DemoModule {5556/**57* main method allows us to run as a standalone demo.58*/59public static void main(String[] args) {60OptionPaneDemo demo = new OptionPaneDemo(null);61demo.mainImpl();62}6364/**65* OptionPaneDemo Constructor66*/67public OptionPaneDemo(SwingSet2 swingset) {68// Set the title for this demo, and an icon used to represent this69// demo inside the SwingSet2 app.70super(swingset, "OptionPaneDemo", "toolbar/JOptionPane.gif");7172JPanel demo = getDemoPanel();7374demo.setLayout(new BoxLayout(demo, BoxLayout.X_AXIS));7576JPanel bp = new JPanel() {77public Dimension getMaximumSize() {78return new Dimension(getPreferredSize().width, super.getMaximumSize().height);79}80};81bp.setLayout(new BoxLayout(bp, BoxLayout.Y_AXIS));8283bp.add(Box.createRigidArea(VGAP30));84bp.add(Box.createRigidArea(VGAP30));8586bp.add(createInputDialogButton()); bp.add(Box.createRigidArea(VGAP15));87bp.add(createWarningDialogButton()); bp.add(Box.createRigidArea(VGAP15));88bp.add(createMessageDialogButton()); bp.add(Box.createRigidArea(VGAP15));89bp.add(createComponentDialogButton()); bp.add(Box.createRigidArea(VGAP15));90bp.add(createConfirmDialogButton()); bp.add(Box.createVerticalGlue());9192demo.add(Box.createHorizontalGlue());93demo.add(bp);94demo.add(Box.createHorizontalGlue());95}9697public JButton createWarningDialogButton() {98Action a = new AbstractAction(getString("OptionPaneDemo.warningbutton")) {99public void actionPerformed(ActionEvent e) {100JOptionPane.showMessageDialog(101getDemoPanel(),102getString("OptionPaneDemo.warningtext"),103getString("OptionPaneDemo.warningtitle"),104JOptionPane.WARNING_MESSAGE105);106}107};108return createButton(a);109}110111public JButton createMessageDialogButton() {112Action a = new AbstractAction(getString("OptionPaneDemo.messagebutton")) {113URL img = getClass().getResource("/resources/images/optionpane/bottle.gif");114String imagesrc = "<img src=\"" + img + "\" width=\"284\" height=\"100\">";115String message = getString("OptionPaneDemo.messagetext");116public void actionPerformed(ActionEvent e) {117JOptionPane.showMessageDialog(118getDemoPanel(),119"<html>" + imagesrc + "<br><center>" + message + "</center><br></html>"120);121}122};123return createButton(a);124}125126public JButton createConfirmDialogButton() {127Action a = new AbstractAction(getString("OptionPaneDemo.confirmbutton")) {128public void actionPerformed(ActionEvent e) {129int result = JOptionPane.showConfirmDialog(getDemoPanel(), getString("OptionPaneDemo.confirmquestion"));130if(result == JOptionPane.YES_OPTION) {131JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.confirmyes"));132} else if(result == JOptionPane.NO_OPTION) {133JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.confirmno"));134}135}136};137return createButton(a);138}139140public JButton createInputDialogButton() {141Action a = new AbstractAction(getString("OptionPaneDemo.inputbutton")) {142public void actionPerformed(ActionEvent e) {143String result = JOptionPane.showInputDialog(getDemoPanel(), getString("OptionPaneDemo.inputquestion"));144if ((result != null) && (result.length() > 0)) {145JOptionPane.showMessageDialog(getDemoPanel(),146result + ": " +147getString("OptionPaneDemo.inputresponse"));148}149}150};151return createButton(a);152}153154public JButton createComponentDialogButton() {155Action a = new AbstractAction(getString("OptionPaneDemo.componentbutton")) {156public void actionPerformed(ActionEvent e) {157// In a ComponentDialog, you can show as many message components and158// as many options as you want:159160// Messages161Object[] message = new Object[4];162message[0] = getString("OptionPaneDemo.componentmessage");163message[1] = new JTextField(getString("OptionPaneDemo.componenttextfield"));164165JComboBox<String> cb = new JComboBox<>();166cb.addItem(getString("OptionPaneDemo.component_cb1"));167cb.addItem(getString("OptionPaneDemo.component_cb2"));168cb.addItem(getString("OptionPaneDemo.component_cb3"));169message[2] = cb;170171message[3] = getString("OptionPaneDemo.componentmessage2");172173// Options174String[] options = {175getString("OptionPaneDemo.component_op1"),176getString("OptionPaneDemo.component_op2"),177getString("OptionPaneDemo.component_op3"),178getString("OptionPaneDemo.component_op4"),179getString("OptionPaneDemo.component_op5")180};181int result = JOptionPane.showOptionDialog(182getDemoPanel(), // the parent that the dialog blocks183message, // the dialog message array184getString("OptionPaneDemo.componenttitle"), // the title of the dialog window185JOptionPane.DEFAULT_OPTION, // option type186JOptionPane.INFORMATION_MESSAGE, // message type187null, // optional icon, use null to use the default icon188options, // options string array, will be made into buttons189options[3] // option that should be made into a default button190);191switch(result) {192case 0: // yes193JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r1"));194break;195case 1: // no196JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r2"));197break;198case 2: // maybe199JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r3"));200break;201case 3: // probably202JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r4"));203break;204default:205break;206}207208}209};210return createButton(a);211}212213public JButton createButton(Action a) {214JButton b = new JButton() {215public Dimension getMaximumSize() {216int width = Short.MAX_VALUE;217int height = super.getMaximumSize().height;218return new Dimension(width, height);219}220};221// setting the following client property informs the button to show222// the action text as it's name. The default is to not show the223// action text.224b.putClientProperty("displayActionText", Boolean.TRUE);225b.setAction(a);226// b.setAlignmentX(JButton.CENTER_ALIGNMENT);227return b;228}229230}231232233