So I've a requirement that based on the selection of an item from a JComboBox, I need to present the user with a selection confirm dialog. What I did was add an ItemListener
and based on a certain logic, I pop up this dialog box.
The rather nagging problem that I'm facing is that the dialog box pop's up first (even while the ComboBox item selection is open) and I've to click twice to confirm my selection. The 1st one is to close the ComboBox popup and the 2nd one is the actual one on the confirmation dialog.
Here's an SSCCE highlighting my problem:
import java.awt.event.ItemEvent;
import javax.swing.JOptionPane;
public class TestFrame extends javax.swing.JFrame {
/**
* Creates new form TestFrame
*/
public TestFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
selectCombo = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Select Option");
selectCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Option 1", "Option 2", "Option 3", "Option 4" }));
selectCombo.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
selectComboItemStateChanged(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(selectCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(168, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(22, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(selectCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(19, 19, 19))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void selectComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_selectComboItemStateChanged
if (evt.getStateChange() == ItemEvent.SELECTED) {
String selectedItem = (String) selectCombo.getSelectedItem();
if (selectedItem == null || selectedItem.equals("Option 1")) {
return;
} else {
JOptionPane.showConfirmDialog(this, "Do you want to change option to - " + selectedItem + " ?", "Confirm Option Selection", JOptionPane.YES_NO_OPTION);
}
}
}//GEN-LAST:event_selectComboItemStateChanged
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JComboBox selectCombo;
// End of variables declaration//GEN-END:variables
}
Can someone tell me what am I doing wrong? I want the confirm dialog to pop up once the user has done selecting his option and the ComboBox popup closes.
I'd do that by invoking appropriate method:
else {
selectCombo.setPopupVisible(false);
JOptionPane.showConfirmDialog(this, "Do you want to change option to - " + selectedItem
+ " ?", "Confirm Option Selection", JOptionPane.YES_NO_OPTION);
}