2 Replies Latest reply: Oct 7, 2004 4:16 PM by 807587 RSS

    Single key operation on the showMessageDialog box

    807587
      Hi,

      Is there a way select (click) the 'Yes' or 'No' buttons by way of single key press like just pressing 'Y' or 'N' instead of 'alt Y' or 'alt N' respectively.

      Thanks in advance

      Suresh
        • 1. Re: Single key operation on the showMessageDialog box
          800382
          there should be... the button uses setMnemonic() to set that, which uses the alt-key combo to run.
          But you can probably use keyboard bindings on the button if you can get a reference to the buttons. To do that, you need to create the JOptionPane using the constructor, not the showXxx() methods, and use the createDialog() method to show it after you find the buttons.
          • 2. Re: Single key operation on the showMessageDialog box
            807587
            Good question... I actually learned somethings:
            import javax.swing.*;
            import javax.swing.event.*;
            import java.awt.*;
            import java.awt.event.*;
            import java.util.TreeSet;
            import javax.swing.plaf.*;
            public class OptionDemo extends JFrame
            {
                JTabbedPane tabbedPane;
                int tabCount;
                JButton addBtn;
                JButton removeBtn;
                JTextField textField;
            
                public OptionDemo()
                {
                    super("Test");
                    this.setDefaultCloseOperation( EXIT_ON_CLOSE );
                    setLocationRelativeTo( null );
                    setVisible(true);
                    int choice = JOptionPane.showConfirmDialog(this, "Test","Test",JOptionPane.YES_NO_OPTION );
                    if( choice == JOptionPane.YES_OPTION )
                    {
                        System.out.println("yes");
                    }
                    else if( choice == JOptionPane.NO_OPTION )
                    {
                        System.out.println("no");
                    }
                    else
                    {
                        System.out.println("canceled");
                    }
            
                }
            
                public static void main( String[] a )
                {
            
                    try
                    {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                        ActionMap map = (ActionMap)UIManager.get("OptionPane.actionMap");
                        if( map == null )
                        {
                            map = new ActionMap();
                            UIManager.put( "OptionPane.actionMap", map );
                        }
            
                        map.put("close", new CloseAction());
                        map.put( "yes", new YesAction() );
                        map.put( "no", new NoAction() );
                        UIManager.put( "OptionPane.windowBindings",
                                       new Object[]
                                       {
                                           "ESCAPE", "close",
                                           KeyStroke.getKeyStroke(KeyEvent.VK_Y , 0), "yes",
                                           KeyStroke.getKeyStroke(KeyEvent.VK_N , 0), "no",
                                           } );
                    }
                    catch( Exception e )
                    {
                        //TODO: Remove
                        e.printStackTrace();
                    }
                    new OptionDemo();
                }
            
                private void addTab()
                {
                    String title = "Tab "+tabCount++;
                    tabbedPane.addTab( title, new JLabel( title ) );
                }
            
                private static class YesAction extends AbstractAction
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        JOptionPane optionPane = (JOptionPane)e.getSource();
            
                        optionPane.setValue(new Integer(JOptionPane.YES_OPTION));
                        optionPane.setVisible( false );
            
                    }
                }
            
                private static class NoAction extends AbstractAction
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        JOptionPane optionPane = (JOptionPane)e.getSource();
            
                        optionPane.setValue(new Integer(JOptionPane.NO_OPTION));
                        optionPane.setVisible( false );
                    }
                }
            
            
                private static class CloseAction extends AbstractAction
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        JOptionPane optionPane = (JOptionPane)e.getSource();
            
                        optionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION));
                    }
                }
            }