Search code examples
javastringswingjcombobox

JComboBox Option Selected Not Imposing Change


In my code I have a simple JComboBox which is used to turn a certain feature on or off. The JComboBox code is:

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

The issue is that this always returns the value as being on regardless of when I click "Off." The JComboBox is called here: note that I have included the System.out.println()'s (and the check for null) just to check for output.

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.... :-/");
}

Any pointers would be greatly appreciated, genuinely baffled as to why this is occurring, no doubt a simple over sight.

Regards, Reg.

EDIT Here is the full exerpt of code

 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);
            }
        }
    });

Solution

  • Use .equals for comparing Strings, not ==

    i.e.

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

    Here might be another problem:

    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")) {
                    // ...
                }
        }
    }
    

    Place String option = combo.getSelectedItem().toString(); after you call JOptionPane.showConfirmDialog(...).