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.

Similar Messages

  • How to focus a button

    Hi all,
    Sorry about this stupid question, but really get confused now.
    I?ve got an Applet and i want to focus a button when it starts! How can i do this?
    thx

    Don't know if anyone is looking at this, but I went through a coupla days work, so I thot I would post. I was only able to get it working in IE. This solves the problem of initially not having focus on the applet, setting initial focus to a component and resetting the focus when switching back & forth betweens apps. First the HTML<SCRIPT LANGUAGE="JavaScript">
    <!--
    function setFocus() {
      hide.hideField.focus();
      document.TestApplet.setFocus();
    // -->
    </SCRIPT>
    <form id=hide>
    <INPUT style="border: 0px;" ReadOnly id=hideField maxLength=0 name="hideField" type=text notab>
    </form>
    </HEAD>
    <BODY onFocus="setFocus()" onLoad="setFocus()">
    <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
    //removed some
    <PARAM NAME="scriptable" VALUE="true">I have no idea how this is going to post. This calls the JApplets setFocus method on loading and on focus. Make sure scriptable="true". Now the JApplet.public class Applet1 extends JApplet {
      public void start() { Focus.setFocus(jButton2); }
      public void init() {
        getContentPane().addContainerListener(Focus.cl);
      public void setFocus() { Focus.setFocus(); }
      static class Focus {
        private static Component lastFocus;
        public static ContainerListener cl = new ContainerAdapter() {
          public void componentAdded(ContainerEvent ce) {
         Component child = ce.getChild();
         addlisteners(child);
        private static void addlisteners(Component c) {
          c.addFocusListener(fl);
          if(c instanceof Container) {
         Container ct = (Container)c;
         ct.addContainerListener(cl);
         for(int i=0; i<ct.getComponentCount(); i++) {
           addlisteners((Component)ct.getComponent(i));
        public static FocusListener fl = new FocusAdapter() {
          public void focusGained(FocusEvent fe) {
         lastFocus = (Component)fe.getSource();
        public static void setFocus() { setFocus(lastFocus); }
        public static void setFocus(final Component comp) {
          if (comp != null) {
         SwingUtilities.invokeLater(new Runnable() {
           public void run() { comp.requestFocus(); }
    }I tried to make it compatible with AWT, but haven't tested it too much. Focus might also be made into it's own class, instead of an inner class so that it can be called from anywhere (after dialogs???).

  • JOptionPane.showConfirmDialog  "NO" button acting as "YES" on MAC using TAB

    JOptionPane.showConfirmDialog "NO" button acting as "YES" on MAC using TAB

    Ya , I am asking question ? Why "NO" is acting as "YES" button in case of JOptionPane.showConfirmDialog on MAC using TAB.
    i.e,
    I clicked on some button, then showConfirmDialog is opened, By default the focus is on "YES" button. Now I pressed TAB button and changed to the focus to "NO" button.
    If press ENTER key --> acting "YES"
    If press SPACE key -> acting as "NO"
    Why ?
    What should I do to make it correct ?
    Am I need mention set (Key,value) pairs for UIManager in case of MAC.
    Please suggest ?

  • JOptionPane.showConfirmDialog()+grab focus

    Hi
    How do I get the cursor blinking in the textfield of JOptionPane.showConfirmDialog() by default when my code executes JOptionPane.showConfirmDialog() ?
    Thanks
    apoorva

    Hi Apoorva,
    Here I am giving you working code. This cocmpletely solves your problem.
    import java.awt.Rectangle;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.SwingUtilities;
    public class test extends JFrame {
         private static final long serialVersionUID = 1L;
         private JPanel jContentPane = null;
         private JButton jButton = null;
         private JPasswordField tPasswordField;
          * This method initializes jButton     
          * @return javax.swing.JButton     
         private JButton getJButton() {
              if (jButton == null) {
                   jButton = new JButton();
                   jButton.setBounds(new Rectangle(85, 121, 104, 27));
                   jButton.setText("Click");
                   jButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent e) {
                             System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
    //                         JPasswordField tPasswordField = new JPasswordField(
                             tPasswordField = new JPasswordField(10);
                             tPasswordField.setEchoChar('*');
                             SwingUtilities.invokeLater(new Runnable()
                                public void run()
                                       tPasswordField.requestFocus();
                             int ret=JOptionPane.showConfirmDialog(test.this,
                                       tPasswordField,
                                       "Enter server password",
                                       JOptionPane.OK_OPTION);
              return jButton;
          * @param args
         public static void main(String[] args) {
              // TODO Auto-generated method stub
              SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                        test thisClass = new test();
                        thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        thisClass.setVisible(true);
          * This is the default constructor
         public test() {
              super();
              initialize();
          * This method initializes this
          * @return void
         private void initialize() {
              this.setSize(300, 200);
              this.setContentPane(getJContentPane());
              this.setTitle("JFrame");
          * This method initializes jContentPane
          * @return javax.swing.JPanel
         private JPanel getJContentPane() {
              if (jContentPane == null) {
                   jContentPane = new JPanel();
                   jContentPane.setLayout(null);
                   jContentPane.add(getJButton(), null);
              return jContentPane;
    }Now u can mark this as fixed and assign duke stars.. :-)

  • JOptionPane.showConfirmDialog: Tabbing between options creates wrong result

    In JOptionPane.showConfirmDialog, when pressing <tab> to move between options and then pressing <Enter> I always get a returned zero (0) no matter which button is in focus. Is this a (known) bug or am I stupid?
    I run the following code.
    import javax.swing.*;
    public class TabOptions
    public static void main (String [] args)
         int n = JOptionPane.showConfirmDialog (null, "Use <Tab> to shift option-button, then press <Enter>. " +
                             "I'll write the returned value to sdt out.");
         System.out.println ("Returned value: " + n);
    }

    import javax.swing.*;
    class TabOptions
    public static void main (String [] args)
    UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);//<--------------------
    int n = JOptionPane.showConfirmDialog (null, "Use <Tab> to shift option-button, then press <Enter>. " +
    "I'll write the returned value to sdt out.");
    System.out.println ("Returned value: " + n);
    }

  • JOptionPane.showConfirmDialog in Russian

    I have an application that supports many languages including Russian. All other texts are displayed in Russian language correctly. However, I have an issue with JOptionPane.showConfirmDialog() in Russian language. This issue does not occur in ZH or ES. Does anyone know why the JOptionPane.showConfirmDialog() show the Yes/No button in English while the locale is in Russian? How can I fix this issue?
    Thanks!

    Does anyone know why the JOptionPane.showConfirmDialog() show the Yes/No button in English while the locale is in Russian?Tried to track it down, but gave up. So I (still) have no idea.
    How can I fix this issue?Here's one way:import javax.swing.JOptionPane;
    import javax.swing.UIManager;
    public class RussianDialog {
      public static void main(String[] args) {
        UIManager.put("OptionPane.yesButtonText", "&#1044;&#1072;");
        UIManager.put("OptionPane.noButtonText", "&#1053;&#1077;&#1090;");
        UIManager.put("OptionPane.cancelButtonText", "&#1086;&#1090;&#1084;&#1077;&#1085;&#1080;&#1090;&#1100;");
        JOptionPane.showConfirmDialog(null,
                "&#1088;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;", "&#1088;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;".toUpperCase(),
                JOptionPane.YES_NO_CANCEL_OPTION);
    }Note that you will need to compile usingjavac -encoding UTF8 RussianDialog.javaif your default encoding doesn't support the Russian characters.
    db

  • JOptionPane.showConfirmDialog requires two key strokes with Java SE 6.

    I have been using the following line of code:
    rc = JOptionPane.showConfirmDialog( comp, objects, "Warning",
    JOptionPane.YES_NO_OPTION,
    JOptionPane.INFORMATION_MESSAGE);
    with no problems with plug-in 1.4 and 1.5. Now with 1.6, when I choose Yes/No/Cancel, the first time I press the button nothing happens, the second time I press the button the action takes place.
    I have been searching for information on this and have not found any known issues (yet). Does anyone have any ideas?

    further investigation shows that this only happens when the component is a JComboBox. I found a new bug that seems to be the same issue:
    http://bugs.sun.com./bugdatabase/view_bug.do?bug_id=6817939
    When calling JOptionPane.showConfirmDialog from an ActionListener called by a JComboBox, there is 2 issues:
    * the focus is not given to the confirm dialog

  • 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

  • Newbie JOptionPane.showConfirmDialog ?

    Hey guys I'm sorry to be asking such an easy question but in the example code how do I capture the user input (click button)
    JOptionPane.showConfirmDialog(null, "Select one", "Testing", JOptionPane.YES_NO_OPTION);
    I need to know how to gather which button they pressed yes or no. Thanks ahead of time.

    if ( JOptionPane.showConfirmDialog(........) == JOptionPane.YES_OPTION)
    Jean-Francois Gilbert

  • JOptionPane.showConfirmDialog() displays weird msg

    I use showConfirmDialog() to request whether or not the program should continue when SUBMIT button is pressed. The first time the button is pressed there are 2 messages displayed in the dialog box: a fake one followed by the real one. This behaviour is not observed if the button is pressed a second time.
    This is only noticeable if I connect to the server where it runs using X windows. The same code run on either a LINUX PC or connecting to server using telnet doesn't show this behaviour.
    Partial code
    public void actionPerformed(ActionEvent ae) {
    String msg = new String("Do you really want to submit this list?");
    String dtitle = new String("Pls. choose one :)");
    int msgType = JOptionPane.QUESTION_MESSAGE;
    int optionType = JOptionPane.YES_NO_OPTION;
    if (ae.getSource() instanceof JButton && ae.getActionCommand().equals("SUBMIT")) {
    jbtn.setBackground(Color.red);
    // Confirm dialog
    int r = JOptionPane.showConfirmDialog((Component)this,msg,dtitle,optionType,msgType,null);
    Thks

    Hi m8, thanks got that working......sometimes you just cant see whats stareing you in the face!!
    Just one more thing...i have used your code to display the confirmdialog...but is there any way i
    can set the default focus to the textfield when the JOptionPane is made?
    Thanks again

  • JOptionPane.showConfirmDialog

    Hi there,
    I have a confirm dialog that i want to close whenever the [x] is clicked in the top left of the window, but i just keep getting a error.
    I have made a custom confirm dialog that i have added a object to that allows user input.
    I had to do this since I couldnt find out if the OK or Cancel button was pressed on the default one!.
    CODE so FAR....
    Object[] msg = {
    "Please Enter a valid Guess (3-27)", textField};
    result = JOptionPane.showConfirmDialog(null, msg, title,
    JOptionPane.OK_CANCEL_OPTION,
    JOptionPane.PLAIN_MESSAGE);

    Hi m8, thanks got that working......sometimes you just cant see whats stareing you in the face!!
    Just one more thing...i have used your code to display the confirmdialog...but is there any way i
    can set the default focus to the textfield when the JOptionPane is made?
    Thanks again

  • JOptionPane.showConfirmDialog in servlets

    Hi
    I am trying to show a dialog in a doPost method of a servlet. For some reason, it blocks the entire method and does not show the dialog. What gets me voiceless is that it works in the doGet method of the servlet.
    The situation is the following:
    - a first serlvet dispatches GET requests to other servlets according to the role of users.
    - in the doGet of this first servlet, a second (servlet2) is instantiated and its doGet is called;
    - in the doGet of servlet2, a HTML form (POST method) is generated;
    - when the submit button of the HTML form is pressed, the form is processed by the doPost of servlet2.
    JOptionPane.showConfirmDialog works in servlet2 only in the doGet and not in the doPost. Their code is:
    //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    DataModule1 dm = (DataModule1) ixPageProducer1.getSessionDataModule(request.getSession());
    dm.parameterRow1.setString("empl_ini", request.getRemoteUser());
    dm.queryDataSet1.refresh();
    empl_id = dm.queryDataSet1.getInt("number");
    ixPageProducer1.servletGet(this, request, response);
    //Process the HTTP Post request
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    DataModule1 dm = (DataModule1) ixPageProducer1.getSessionDataModule(request.getSession());
    dm.queryDataSetReq.insertRow(false);
    ixPageProducer1.servletPost(this, request, response);
    JOptionPane.showConfirmDialog(null, "test" , "Request Confirmation", JOptionPane.YES_NO_OPTION);
    (I am using JBuilder and its InternetBeans to link the HTML pages with the DB).
    Many thanks in advance for helping a desperate Java beginner!!!

    I figured out that the dialog was actually generated, but "behind" my browser.You can try this to add the following either before or after the JOptionPane.showConfirmDialog:
    JOptionPane.requestFocus();
    But I think your best bet is to associate a frame with your dialog box as follows:
    Object parent = this.getParent();
    while (!(parent instanceof Frame)) parent = ((Component)parent).getParent();
    F = (Frame)parent;
    JOptionPane.showConfirmDialog(F, "test" , "Request Confirmation", JOptionPane.YES_NO_OPTION);
    If this helps, don't forget the Dukes
    ;o)
    V.V.

  • Can we add scrollbar to JOptionPane.showConfirmDialog()

    Hi,
    Can we add a scroll bar to JOptionPane.showConfirmDialog() so that the contents(messages) which are to be displayed are present in scrollbar so that the buttons will be visible to the user like OK, CANCEL.
    Currently when the message is large the buttons will not be visble in the screen.
    So, it is possible ? Or there is any other alternative for the same.
    Thanks.

    Is there another library that I have to import? I recieve this error when I try to compile...
    isPrime.java:53: cannot find symbol
    symbol : variable sb
    location: class isPrime
         JTextArea area = new JTextArea(sb.toString() );
    I have javax.swing.* imported as of right now...
         JTextArea area = new JTextArea(sb.toString());
          area.setRows(25);
          area.setColumns(100);
          area.setLineWrap(true);
          JScrollPane pane = new JScrollPane(area);
           JOptionPane.showConfirmDialog(null,
         pane,
           "Test", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    /*         JOptionPane.showMessageDialog(null,
               printArray(array),
         "Thanks for using Reverse Array", JOptionPane.INFORMATION_MESSAGE);
    */       the comment text is what I eventually wish to print((I simply hid it from the compiler so I could play w/ the code you provided)... it is the first 'n' number of primes up to which I'd like to be able to scroll through for large prints over 100 primes.
    thx for the help

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

  • ActionListener for JOptionPane.showConfirmDialog

    Is it possible to handle JOptionPane.showConfirmDailog 's button click events through action listeners?
    I want to execute some code on the "Save" button click and want to do it like my JFileChooser window stays in the background until either of the Yes,No or Cancel buttons of the showConfirmDialog is pressed.In fact I'm able to get the state of the button pressed using,
    int i=JOptionPane.showConfirmDialog(null,"Confirm file overwrite?")but can't make the showConfirmDialog window stay in the background,,,
    Can anybody please help?
    Thanks in advance...

    There are forums specifically for GUI questions.
    I believe there is also a forum for international issues.

Maybe you are looking for