gpt4 book ai didi

java - JComboBox:ItemStateChange 上的行为

转载 作者:搜寻专家 更新时间:2023-10-31 19:57:03 25 4
gpt4 key购买 nike

所以我有一个要求,基于从 JComboBox 中选择一个项目,我需要向用户显示一个选择确认对话框。我所做的是添加一个ItemListener,然后根据一定的逻辑,弹出这个对话框。

我面临的相当棘手的问题是对话框首先弹出(即使 ComboBox 项目选择处于打开状态),我必须单击两次以确认我的选择。第一个是关闭 ComboBox 弹出窗口,第二个是确认对话框中的实际那个。

这是一个强调我的问题的 SSCCE:

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
}

有人能告诉我我做错了什么吗?我希望在用户选择完他的选项并且 ComboBox 弹出窗口关闭后弹出确认对话框。

最佳答案

我会通过调用适当的方法来做到这一点:

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

关于java - JComboBox:ItemStateChange 上的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11694085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com