Quindi ho un requisito che in base alla selezione di un elemento da un JComboBox, ho bisogno di presentare l'utente con una finestra di dialogo di selezione. Quello che ho fatto è stato aggiungere un ItemListener
e in base a una determinata logica, ho visualizzato questa finestra di dialogo.JComboBox: comportamento su ItemStateChange
Il problema piuttosto fastidioso che sto affrontando è che la finestra di dialogo si apre per prima (anche mentre la selezione di elementi ComboBox è aperta) e devo cliccare due volte per confermare la mia selezione. Il primo è quello di chiudere il popup di ComboBox e il secondo è quello attuale nella finestra di conferma.
Ecco uno SSCCE evidenziando il mio problema:
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
}
qualcuno può dirmi che cosa sto facendo di sbagliato? Voglio che la finestra di conferma compaia quando l'utente ha finito di selezionare la sua opzione e il popup di ComboBox si chiude.
vorrei dimenticare questa idea di conferma immediata. Rende l'applicazione * molto * fastidiosa quando si usa la tastiera per modificare la selezione popup: ogni volta che si preme il tasto freccia Su o Giù per scorrere tra le opzioni, viene visualizzato il popup di conferma. Prendi in considerazione la possibilità di chiedere conferma quando viene premuto un pulsante OK o almeno in caso di messa a fuoco persa. –
@ JBNizet - Grazie per il consiglio. In realtà questo ha più senso, non ho preso in considerazione i cambiamenti usando la tastiera. Penso che andrò con una conferma basata sul focus perso. – Sujay