Mnemonics in JOptionPane buttons??

ive looked wround the api but i find no method that allows to add mnemonics to the default buttons that u can get when having a JOptionPane dialog box popup.
does anyone knowhow to add mnemonics to these buttons?
normally u would say button.setMnemonic(KeyEvent.VK_X)... but these buttons dont have a variable associated with them.
help please!

Hi... it may seem that I'm stating the obvious, but the JOptionPane classes are designed as a quick and dirty convenience method. There exist options to create your own buttons and pass them to the JOptionPane dialog constructor(s), which you should do to solve your problem. :-/
Merry Christmas

Similar Messages

  • Mnemonics for JDialog Buttons?

    ive looked wround the api but i find no method that allows to add mnemonics to the default buttons that u can get when having a JOptionPane dialog box popup.
    does anyone knowhow to add mnemonics to these buttons?
    normally u would say button.setMnemonic(KeyEvent.VK_X)... but these buttons dont have a variable associated with them.
    help please!

    I don't beleive that you can add anything to a JOptionPane dialog box. All you can do is display them and take whatever they return to you. Try extending Jdialog and making your own then you can do whatever you want.

  • 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.

  • JOptionPane buttons(OK/Cancel) in German Language??

    hi,
    I need to do translation of my Swing Screens.
    I implemented it using ResouceBundle.Now the messages are coming in the JOptionPane are coming from the property file.
    But the buttons-->OK & CANCEL are still in english.I need to translate this also.How to do this.??Any pointers on this.??
    thnx
    Neel

    hi,
    sorry guys..I got the solution from Java Doc itself
    This is the solution
    Object[] options = { "OK", "CANCEL" };
    JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
    JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
    null, options, options[0]);Thnx
    Neel

  • JOptionPane button renaming

    Hi guys!
    Had loadsa help with my last post, and I really appreciate it! But I call upon you once again!
    Finishing touches, and I wanna remname the buttons in a JOptionPane message box.
    I want a message to pop up and state the message "Welcome! Click 'next' to continue"
    and for it to have two buttons, saying NEXT and QUIT. Have been rolling around the APIs, but can't see a thing that'll help me. Got some nice tips like renaming the title of the window a.s.o., but no button renaming options!
    Really appreciate the help... !
    :)

         JOptionPane op = new JOptionPane();
         String s = "Welcome! Click 'next' to continue";
         Object[]    bt = {"NEXT","QUIT"};
         op.showOptionDialog(null,s, "",op.YES_NO_OPTION,
                        op.QUESTION_MESSAGE, null, bt,bt[0]);      Noah

  • How to Handle Events for JOptionPane buttons?

    Hi All,
    I need a help...
    I am trying to develop a simple SWING application that consists of multiple elements (mainly text fields) and a Submit button. On pressing Submit button, it should validate all the fields and then do something else. But if some fields are left blank, then it should give a pop-up message and on clicking the OK button on the pop-up message, the focus should go to that text field which is left null.
    The problem is I am not able to understand how to Capture this OK button click-event of the pop-up message. I mean how to do something when the OK/Cancle button of the pop-up window is clicked.
    Please Help me.
    Thanks in Advance,
    Ujjal

    As already pointed out, JOptionPane has some static methods which show a dialog, and return an int, or a string. The whole point of this class is to remove the need for any event handling by the developer, that is, you simply call the showDialog method or whatever, and it returns a value telling you, for example, which button was pressed - no need for action listeners at all. The result of the showDialog method (or whichever you use) tells you which button was pressed. Check the javadocs for JOptionPane for details of several constants that indicate which button was pressed
    The class exists to make simple dialogs trivial to generate - you're overcomplicating things!

  • JOptionPane button keyevents

    how do i enable the buttons to respsond to key presses?
    here's my Yes_No dialog to the user when he wants to quit the prog. buttons respond on clicking but there's no key event associated. How can i associate key presses with the buttons?
    final JOptionPane optionPane = new
         JOptionPane("Are you sure you want to quit?",
            JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
         final JDialog dialog = new JDialog(this, "Exit", true);
         dialog.setContentPane(optionPane);
         dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
         optionPane.addPropertyChangeListener(new PropertyChangeListener() {
         public void propertyChange(PropertyChangeEvent e)
              String prop = e.getPropertyName();
              if (dialog.isVisible() && (e.getSource() == optionPane) && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop.equals(JOptionPane.INPUT_VALUE_PROPERTY)))
                         dialog.setVisible(false);
         dialog.pack();
         dialog.setLocationRelativeTo(null);
         dialog.setVisible(true);
         int result = ((Integer)optionPane.getValue()).intValue();
         if (result == JOptionPane.YES_OPTION)
              System.exit(0);
         else
              System.out.println("User decided not to quit.");

    hi,
    JOptionPane seems to have a keylistener, you could use that to detect key events, extend the JOptionPane class, making your own JOptionPane and use the listener to detect the key you want and handle the event.

  • Change Button Name in JOptionPane.showMessageDialog

    Hello,
    I've written the following code that invokes a JOPtionPane messageDialog, when the delete button is hit in the following frame and the selectedItem in the JComboBox is Default.
    I want to change the JOPtionPane button name from OK to Close. Is there a way to do this with error message dialoges, as their is with confirmDialoges?
    Please help out.
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    public class MyJOptionPaneTest {
         public MyJOptionPaneTest()
              JFrame frame = new JFrame("My Frame");
              JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT,5,0));
              final JComboBox myComboBox = new JComboBox();
              myComboBox.setPreferredSize(new Dimension(100,20));
              myComboBox.getModel().setSelectedItem("Default");
              myComboBox.addItem("Default");
              myComboBox.addItem("Other");
              JButton deleteButton = new JButton("Delete");
              deleteButton.addActionListener(new ActionListener()
                   public void actionPerformed(ActionEvent e)
                        if(myComboBox.getSelectedItem().equals("Default"))
                             JOptionPane.showMessageDialog(null, "Default cannot be deleted", "Error", JOptionPane.ERROR_MESSAGE);
              panel.add(myComboBox);
              panel.add(deleteButton);
              frame.getContentPane().add(panel);
              frame.setSize(200,100);
              frame.setResizable(false);
              frame.setVisible(true);
         public static void main(String [] args)
              MyJOptionPaneTest test = new MyJOptionPaneTest();
    }

    Hi,
    yes, I've looked at the tutorials. But there is no way to change button title for JOptionPane.showMessageDialog. Only JOptionPane.showOptionsDialog provides you w/ that feature, becuz it provides the parameter in the method.
    I was wondering if there was another way to do this for showMessageDialog.

  • JFileChooser - mnemonics for buttons

    Hi,
    is it possible to set your own mnemonics for the buttons save/open and cancel in JFileChooser? If so, how does it work??
    thanks
    sicki

    All properties have standard names; I don't know those of the mnemonics and if they're not in the list, I'm afraid you can't change them.
    You maybe could try to make our own properties file with a non-existent locale and define what you need in that, if the mnemonics exist and it doesn't work by doing "UIManager.put(propertyname, propertyvalue)". Then you set your Locale before the call to the new Locale and it should work.
    Hope this explanation helps you out anyway.
    Greetings,
    Silkje

  • Mnemonic in JOptionPane dialogs

    In the dialogs opening with JOptionPane.showConfirmDialog(...) There are mnemonics 'Y' & 'N' set for Yes & No options resp.
    Yes option can be chosen with Alt+Y & No option with Alt+N
    Is there any WorkAround that by just pressing 'Y' option Yes is selected. (like there with option dialogs in windows).

    Here you go ... I have written the code for you ..
    package ramanujam.test;
    import javax.swing.*;
    import java.awt.event.*;
    public class TestKeyOnPane extends JOptionPane implements ActionListener, KeyListener
          * Create Constants for users to get notified of the button clicked
              public static final int YES_BUTTON_PRESSED = 0;
              public static final int NO_BUTTON_PRESSED = 1;
              public static final int CANCEL_BUTTON_PRESSED = 2;
              public static final int OK_BUTTON_PRESSED = 3;
              public static final int CUSTOM_BASE_VALUE = 4;
              private int selectedButtonValue = -1;
              StringBuffer usedMnemonics = null;
              private JButton[] bOptionsX;
          *Inner Action Listener to take care of Property Change Notification
         class PropertyActionListener implements ActionListener
              public void actionPerformed(ActionEvent ae)     {
                   setValue(ae.getSource());
    *private KeyListener to setValue on enter key to the respective value and
    * -1 on exit.
    class PropertyKeyListener extends KeyAdapter
         private JButton[] bInnerObj;
         public PropertyKeyListener(JButton[] bInnerObjParam)
              bInnerObj = bInnerObjParam;
         public void keyPressed(KeyEvent ke)
              int code = ke.getKeyCode();
              /*               if(code == KeyEvent.VK_ENTER)
                                  for(int i=0;i<bInnerObj.length;i++)
                                       if(bInnerObj.hasFocus())
                                            bInnerObj[i].doClick();
                                            break;
              for (int i = 0; i < bInnerObj.length; i++)
                   char cc[] = {(char) code};
                   String str = new String(cc);
                   if (bInnerObj[i].getText().startsWith(str))
                        bInnerObj[i].doClick();
                        break;
         * Constructor - to match that of parent class
         * Creates a MojoActionOptionPane with a test message.
         public TestKeyOnPane()
              this("MojoActionOptionPane Message",PLAIN_MESSAGE,OK_OPTION,null,new Object[]{"OK"},null);
         *Creates a instance of MojoActionOptionPane to display a message using the plain-message  message type
         *and the default options delivered by the UI.
         *@param message - The message to be displayed
         public TestKeyOnPane(Object message)
              this(message,PLAIN_MESSAGE,OK_OPTION,null,new Object[]{"OK"},null);
         *Creates an instance of MojoActionOptionPane to display a message with the specified message
         *type and the default options,
         *@param message The message to be displayed
         *@param messageType The Type of message to be displayed
         public TestKeyOnPane(Object message, int messageType)
              this(message,messageType,OK_OPTION,null,new Object[]{"OK"},null);
         *Creates an instance of MojoActionOptionPane to display a message with the specified message type
         *and options.
         *@param message The message to be displayed
         *@param messageType The Type of message to be displayed      
         *@param optionType The Type of User Action Options to be displayed
         public TestKeyOnPane(Object message, int messageType, int optionType)
              this(message,messageType,optionType,null,new Object[]{"OK"},null);
         *Creates an instance of MojoActionOptionPane to display a message with the specified message type,
         *options, and icon. 
         *@param message The message to be displayed
         *@param messageType The Type of message to be displayed
    *@param optionType The Type of User Action Options to be displayed
         *@param icon The Icon to be displayed on the option pane
         public TestKeyOnPane(Object message, int messageType, int optionType, Icon icon)
    this( message, messageType, optionType, icon,new Object[]{"OK"},null);
         *Creates an instance of MojoActionOptionPane to display a message with the specified message type,
         *icon, and options.
         *@param message The message to be displayed
         *@param messageType The Type of message to be displayed
    *@param optionType The Type of User Action Options to be displayed
         *@param icon The Icon to be displayed on the option pane
         *@param options     A Collection of values from which the user can select     
         public TestKeyOnPane(Object message, int messageType, int optionType, Icon icon, Object[] options)
    this(message, messageType, optionType, icon, options,options[0]);
         *Creates an instance of MojoActionOptionPane to display a message with the specified message type,
         *icon, and options, with the initially-selected option specified. 
         *@param message The message to be displayed
         *@param messageType The Type of message to be displayed
    *@param optionType The Type of User Action Options to be displayed
         *@param icon The Icon to be displayed on the option pane
         *@param options     A Collection of values from which the user can select
         *@param initialValue The Default Selected Value for the OptionPane
         public TestKeyOnPane(Object message, int messageType, int optionType, Icon icon, Object[] options, Object initialValue)
    super(message, messageType,optionType,icon,options,initialValue);
              setOptions(options);
    setInitialValue(initialValue);
         * Event Handler to take care of enter,escape or any other case
         * Here, we check the button clicked, we set the clicked buttons
         * index using the caption of the button.
         * @see setSelectedButtonValue()
         * method.
              public void actionPerformed(ActionEvent ae)
                   String actionCommand = ae.getActionCommand();
                   if(actionCommand.equalsIgnoreCase("OK"))
                        setSelectedButtonValue(OK_BUTTON_PRESSED);
                   else if(actionCommand.equalsIgnoreCase("CANCEL"))
                        setSelectedButtonValue(CANCEL_BUTTON_PRESSED);
                   else if(actionCommand.equalsIgnoreCase("YES"))
                        setSelectedButtonValue(YES_BUTTON_PRESSED);
                   else if(actionCommand.equalsIgnoreCase("NO"))
                        setSelectedButtonValue(NO_BUTTON_PRESSED);
                   else
                        int temp = CUSTOM_BASE_VALUE+findIndexOfCustomButton(actionCommand);
                        setSelectedButtonValue(temp);
         protected int findIndexOfCustomButton(String title)
              for(int i=0;i<options.length; i++)
                   String strTitle = ((JButton)options[i]).getText();
                   if(strTitle.equalsIgnoreCase(title))
                        return i;
              return -1;
         * Read only method that returns an int corresponding to the value of the button
         * clicked by the user
         * @return int The index of the selected button.
         * @see setSelectedButtonValue()
         * If the default YES, NO , CANCEL or OK buttons are used with the OptionPane,
         * the indices for the same defined by the constants in this class are used.
         * the indices 0 to 3 are reserved for the default buttons.
         * To get the indices for the custom buttons, the return value is decided by
         * its index in the options array + a base value 4, that is used to avoid
         * any possible ambiguity with the default button index.
         public int getSelectedButtonValue()
              return this.selectedButtonValue;
    * Invoked when a key has been pressed.
    public void keyPressed(java.awt.event.KeyEvent ke)
              int code = ke.getKeyCode();
              /*               if(code == KeyEvent.VK_ENTER)
                                  for(int i=0;i<bInnerObj.length;i++)
                                       if(bInnerObj[i].hasFocus())
                                            bInnerObj[i].doClick();
                                            break;
              for (int i = 0; i < this.options.length; i++)
                   char cc[] = {(char) code};
                   String str = new String(cc);
                   if (bOptionsX[i].getMnemonic() == code || ((char)bOptionsX[i].getMnemonic()) == Character.toUpperCase(cc[0]))
                        bOptionsX[i].doClick();
                        break;
         * Invoked when a key has been released.
    public void keyReleased(java.awt.event.KeyEvent e) {}
         * Invoked when a key has been typed.
         * This event occurs when a key press is followed by a key release.
    public void keyTyped(java.awt.event.KeyEvent e) {}
         *This method sets the Mnemonics for the buttons
         *@param buttons - Array of JButtons for which mnemonics are to be set
         *@return void
         protected void setMnemonics(JButton[] buttons)
              * 1. Get the button array
              * 2. Check if they are the default buttons.
              * 3. If they are, then set the first character as the mnemonic, since Yes, No, Cancel
              * and OK do not have common first characters.
              * 4. Else, if the buttons are custom defined, then get the first non ambiguous
              * character from the button's text and set that as the mnemonic          
              usedMnemonics = new StringBuffer();     
              int buttonsLen = buttons.length;
              String strArr[] = new String[buttonsLen];
              defaultMnemonicSetter:for(int i=0;i<buttonsLen;i++)
                   strArr[i] = buttons[i].getText();
                   if(strArr[i].equalsIgnoreCase("OK") || strArr[i].equalsIgnoreCase("Cancel") || strArr[i].equalsIgnoreCase("Yes") || strArr[i].equalsIgnoreCase("No"))
                        buttons[i].setMnemonic(strArr[i].charAt(0));
                        usedMnemonics.append(strArr[i].charAt(0));
                   else
                        char charAt0[] = {strArr[i].charAt(0)};
                        String strAt0 = new String(charAt0);
                        customMnemonicSetter :for(int j=0;j<strArr[i].length();j++)
                             char charAt0j[] = {strArr[i].charAt(j)};
                             String strAt0j = new String(charAt0j);
                             if(usedMnemonics.toString().indexOf(strAt0j) == -1)
                                  buttons[i].setMnemonic(charAt0j[0]);
                                  usedMnemonics.append(charAt0j);
                                  break customMnemonicSetter;                     
                             else
                                  continue customMnemonicSetter;
         *method that takes care of Adding actionListeners to
         *the buttons that are created by the OpionPane based on options
         *specified by the user
         *@param options[] - Array of type Object that holds the Button Options
         public void setOptions(Object options[])
              int len = options.length;
              Object optionsCopy[] = new Object[len];
              JButton buttonsCopy[] = new JButton[len];
              bOptionsX = new JButton[len];
              for(int i = 0; i < len; i++)
                   String optionTitle = options[i].toString();
                   JButton bBuff = new JButton(optionTitle);
                   bBuff.addActionListener(this);
                   bBuff.addActionListener(new PropertyActionListener());
                   optionsCopy[i] = bBuff;     
                   buttonsCopy[i] = bBuff;
                   bOptionsX[i] = bBuff;
                   //bBuff.addKeyListener(new PropertyKeyListener(buttonsCopy));
                   bBuff.addKeyListener(this);
              setMnemonics(buttonsCopy);
              buttonsCopy = null;
              super.setOptions(optionsCopy);
              optionsCopy = null;
         * private mutator to set the value of the selected button
         * @param value - the value to be set
         * @see getSelectedButtonValue()
         private void setSelectedButtonValue(int value)
              this.selectedButtonValue = value;

  • JOptionPane and JDialog.DO_NOTHING_ON_CLOSE broken?

    Hi there
    I've created a JDialog from a JOptionPane and I don't want the user to simply dispose of the dialog by clicking on the close button but they are still able and I'm sure that my code is correct. Is it my code that is wrong or is it a java bug? Oh I'm running on 1.3 BTW
    JFrame f = new JFrame();
    f.setSize(500, 500);
    f.setVisible(true);
    JOptionPane optionPane = new JOptionPane("Hello there", JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
    JDialog d = optionPane.createDialog(f, "Testing");
    d.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    d.setVisible(true);I know that I can just set up a JDialog directly and use the setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE) and it seems to work properly but I would like to see it work for JOptionPane!
    Thanks in advance

    Sorry but this doesn't make it work either. I've looked at the code for createDialog in JOptionPane and it actually adds a WindowListener to the dialog in there as well as a propertyListener. On closing the option pane it calls a method in windowClosing of the windowListener which in turn fires a property change event which then tells the dialog to dispose itself so the addition of another windowAdapter to override the windowClosing method will not help :-(
    I've managed to get round it by doing something similar to
    JFrame frame = new JFrame();
    final JDialog dialog = new JDialog(frame, "Testing", true);
    JButton button = new JButton("OK");
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        dialog.dispose();
    JOptionPane optionPane = new JOptionPane(button, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
    dialog.setContentPane(optionPane);
    dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    dialog.pack();
    dialog.setVisible(true);

  • 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

  • Mnemonics inconsistent w/XP & 1.4.1

    When I set mnemonics for menus & buttons in my Swing application (running with j2sdk1.4.1), the underlines don't appear at all until the Alt key is pressed, and then they'll be there for the next several tries, before apparently going away for good. I can still operate the mnemonic-enabled menus & buttons with keystrokes, but the underlines no longer appear.
    The mnemonics with underlines work fine when the same application is run under RH Linux 7.3 however.
    Is this a known bug, and is there any workaround?

    Actually, the menus alone work all right (except that the underscores don't appear until you press the Alt key, which is actually a nice feature), but when a button with mnemonics is added, then the problem occurs:import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JCheckBoxMenuItem;
    import javax.swing.JRadioButtonMenuItem;
    import javax.swing.ButtonGroup;
    import javax.swing.JMenuBar;
    import javax.swing.KeyStroke;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.UIManager;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JFrame;
    * This class exists solely to show you what menus look like.
    * It has no menu-related event handling.
    public class MenuLookDemo extends JFrame {
        JTextArea output;
        JScrollPane scrollPane;
      public MenuLookDemo() {
        JMenuBar menuBar;
        JMenu menu, submenu;
        JMenuItem menuItem;
        JCheckBoxMenuItem cbMenuItem;
        JRadioButtonMenuItem rbMenuItem;
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
        //Add regular components to the window, using the default BorderLayout.
        Container contentPane = getContentPane();
        output = new JTextArea(5, 30);
        output.setEditable(false);
        scrollPane = new JScrollPane(output);
        contentPane.add(scrollPane, BorderLayout.CENTER);
        //Create the menu bar.
        menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        //Build the first menu.
        menu = new JMenu("A Menu");
        menu.setMnemonic(KeyEvent.VK_A);
        menu.getAccessibleContext().setAccessibleDescription(
                "The only menu in this program that has menu items");
        menuBar.add(menu);
        //a group of JMenuItems
        menuItem = new JMenuItem("A text-only menu item",
                                 KeyEvent.VK_T);
        //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_1, ActionEvent.ALT_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "This doesn't really do anything");
        menu.add(menuItem);
        menuItem = new JMenuItem("Both text and icon",
                                 new ImageIcon(getClass().getResource("images/middle.gif")));
        menuItem.setMnemonic(KeyEvent.VK_B);
        menu.add(menuItem);
        menuItem = new JMenuItem(new ImageIcon(getClass().getResource("images/middle.gif")));
        menuItem.setMnemonic(KeyEvent.VK_D);
        menu.add(menuItem);
        //a group of radio button menu items
        menu.addSeparator();
        ButtonGroup group = new ButtonGroup();
        rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
        rbMenuItem.setSelected(true);
        rbMenuItem.setMnemonic(KeyEvent.VK_R);
        group.add(rbMenuItem);
        menu.add(rbMenuItem);
        rbMenuItem = new JRadioButtonMenuItem("Another one");
        rbMenuItem.setMnemonic(KeyEvent.VK_O);
        group.add(rbMenuItem);
        menu.add(rbMenuItem);
        //a group of check box menu items
        menu.addSeparator();
        cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
        cbMenuItem.setMnemonic(KeyEvent.VK_C);
        menu.add(cbMenuItem);
        cbMenuItem = new JCheckBoxMenuItem("Another one");
        cbMenuItem.setMnemonic(KeyEvent.VK_H);
        menu.add(cbMenuItem);
        //a submenu
        menu.addSeparator();
        submenu = new JMenu("A submenu");
        submenu.setMnemonic(KeyEvent.VK_S);
        menuItem = new JMenuItem("An item in the submenu");
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_2, ActionEvent.ALT_MASK));
        submenu.add(menuItem);
        menuItem = new JMenuItem("Another item");
        submenu.add(menuItem);
        menu.add(submenu);
        //Build second menu in the menu bar.
        menu = new JMenu("Another Menu");
        menu.setMnemonic(KeyEvent.VK_N);
        menu.getAccessibleContext().setAccessibleDescription(
                "This menu does nothing");
        menuBar.add(menu);
    /* Adding the button causes the problem when using Windows LnF
        JButton jButton_Search = new JButton();
        jButton_Search.setMnemonic(KeyEvent.VK_S);
        jButton_Search.setText("Search");
        getContentPane().add(jButton_Search, BorderLayout.SOUTH);
      public static void main(String[] args) {
    /* Commenting out the UIManager stuff below will make the programs "work" with Windows
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        catch(Exception e) {
          e.printStackTrace();
          MenuLookDemo window = new MenuLookDemo();
          window.setTitle("MenuLookDemo");
          window.setSize(450, 260);
          window.setVisible(true);
    }

  • Key-enable buttons on a JOptionPane

    Hi,
    The existing code in my Application uses a JOptionPane static method = JOptionPane.showOptionDialog() to create a dialog box.Is there any way I can key-enable the buttons on the dialog created by this method.
    public int createAndShowGUI() {
    if (!issuesPanel.hasIssues())
    return -2; //TODO make constant
    else {
    //Custom button text
    String[] options = should_continue ? new String[] {returnText, continueText} : new String[] {returnText};
    int optionButtons = should_continue ? JOptionPane.OK_CANCEL_OPTION : JOptionPane.CANCEL_OPTION ;
    log.info("IssuesOptionPane displayed - " + title);
    int optionChosen = JOptionPane.showOptionDialog(parent,
    issuesPanel.createAndGetIssuesPanel(),
    title,
    optionButtons,
    JOptionPane.PLAIN_MESSAGE,
    null, //don't use a custom icon
    options, //titles of buttons
    null); //no default selected button
    String buttontext = optionChosen == CLOSE ? "X" : options[optionChosen];
    log.info("User clicked on the " + buttontext + " button");
    return optionChosen;
    I see that there is no way to get a handle on the JButtons created by the JOptionPane.So, one work around that I tried is to create a JPanel, add JButtons , set Input Map, set Action Map ( to add key bindings ) on it.Then create a JDialog and pass this JPanel to the dialog using setContentPane
         private static void createAndShowGUI(){
              JButton bookingButton=new JButton(bookStr);
              JButton returnButton=new JButton(returnStr);
              bookingButton.addActionListener(new BookAction());
              returnButton.addActionListener(new ReturnAction());
              JPanel panel=new JPanel();
              panel.add(bookingButton);
              panel.add(returnButton);
              panel.setActionMap(initActionMap());
              initAndSetInputMap(panel);
              JDialog dialog=new JDialog();
              dialog.setSize(400,100);
              dialog.setModal(true);
              dialog.setContentPane(panel);
              dialog.addPropertyChangeListener(indicator,listener);
              System.out.println("step 1" );
              dialog.pack();
              System.out.println("step 2");
              dialog.setVisible(true);
    But the problem that I am facing here is that the property change events triggered by the code inside the actionPerformed methods is not getting capturesd by the listener attached to the JDialog.
    Any thoughts?
    Thanks
    Aman

    google: JOptionPane mnemonics
    http://www.devx.com/tips/Tip/13718
    http://forum.java.sun.com/thread.jspa?threadID=321824&messageID=1649963
    http://coding.derkeiler.com/Archive/Java/comp.lang.java.gui/2005-03/0495.html
    etc.

  • JoptionPane need some help understanding, with buttons PLEASE SOMEONE :)

    Hi all I was hoping that someone would be kind enough to explain to me how I can add a method to buttons in a JoptionPane.
    Also can I pplease ask for you to look at this code I think I have stuff something as it is not working the way that I want,
    I want to add something like this to the ok button.
    String check = LastNameJText.getText()+FirstNameJText.getText();
    System.out.println(check);
          if
            //check to see if First & Last name have been entered.
            (check != null){
            return;
            else //create a dialog that shows Title,first & Last Name
             // & ask if you would like to make an new ordre
              String d= System.getProperty("line.separator");
           Object[] options = {"Cancel","OK",};
           int n = JOptionPane.showOptionDialog(null,
           "Do you really want to add a new shoe order for:"+ d +
           (String) TitleComboBox.getSelectedItem()+" "+FirstNameJText.getText()+" "+LastNameJText.getText(),"New Order",
           JOptionPane.YES_OPTION,
           JOptionPane.INFORMATION_MESSAGE,
           null,
           options,
        options[0]);
          if //Check to see if First & Last name text feilds are clear.
    (check == null){
    return ;
    else
       //make sure that all text fields are clear for new order
            ClearAllTextFields();

    When I do that i get
    "Order.java": Error #: 300 : variable options not found in class epod_1_0.Order at line 2152, column 4
    "Order.java": Error #: 300 : variable options not found in class epod_1_0.Order at line 2153, column 4
    why would this be ?
    This is the code
    String check = LastNameJText.getText()+FirstNameJText.getText();
    System.out.println(check);
          if
            //check to see if First & Last name have been entered.
            (check != null){
            return;
            else //create a dialog that shows Title,first & Last Name
             // & ask if you would like to make an new ordre
              String d= System.getProperty("line.separator");
          int result = JOptionPane.showOptionDialog(null,
       "Do you really want to add a new shoe order for:"+ d +
       (String) TitleComboBox.getSelectedItem()+" "+FirstNameJText.getText()+
        " "+LastNameJText.getText(),"New Order",
       JOptionPane.YES_OPTION,
       JOptionPane.INFORMATION_MESSAGE,
       null,
       options,
       options[0]);
    switch(result){
       case JOptionPane.YES_OPTION: // Add your code for yes
          break;
       case JOptionPane.NO_OPTION: // Add your code for no
         dispose();
          break;
          if //Check to see if First & Last name text feilds are clear.
    (check == null){
    return ;
    else
       //make sure that all text fields are clear for new order
            ClearAllTextFields();
      }

Maybe you are looking for

  • Adding a third (down" behavior to my navigation buttons

    Hello, I've designed my first two-state navigation bar using DW-generated behaviors (Javascripts). I have an "up" (inactive) graphic, and a "mouseover" graphic working. I also have a "down" state graphic created, but don't see how I would apply that

  • Why are my jpeg images lighter in Bridge than in photoshop?

    Have tried all the options suggestion in a similar issue in 2012 without success. Been onto Adobe chat 3 times.  Raw images look okay in Bridge but when opened and saved as jpegs, in Bridge they look much lighter but jpegs look okay when opened in ph

  • Thermal issues with PowerMac G5 Dual 2.0 Processor

    I've been having issues with my fans running super loud for no apparent reason. I have been told that my thermal meter for one of my processors has gone bad and that I have to replace both my processors at a cost of over $800! I am simply wondering i

  • [Oracle AS, Datasource Pool]: java.lang.NullPointerException (Soap)

    I found below error on my applications that are on Oracle AS (data source pool). After I killed some processes on Oracle Database. <SOAP-ENV:Fault> <faultcode>SOAP-ENV:Server.Exception:(/faultcode> <faultstring>java.lang.NullPointerException</faultst

  • SQL required

    Hi All I have a table called file_seq havign the following field ID, S_TIME,SEQ_NO. The sample is below: ID S_TIME SEQ_NO IJ_A1 01-AUG-10 1232 IJ_A2 01-AUG-10 1233 IJ_A3 01-AUG-10 1234 IJ_A4 01-AUG-10 1235 IJ_A5 01-AUG-10 1236 IJ_A6 01-AUG-10 1237 A1