how to set the default selected option as NO_OPTION in a JoptionPane

This is just an example program to create the same scenario in the project.When clicked on OK button, it will pop up a JOptionPane confirm dialog with YES-NO buttons.I want to set the default selected button as NO button. In the below program, the keyboard focus is set on NO button, but in the look and feel the selection is still YES button. Can anyone suggest a solution to make both the focus on NO button?

public class Test extends JPanel implements ActionListener{

Test() {

JButton btn = new JButton("OK");

this.add(btn);

btn.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

Object[] o = {"Yes", "No"};

JOptionPane p = new JOptionPane("Are u sure?",JOptionPane.QUESTION_MESSAGE,JOptionPane.YES_OPTION,null,o,null);

//p.setWantsInput(false);

p.setInitialSelectionValue(o[1]);

try {

UIManager.setLookAndFeel(new MetalLookAndFeel());

} catch (UnsupportedLookAndFeelException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

//p.setFocusCycleRoot(false);

p.setInitialValue(o[1]);

//p.getRootPane().getDefaultButton().requestFocusInWindow();

UIManager.put("Button.defaultButtonFollowsFocus", Boolean.FALSE);

p.createDialog(this, "").setVisible(true);

if(p.getValue() == o[0]) {

System.out.println("yes");

}else if(p.getValue() == o[1]) {

System.out.println("no");

}

//p.setWantsInput(true);

//p.showConfirmDialog(this, "Are u sure?");

//p.setInitialSelectionValue(new Integer(JOptionPane.NO_OPTION));

}

public static void main(String[] args) {

Test t = new Test();

JFrame f = new JFrame();

f.getContentPane().add(t);

f.pack();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setSize(500, 500);

f.setVisible(true);

}

[1906 byte] By [SSVNa] at [2007-10-3 10:30:29]
# 1

not sure what you're trying to do, but try this

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Test extends JPanel implements ActionListener

{

Test()

{

JButton btn = new JButton("OK");

this.add(btn);

btn.addActionListener(this);

}

public void actionPerformed(ActionEvent e)

{

Object[] o = {"Yes", "No"};

int value = JOptionPane.showOptionDialog(null,"Are u sure?","",-1,-1,null,o,o[1]);

if(value == 0) System.out.println("yes");

else if(value == 1) System.out.println("no");

}

public static void main(String[] args)

{

UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);

Test t = new Test();

JFrame f = new JFrame();

f.getContentPane().add(t);

f.pack();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setSize(500, 500);

f.setVisible(true);

}

}

Michael_Dunna at 2007-7-15 5:53:10 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks. This is working. But when I tab out to YES button and then press Enter, it selects NO button even though L&F it's showing YES. Is there a way to fix this?Message was edited by: SSVN
SSVNa at 2007-7-15 5:53:10 > top of Java-index,Desktop,Core GUI APIs...
# 3
works OK for me.clicking OK brings up the dialog, hit enter, 'no' is printedOK again for dialogtab, so that yes has focushit enter, 'yes' is printedwinxp java1.5.0_05
Michael_Dunna at 2007-7-15 5:53:10 > top of Java-index,Desktop,Core GUI APIs...
# 4
Sorry, it's working.Actually I did not add UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);.That's why it was not working.Thanks a lot.
SSVNa at 2007-7-15 5:53:10 > top of Java-index,Desktop,Core GUI APIs...