2012-07-27 9 views
5

So habe ich eine Anforderung, dass basierend auf der Auswahl eines Elements aus einer JComboBox, muss ich den Benutzer mit einem Auswahl bestätigen Dialog präsentieren. Was ich getan habe, war ein ItemListener hinzufügen und basierend auf einer bestimmten Logik, öffne ich dieses Dialogfeld.JComboBox: Verhalten bei ItemStateChange

Das eher nörgelnde Problem, dass ich konfrontiert bin, ist, dass das Dialogfeld Pop-up zuerst (auch während die ComboBox Elementauswahl geöffnet ist) und ich muss zweimal klicken, um meine Auswahl zu bestätigen. Das erste ist das Schließen des ComboBox-Popups und das zweite ist das aktuelle im Bestätigungsdialog.

Hier ist ein SSCCE Hervorhebung mein 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 
} 

jemand kann mir sagen, was mache ich falsch? Ich möchte, dass der Bestätigungsdialog erscheint, sobald der Benutzer seine Auswahl getroffen hat und das ComboBox-Popup geschlossen wird.

+2

ich über diese Idee der sofortigen Bestätigung vergessen würde. Es macht die Anwendung * sehr * ärgerlich, wenn Sie die Popup-Auswahl über die Tastatur ändern: Jedes Mal, wenn Sie die Pfeiltaste nach oben oder unten drücken, um durch die Optionen zu blättern, wird das Bestätigungsdialogfeld angezeigt. Stellen Sie sich vor, dass Sie nach einer Bestätigung gefragt werden, wenn eine der OK-Tasten gedrückt oder zumindest der Fokus verloren ist. –

+0

@JBNizet - Danke für den Rat. Eigentlich macht das mehr Sinn, ich habe Änderungen über die Tastatur nicht berücksichtigt. Ich denke, ich werde mit einer Bestätigung gehen, die auf dem verlorenen Fokus basiert. – Sujay

Antwort

6

ich tun würde, dass durch eine geeignete Methode aufgerufen:

else { 
      selectCombo.setPopupVisible(false); 
      JOptionPane.showConfirmDialog(this, "Do you want to change option to - " + selectedItem 
        + " ?", "Confirm Option Selection", JOptionPane.YES_NO_OPTION); 
     } 
+0

Das funktioniert für mich! Danke für Ihre Hilfe :) – Sujay