X button or ok button on JOptionPane?

I have this message box that am throwing in my Gui. My question goes: how can I know if the user pushes the x button or the ok button in JOptionPane?
JOptionPane.showMessageDialog(this,
"Du har glemt at skrive en tekst!",
"BESKED",
JOptionPane.PLAIN_MESSAGE);     

It returns a value corresponding to something like JOptionPane.OK_OPTION.
Hope this helps.

Similar Messages

  • Help Button in JOptionPane

    Hi all,
    is there any easy way to provide a (possible locale sensitive) Help Button in JOptionPane Dialogs? Are there any libs that extend the Standard Dialogs to provide such a button?
    Many thanks for your ideas!
    bye
    Marcus

    This guy made his own alternative OptionPane that has such a feature:
    [http://javagraphics.blogspot.com/2008/06/joptionpane-making-alternative.html|http://javagraphics.blogspot.com/2008/06/joptionpane-making-alternative.html]

  • How to focus No button in JOptionPane.showConfirmDialog?

    How to focus No button in JOptionPane.showConfirmDialog when the Dialog window is opened?
    (default it focus Yes button).
    Please help me. Thank you very much.

    Use the showOptionDialog(...) method then you can specify the buttons and which button has focus.

  • Adding custom buttons to JOptionPane

    Hi,
    I need to add custom buttons to my showMessageDialogue boxes.
    I know that
    JOptionPane.showMessageDialog
    (null, "message body", "message title", JOptionPane.YES_NO_OPTION,
    JOptionPane.INFORMATION_MESSAGE);displays an information box with a 'Yes' and a 'No' button. But how do I include three or more custom buttons on such a box?
    For example, I want to display an information message box with buttons: 'Auto', 'Prompt' and 'Never'.
    What if I want to mix orthodox buttons, with my custom buttons? For example, putting 'Auto', 'Prompt' and 'Cancel' in one information message box?
    Thanks,
    Hasanka.

    Use the last method for showMessageDialog. The options[] paremeter can take an array of Strings and return to you which button was pressed (by index).
    showOptionDialog
    public static int showOptionDialog(Component parentComponent,
                                       Object message,
                                       String title,
                                       int optionType,
                                       int messageType,
                                       Icon icon,
                                       Object[] options,
                                       Object initialValue)Brings up a modal dialog with a specified icon, where the initial choice is dermined by the initialValue parameter and the number of choices is determined by the optionType parameter.
    If optionType is YES_NO_OPTION, or YES_NO_CANCEL_OPTION and the options parameter is null, then the options are supplied by the Look and Feel.
    The messageType parameter is primarily used to supply a default icon from the Look and Feel.
    Parameters:
    parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used
    message - the Object to display
    title - the title string for the dialog
    optionType - an integer designating the options available on the dialog: YES_NO_OPTION, or YES_NO_CANCEL_OPTION
    messageType - an integer designating the kind of message this is, primarily used to determine the icon from the pluggable Look and Feel: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE
    icon - the icon to display in the dialog
    options - an array of objects indicating the possible choices the user can make; if the objects are components, they are rendered properly; non-String objects are rendered using their toString methods; if this parameter is null, the options are determined by the Look and Feel.
    initialValue - the object that represents the default selection for the dialog
    Returns:
    an integer indicating the option chosen by the user, or CLOSED_OPTION if the user closed the Dialog

  • Can i use the buttons in JOptionPAne instead of default

    sir
    i want to change means insert icon on the the ok and cancel buuton instaed of JOptionPane buuttons
    can i do and how?
    thanks

    import java.awt.*;
    import javax.swing.*;
    public class CustomOptionPane {
      public static void main(String[] args) {
        JButton buttonOne = new JButton("One");
        JButton buttonTwo = new JButton("Two");
        JButton buttonThree = new JButton("Three");
        JButton[] buttons = {
          buttonOne, buttonTwo, buttonThree
        for(int i = 0; i < buttons.length; i++)
          buttons.setIcon(new ImageIcon("images/T" + (i + 3) + ".gif"));
    JOptionPane pane = new JOptionPane("Your options are:",
    JOptionPane.PLAIN_MESSAGE,
    JOptionPane.DEFAULT_OPTION,
    null,
    buttons,
    buttons[0]);
    JFrame f = new JFrame();
    JDialog dialog = pane.createDialog(f, "Custom Option Pane");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(200,100);
    f.setLocation(300,400);
    f.setVisible(true);
    dialog.show();

  • How to right align the buttons in JOptionPane?

    Hi,
    I am using JOptionPane in my code.
    I want all the default buttons that are created by any of the JOptionPane methods (e.g. JOptionPane.showMessageDialog()) be right aligned. Currently all the buttons are shown center aligned.
    Here is the only code that I have to display the dialog:
    JOptionPane.showMessageDialog(getInstance(), msg, "Deprecated components", JOptionPane.WARNING_MESSAGE);
    Can someone tell me how to achieve this?
    Regards,
    ParagJ

    Build your own JDialog

  • Can i insert the custom button on JOptionPane

    sir i want to insert the custom button instead of default buttons means
    i want to insert the icon button of ok and cancel.
    plz guide me with line of codes
    thanks in advance

    plz guide me

  • Anyone know how to add a border to buttons in JOptionPanes?

    I can't find a way to add a border to the buttons on my JOption dialog boxes. I would like them to have the same look and feel of my other buttons. Any suggestions?

    This inspired me to look for a way to globally change the background color of JTextFields. ALso the color of JLabels and the font for labels and textfields. I searched the API for topics related to javax.swing.UIManager.put but cannot find anything helpful. Can you point me in the right direction or tell me how to do this?
    Thanks

  • JOptionPane buttons in English and in another language?

    How do I change the language of the buttons in JOptionPane.showInputDialog?
    When I use the Traditional Chinese Windows, I see Chinese characters in the buttons.
    Can I have buttons in English even if I use the Traditional Chinese Windows? How? What web pages should I consult?
    Locale locale = Locale.getDefault();
        locale = new Locale("en", "US");
        Locale.setDefault(locale);does not help me to get the correct human language I prefer, e.g. on the confirm button.
    Edited by: tse2009 on Sep 12, 2009 6:22 AM

    Finally, I searched the web and got some ideas from:
    [http://jackywu1978.javaeye.com/blog/forum/97|http://jackywu1978.javaeye.com/blog/forum/97]
    Then, I can get the preferred language on the buttons on JOptionPane.
    import sun.awt.AppContext;
    public class getTheRightLanguagePreferred{
    Locale locale = Locale.getDefault();
        locale = new Locale("en", "US");
        Locale.setDefault(locale);
    AppContext.getAppContext().put("JComponent.defaultLocale", locale);
    }If you think that it is useful, please comment.

  • Keystrokes for button actions in JOptionPane

    When running my app in Windows, the Alt key must be pressed to cause the "Yes" button or "No" button to respond from the keyboard. Can I set an option in JOptionPane, or elsewhere, to not require pressing the Alt key to activate a button in JOptionPane?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    * Class JPSOptionPane
    * @author Sreenivasa Reddy M
    * @version 1.0
    public class JPSOptionPane {
    private static boolean isKeyPressed;
    private static Object selectedValue;
    * Method showOptionDialog
    * @param parentComponent
    * @param message
    * @param title
    * @param optionType
    * @param messageType
    * @param icon
    * @param options
    * @param initialValue
    * @return
    public static int showOptionDialog (Component parentComponent,
    Object message, String title,
    int optionType, int messageType,
    Icon icon, Object [] options,
    Object initialValue) {
    JOptionPane pane = new JOptionPane (message, messageType, optionType,icon, options, initialValue);
    pane.setInitialValue (initialValue);
    HashSet hash1 = new HashSet ();
    hash1.add (AWTKeyStroke.getAWTKeyStroke (KeyEvent.VK_UP, 0,true));
    hash1.add (AWTKeyStroke.getAWTKeyStroke (KeyEvent.VK_TAB, 0,true));
    pane.setFocusTraversalKeys (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,hash1);
    HashSet hash2 = new HashSet ();
    hash2.add (AWTKeyStroke.getAWTKeyStroke (KeyEvent.VK_TAB, 1, true));
    hash2.add (AWTKeyStroke.getAWTKeyStroke (KeyEvent.VK_DOWN, 0, true));
    final JDialog dialog = pane.createDialog (parentComponent, title);
    pane.setFocusTraversalKeys (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, hash2);
    ActionListener yActionLis=new ActionListener(){
    public void actionPerformed(ActionEvent e){
    isKeyPressed=true;
    dialog.dispose();
    selectedValue=new Integer(0);
    KeyStroke ks_y=KeyStroke.getKeyStroke(KeyEvent.VK_Y,8,true);
    pane.registerKeyboardAction(yActionLis,ks_y,JComponent.WHEN_IN_FOCUSED_WINDOW);
              ActionListener nActionLis=new ActionListener(){
    public void actionPerformed(ActionEvent e){
    isKeyPressed=true;
    dialog.dispose();
    selectedValue=new Integer(1);
    KeyStroke ks_n=KeyStroke.getKeyStroke(KeyEvent.VK_N,8,true);
    pane.registerKeyboardAction(nActionLis,ks_n,JComponent.WHEN_IN_FOCUSED_WINDOW);
    pane.selectInitialValue();
    dialog.show ();
    if(!isKeyPressed){
    dialog.dispose ();
    selectedValue = pane.getValue();
              isKeyPressed=false;
    else{
              isKeyPressed=false;
    return ((Integer) selectedValue).intValue ();
    if (selectedValue == null) {
    return JOptionPane.CLOSED_OPTION;
    if (options == null) {
    if (selectedValue instanceof Integer) {
    return ((Integer) selectedValue).intValue ();
    return JOptionPane.CLOSED_OPTION;
    for (int counter = 0, maxCounter = options.length; counter < maxCounter; counter++) {
    if (options [counter].equals (selectedValue)) {
    return counter;
    return JOptionPane.CLOSED_OPTION;
    * Method showInputDialog
    * @param parentComponent
    * @param message
    * @param title
    * @param messageType
    * @param icon
    * @param selectionValues
    * @param initialSelectionValue
    * @return
    * @throws HeadlessException
    public static Object showInputDialog (Component parentComponent,
    Object message, String title,
    int messageType, Icon icon,
    Object [] selectionValues,
    Object initialSelectionValue)
    throws HeadlessException {
    JOptionPane pane = new JOptionPane (message, messageType,JOptionPane.OK_CANCEL_OPTION,
    icon, null, null);
    HashSet hash1 = new HashSet ();
    hash1.add (AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_UP,0,true));
    pane.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, hash1);
    HashSet hash2 = new HashSet ();
    hash2.add(AWTKeyStroke.getAWTKeyStroke (KeyEvent.VK_DOWN, 0,true));
    pane.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, hash2);
    pane.setWantsInput (true);
    pane.setSelectionValues (selectionValues);
    pane.setInitialSelectionValue (initialSelectionValue);
    JDialog dialog = pane.createDialog (parentComponent, title);
    pane.selectInitialValue();
    dialog.show();
    dialog.dispose();
    Object value = pane.getInputValue();
    if (value == JOptionPane.UNINITIALIZED_VALUE){
    return null;
    return value;
    public static void showMessageDialog(Component parentComponent,
    Object message)
    throws HeadlessException{
    JOptionPane.showMessageDialog(parentComponent,message);
    public static void showMessageDialog(Component parentComponent,
    Object message,
    String title,
    int messageType)
    throws HeadlessException{
    JOptionPane.showMessageDialog(parentComponent,message,title,
    messageType);
    public static void showMessageDialog(Component parentComponent,
    Object message,
    String title,
    int messageType,
    Icon icon)
    throws HeadlessException{
    JOptionPane.showMessageDialog(parentComponent,message,title,
    messageType,icon);

  • JOptionPane and button L&F

    Hi everybody,
    I'm curious-- I've noticed that in Windows L&F, the buttons for JOptionPane dialogs switch places, from OK-Cancel to Cancel-OK. Also, they magically have icons appear on them (an X for Cancel and check mark for OK). Is there any way to stop this behavior using JOptionPane? I know it's possible to create your own dialog, and I plan to do that if all else fails, but if there's an easier way that anyone knows of, I'd be very interested to know. Has anyone else ever noticed this with JOptionPane?
    Thanks,
    Jezzica85

    icons appear on them (an X for Cancel and check mark for OK)What version of Windows?
    I've never seen this under XP (classic skin).

  • HELP --JOPtionPane.showInputDialog

    I was developing a employee information system in swing.
    On the way of development,the major problem the is occuring
    is that two JOPtionPane.showInput Dialog boxes are appearing
    at a time.When I fetching the data to the JOPtionPane.showInput Dialog box and then clicking the "ok" option button,another JOPtionPane.showInput Dialog box appears making the program difficult to
    bug.
    Please suggest ways to solve this bug.

    It is more likely that your program has logical errors, just search for all JOptionPane.showInputDialog(...) statements and insert a println statement before it to trace the source of the problem.
    Visual Paradigm - http://visual-paradigm.com/

  • JOptionPane Problem Plz help me

    I want to add an image button on JOptionPane
    how can i do?
    Means change the default button For JOptionPane.OK_OPTION
    Thanks in advance

    set your FORMS60_PATH on the client to Formspath;libpath;menupath so it looks in every dir named in the path.

  • JOptionPane JButton Icon

    Hi,
    Does anyone knows how to set icons in the JOptionPane buttons ?
    I use this but when I click it never closes the dialog.
    JButton[] options = new JButton[3];
    options[0] = new JButton("B1");
    options[1] = new JButton("B2");
    options[2] = new JButton("B3");
    int n = JOptionPane.showOptionDialog(null,
    "Would you like to replace it ?",
    "Question",
    JOptionPane.YES_NO_CANCEL_OPTION,
    JOptionPane.QUESTION_MESSAGE,
    null,
    options,
    null);
    System.out.println(n);
    Thanks.

    Use this :
    Icon[] options = { new ImageIcon("yes.gif"),new ImageIcon("no.gif"), new ImageIcon("cancel.gif")};
    int res = JOptionPane.showOptionDialog(null, "Select a button", "title", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);I hope this helps,
    Denis

  • Tooltip for a JOptionPane.showConfirmDialog??

    I can't get this to work, any ideas on how to reference the OK & Cancel buttons of the "JOptionPane.showConfirmDialog" and create a custom tooltip for each??
    // --------- test to see if the current file was modified and give an option to save first.
    if (changed)
    // ----- display pop-up alert --------------------------------------------------------------
    int confirm = JOptionPane.showConfirmDialog(null,
    "Click OK to discard current changes, \n or Cancel to save before proceeding.", // msg
    "Unsaved Modifications!", // title
    JOptionPane.OK_CANCEL_OPTION, // buttons displayed
                        // JOptionPane.ERROR_MESSAGE
                        // JOptionPane.INFORMATION_MESSAGE
                        // JOptionPane.PLAIN_MESSAGE
                        // JOptionPane.QUESTION_MESSAGE
    JOptionPane.WARNING_MESSAGE,
    null);
    // -- set the tool tip for the buttons here -- ??????
    // public Point getToolTipLocation( MouseEvent event);
    // public JToolTip createToolTip( );
    // toolTipText
    // Point getToolTipLocation( MouseEvent MOUSE_CLICKED);
    JOptionPane.getToolTipLocation(OK_OPTION, Click to discard current changes."")
    // OKButton.setToolTipText("Click to discard current changes.");
    // CancelButton.setToolTipText("Click to save the current file.");
    if (confirm != JOptionPane.YES_OPTION)
    { //user wants to save changes
    try {
    // save the file
    catch(Exception e) {}
    } // close "if (changed)"
    // ----- display pop-up alert --------------------------------------------------------------

    The renderer code has this structure:
    public class MyRenderer extends DefaultTreeCellRenderer {
         public Component getTreeCellRendererComponent(JTree tree, Object value, boolean isSelected, boolean isExpanded, boolean isLeaf, int index, boolean hasFocus) {
              JTextPanel pane = new JTextPanel();
              pane.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 0));
              if (data instanceof MyDataObject) {
                   MyDataObject myObj = (MyDataObject)data;
                   JLabel label = new JLabel();
                   label.setIcon(myObj.getIcon());
                   label.setText(myObj.getInfo());
                   label.setToolTipText("MyTooltip");
                   pane.add(label);
              return pane;
    Thomas Wiedmann

Maybe you are looking for

  • Dreamweaver 8 with ASP, ColdFusion, and PHP

    I'm having trouble with diplaying multiple recordsets. I did everything that the book told me to do, but it doesn't work. I just got done with the tours.asp and it works fine. Now I'm working on the index.asp. rs_journal comes up fine, but when I add

  • Using Oracle 8i db with creator

    Hi there, could someone please help me, I'm trying to use an 8i database, I added an 8i driver to creator, I can see the tables in creator and run queries but as soon as I try to bind it to a control it gives an error. The program works fine if I use

  • InDesign Japanese to InDesign English

    Are there any issues that would prevent a file created in InDesign CS 4 (Japanese) file from being opened in InDesign CS 5 (US version)?  We have a client that has asked us to create a file in Japanese and then to outline the fonts and send them the

  • Setting break lines

    Hi all I want to know how to insert a blank line between the first line of column labels and lines of column values in a group-left report . And is it also possible to break line between each result set records ? Thank you.

  • Can BAM extract data from Excel Sheet?

    Hi, Thanks for seeing my message. We have huge data in the form of Excel sheet with large number of regular updates every day. Can BAM read Excel sheet so I can make a dash board reflecting lot of KPI's to track? regards,