【问题标题】:JComboBox Option Selected Not Imposing Change选择的 JComboBox 选项不影响更改
【发布时间】:2015-06-04 12:58:26
【问题描述】:

在我的代码中,我有一个简单的 JComboBox,用于打开或关闭某个功能。 JComboBox 代码为:

public static JComboBox getErrorLoggingOnOrOff(){
    String[] options = { "On", "Off" };
    JComboBox combo = new JComboBox(options);
    return combo;
}

问题在于,无论我何时单击“关闭”,它都会将值返回为开启状态。 JComboBox 在这里被调用:请注意,我已经包含了 System.out.println()(以及对 null 的检查)只是为了检查输出。

String option = combo.getSelectedItem().toString();
....
if(option.equals("On")){
    System.out.println("On selected");
    return;
}
else if(option.equals("Off")){
    System.out.println("Off selected");
    return;
}
else {
    System.out.println("Null value for option.... :-/");
}

任何指针都将不胜感激,对于为什么会发生这种情况确实感到困惑,无疑是一个简单的疏忽。

问候,Reg.

编辑 这是完整的代码摘录

 final JMenuItem enableErrorLogging = new JMenuItem("Enable Error Logging");
    enableErrorLogging.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.ALT_MASK));
    enableErrorLogging.setMnemonic(KeyEvent.VK_M);
    menu.add(enableErrorLogging);
    enableErrorLogging.addActionListener( new ActionListener() {

        public void actionPerformed(final ActionEvent e) {
            if (ui.getWorkspace().getCurrentProject() == null) {
                openProjectMessagePane();
                return;
            }

            JComboBox combo = getErrorLoggingOnOrOff();
            JPanel panel = new JPanel(new GridLayout(0, 1));
            panel.add(new JLabel("Turn Error Logging On Or Off"));
            panel.add(combo);
            String option = combo.getSelectedItem().toString();

            try{

            int r = JOptionPane.showConfirmDialog(frame, panel, "Enable Error Logging?",
                    JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE);
            if (r == JOptionPane.OK_OPTION) {
            if(option.equals("On")){                 
                System.out.println("On selected");
                return;
            }
            else if(option.equals("Off")){
                System.out.println("Off selected");
                return;
            }
            else{
                System.out.println("Null value for option.... :-/");
            }
                }
            } catch (Exception ex) {
                LOGGER.debug(ex);
            }
        }
    });

【问题讨论】:

  • 能否提供可运行版本的代码,还是太长了?
  • 可能太长了...我会看看其他可能导致问题的摘录!
  • @bakalolo 把这个操作的所有代码放在那里,忽略我之前的评论。顺便感谢您的关注。
  • 将String option = combo.getSelectedItem().toString(); 放在您的if (r == JOptionPane.OK_OPTION) 之后。我认为您在选择任何内容之前就获得了“selectedItem”。

标签: java string swing jcombobox


【解决方案1】:

使用.equals 比较字符串,不是 ==

即

if(option.equals("On")) {
    // ...
}
else if(option.equals("Off")) {
    // ...
}

这可能是另一个问题:

JComboBox combo = getErrorLoggingOnOrOff();
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(new JLabel("Turn Error Logging On Or Off"));
panel.add(combo);

// Here, you seem to be getting the 'selected item' before even
// showing it using a JOptionPane, which I believe will be null by default
String option = combo.getSelectedItem().toString();

try {
    int r = JOptionPane.showConfirmDialog(frame, panel, "Enable Error Logging?",
        JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE);
    if (r == JOptionPane.OK_OPTION) {
            if(option.equals("On")) {
                // ...
            }
    }
}

在您致电JOptionPane.showConfirmDialog(...) 之后放置String option = combo.getSelectedItem().toString();。

【讨论】:

  • @Reginald 是的,很确定就是这样。
  • 没有解决问题...但是,我将编辑问题以显示此更改。
  • @Reginald 如果您将errorLoggingStatus 与任何其他字符串进行比较,您也需要这样做。
  • @Reginald 如果这不能解决您的问题,也许问题出在您代码的其他部分?添加一些调试文本以检查流程是否正确?
  • @Reginald 更新答案
猜你喜欢
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-07
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多