I am having a problem with a JComboBox
and a JOptionPane
dialog.
The situation is this:
- It needs to run from a Dialog, not a Frame
- Before data is set no check needs to be done
- After data is set changes need to be validated
I have a JComboBox with initial data in it and I have an ActionListener
which, on selection, displays a JOptionPane
dialog with a warning about revalidating.
When a selection is made the dialog appears as normal but the JComboBox
doesn't finish it's cycle and keeps the JComboBox
open until the dialog is clicked and then you need to do a further click to choose an option (regardless whether the first click is on top of the button).
import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DialogFocus extends JDialog {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JComboBox jComboBox = null;
public static void main(String[] args) {
new DialogFocus(null);
}
/**
* @param owner
*/
public DialogFocus(Frame owner) {
super(owner);
initialize();
setLookAndFeel();
this.setVisible(true);
}
public void setLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJComboBox(), BorderLayout.CENTER);
}
return jContentPane;
}
/**
* This method initializes jComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox() {
if (jComboBox == null) {
String[] items = {"1", "2", "3", "4", "5"};
jComboBox = new JComboBox(items);
jComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
showDialog();
}
});
}
return jComboBox;
}
protected void showDialog() {
JOptionPane.showMessageDialog(this, "Selection made");
}
}
The code I am using is quite complex so I have thrown together a basic example of code here. With the look and feel set it required 2 clicks to click on a valid option but without it it required a single click. (Comment out setLookAndFeel()
in the constructor).
Does anyone have any ideas how to fix this to make sure it only requires 1 click regardless of the look and feel?
Thanks in advance