Example usage for javax.swing JOptionPane JOptionPane

List of usage examples for javax.swing JOptionPane JOptionPane

Introduction

In this page you can find the example usage for javax.swing JOptionPane JOptionPane.

Prototype

public JOptionPane() 

Source Link

Document

Creates a JOptionPane with a test message.

Usage

From source file:MainClass.java

public static void main(String[] a) {
    JOptionPane optionPane = new JOptionPane();
    optionPane.setMessage("Set Message");
    optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
    optionPane.setOptions(new Object[] { new JButton("Button") });
    JDialog dialog = optionPane.createDialog(null, "Icon/Text Button");
    dialog.setVisible(true);/*w  w  w .  j a  v  a 2s  .c o  m*/
}

From source file:GettingJOptionPaneSelectionDemo.java

public static void main(String[] a) {

    String multiLineMsg[] = { "Hello,", "World" };
    JOptionPane pane = new JOptionPane();
    pane.setMessage(multiLineMsg);//from ww  w  . j  a va 2 s  .c om
    JDialog d = pane.createDialog(null, "title");
    d.setVisible(true);
    int selection = getSelection(pane);

    switch (selection) {
    case JOptionPane.OK_OPTION:
        System.out.println("OK_OPTION");
        break;
    case JOptionPane.CANCEL_OPTION:
        System.out.println("CANCEL");
        break;
    default:
        System.out.println("Others");
    }

}

From source file:JSliderOnJOptionPane.java

public static void main(final String[] args) {
    JFrame parent = new JFrame();

    JOptionPane optionPane = new JOptionPane();
    JSlider slider = getSlider(optionPane);
    optionPane.setMessage(new Object[] { "Select a value: ", slider });
    optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE);
    optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
    JDialog dialog = optionPane.createDialog(parent, "My Slider");
    dialog.setVisible(true);//  w  ww  .j a  va 2 s .co m
    System.out.println("Input: " + optionPane.getInputValue());
}

From source file:MainClass.java

public static void main(String[] a) {

    String msg = "<html>this is a really long message<br>this is a really long message this is a really long message this is a really long message this is a really long message this is a really long message this is a really long message";

    JOptionPane optionPane = new JOptionPane();
    optionPane.setMessage(msg);/*from ww w .ja v a2 s  . c  om*/
    optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
    JDialog dialog = optionPane.createDialog(null, "Width 100");
    dialog.setVisible(true);
}

From source file:AddingButtonWithActionListener.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    JOptionPane optionPane = new JOptionPane();
    optionPane.setMessage("I got an icon and a text label");
    optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
    Icon icon = new ImageIcon("yourFile.gif");
    JButton jButton = getButton(optionPane, "OK", icon);
    optionPane.setOptions(new Object[] { jButton });
    JDialog dialog = optionPane.createDialog(frame, "Icon/Text Button");
    dialog.setVisible(true);/*  w  ww  . ja  va  2  s. c o m*/

}

From source file:ComplexMessageDemo.java

public static void main(String[] a) {

    Object complexMsg[] = { "Above Message", new ImageIcon("yourFile.gif"), new JButton("Hello"), new JSlider(),
            new ImageIcon("yourFile.gif"), "Below Message" };

    JOptionPane optionPane = new JOptionPane();
    optionPane.setMessage(complexMsg);// w ww . ja  v a2 s . c o  m
    optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
    JDialog dialog = optionPane.createDialog(null, "Width 100");
    dialog.setVisible(true);
}

From source file:MessagePopup.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Message Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Pop it");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();

            /*/*from   ww  w .  j  a v a 2  s.  c  om*/
             * // String msg = "this is a really long message this is a
             * really long message this is a really long message this is a
             * really long message this is a really long message this is a
             * really long message this is a really long message"; String
             * msg = " <html>this is a really long message <br> this is a
             * really long message this is a really long message this is a
             * really long message this is a really long message this is a
             * really long message this is a really long message";
             * JOptionPane.showMessageDialog(source, msg); JOptionPane
             * optionPane = OptionPaneUtils.getNarrowOptionPane(72);
             * optionPane.setMessage(msg);
             * optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
             * JDialog dialog = optionPane.createDialog(source, "Width 72");
             * dialog.show(); int selection =
             * OptionPaneUtils.getSelection(optionPane);
             * JOptionPane.showMessageDialog(source, msg); String
             * multiLineMsg[] = {"Hello", "World"};
             * JOptionPane.showMessageDialog(source, multiLineMsg);
             * 
             * Object complexMsg[] = {"Above Message", new
             * DiamondIcon(Color.red), new JButton ("Hello"), new JSlider(),
             * new DiamondIcon(Color.blue), "Below Message"};
             * JOptionPane.showInputDialog(source, complexMsg); JOptionPane
             * optionPane = new JOptionPane(); JSlider slider =
             * OptionPaneUtils.getSlider(optionPane);
             * optionPane.setMessage(new Object[] {"Select a value: " ,
             * slider});
             * optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE);
             * optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
             * JDialog dialog = optionPane.createDialog(source, "My
             * Slider"); dialog.show(); System.out.println ("Input: " +
             * optionPane.getInputValue());
             */
            JOptionPane optionPane = new JOptionPane();
            optionPane.setMessage("I got an icon and a text label");
            optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
            Icon icon = new DiamondIcon(Color.blue);
            JButton jButton = OptionPaneUtils.getButton(optionPane, "OK", icon);
            optionPane.setOptions(new Object[] { jButton });
            JDialog dialog = optionPane.createDialog(source, "Icon/Text Button");
            dialog.show();
        }
    };
    button.addActionListener(actionListener);
    Container contentPane = frame.getContentPane();
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static JOptionPane createMaxLengthOptionPane(final int maxLength) {
    return new JOptionPane() {
        @Override/*from  w  ww .ja  va2s .c  o  m*/
        public int getMaxCharactersPerLineCount() {
            return maxLength;
        }
    };
}

From source file:Main.java

/** Create a JOptionPane instance that word-wraps its message. */
public static JOptionPane makeWordWrapJOptionPane() {
    // The basic problem is described in this bug report:
    ////  w w  w  . j a  v a2 s  . c  o m
    //   http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4104906
    //
    // The workaround there requires adding a scrollbar to the
    // message, which I do not want to do.

    // I tried these solutions, but they do not work (anymore?):
    //
    //   http://stackoverflow.com/questions/4330076/joptionpane-showmessagedialog-truncates-jtextarea-message
    //   http://www.coderanch.com/t/339970/GUI/java/wrap-large-message-JOptionPane-showConfirmDialog
    //
    // Most other solutions involve manually inserting newlines.

    // Thankfully, this one actually does work:
    //
    //   http://www.jroller.com/Fester/entry/joptionpane_with_word_wrapping

    @SuppressWarnings("serial")
    JOptionPane pane = new JOptionPane() {
        @Override
        public int getMaxCharactersPerLineCount() {
            return 80;
        }
    };
    return pane;
}

From source file:io.github.jeremgamer.editor.panels.Actions.java

public Actions(final JFrame frame, final ActionPanel ap) {
    this.frame = frame;

    this.setBorder(BorderFactory.createTitledBorder(""));
    JButton add = null;/*from w ww .ja v  a  2 s.c o m*/
    try {
        add = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("add.png"))));
    } catch (IOException e) {
        e.printStackTrace();
    }
    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                JOptionPane jop = new JOptionPane();
                @SuppressWarnings("static-access")
                String name = jop.showInputDialog(null, "Nommez l'action :", "Crer une action",
                        JOptionPane.QUESTION_MESSAGE);

                if (name != null) {
                    for (int i = 0; i < data.getSize(); i++) {
                        if (data.get(i).equals(name)) {
                            name += "1";
                        }
                    }
                    data.addElement(name);
                    new ActionSave(name);
                    OtherPanel.updateLists();
                    ButtonPanel.updateLists();
                    ActionPanel.updateLists();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    });

    JButton remove = null;
    try {
        remove = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png"))));
    } catch (IOException e) {
        e.printStackTrace();
    }
    remove.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                if (actionList.getSelectedValue() != null) {
                    File file = new File("projects/" + Editor.getProjectName() + "/actions/"
                            + actionList.getSelectedValue() + ".rbd");
                    JOptionPane jop = new JOptionPane();
                    @SuppressWarnings("static-access")
                    int option = jop.showConfirmDialog(null,
                            "tes-vous sr de vouloir supprimer cette action?", "Avertissement",
                            JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);

                    if (option == JOptionPane.OK_OPTION) {
                        if (actionList.getSelectedValue().equals(ap.getFileName())) {
                            ap.setFileName("");
                        }
                        ap.hide();
                        file.delete();
                        data.remove(actionList.getSelectedIndex());
                        OtherPanel.updateLists();
                        ButtonPanel.updateLists();
                    }
                }
            } catch (NullPointerException npe) {
                npe.printStackTrace();
            }

        }

    });

    JPanel buttons = new JPanel();
    buttons.setLayout(new BoxLayout(buttons, BoxLayout.LINE_AXIS));
    buttons.add(add);
    buttons.add(remove);

    updateList();
    actionList.addMouseListener(new MouseAdapter() {
        @SuppressWarnings("unchecked")
        public void mouseClicked(MouseEvent evt) {
            JList<String> list = (JList<String>) evt.getSource();
            if (evt.getClickCount() == 2) {
                int index = list.locationToIndex(evt.getPoint());
                if (isOpen == false) {
                    ap.show();
                    ap.load(new File("projects/" + Editor.getProjectName() + "/actions/"
                            + list.getModel().getElementAt(index) + ".rbd"));
                    previousSelection = list.getSelectedValue();
                    isOpen = true;
                } else {
                    try {
                        if (previousSelection.equals(list.getModel().getElementAt(index))) {
                            ap.hide();
                            previousSelection = list.getSelectedValue();
                            list.clearSelection();
                            isOpen = false;
                        } else {
                            ap.hideThenShow();
                            previousSelection = list.getSelectedValue();
                            ap.load(new File("projects/" + Editor.getProjectName() + "/actions/"
                                    + list.getModel().getElementAt(index) + ".rbd"));
                        }
                    } catch (NullPointerException npe) {
                        ap.hide();
                        list.clearSelection();
                    }
                }
            } else if (evt.getClickCount() == 3) {
                int index = list.locationToIndex(evt.getPoint());
                if (isOpen == false) {
                    ap.show();
                    ap.load(new File("projects/" + Editor.getProjectName() + "/actions/"
                            + list.getModel().getElementAt(index) + ".rbd"));
                    previousSelection = list.getSelectedValue();
                    isOpen = true;
                } else {
                    try {
                        if (previousSelection.equals(list.getModel().getElementAt(index))) {
                            ap.hide();
                            previousSelection = list.getSelectedValue();
                            list.clearSelection();
                            isOpen = false;
                        } else {
                            ap.hideThenShow();
                            previousSelection = list.getSelectedValue();
                            ap.load(new File("projects/" + Editor.getProjectName() + "/actions/"
                                    + list.getModel().getElementAt(index) + ".rbd"));
                        }
                    } catch (NullPointerException npe) {
                        ap.hide();
                        list.clearSelection();
                    }
                }
            }
        }
    });
    JScrollPane listPane = new JScrollPane(actionList);
    listPane.getVerticalScrollBar().setUnitIncrement(Editor.SCROLL_SPEED);
    this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    this.add(buttons);
    this.add(listPane);
    OtherPanel.updateLists();
}