user3633026
user3633026

Reputation: 15

Java ComboBox returns -1/null

I've used a ComboBox in my program (Example is below) which has various choices. If I want to read out the ComboBox it returns -1 for the Index and "null" for the item. Did somebody else had that problem and could help me?

Thank in advance!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class TestBox extends JFrame {
  // start attributes
  private JComboBox jComboBox1 = new JComboBox();
  private DefaultComboBoxModel jComboBox1Model = new DefaultComboBoxModel();
  // end attributes

  public TestBox(String title) { 
    // frame-initialization
    super(title);
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    int frameWidth = 300; 
    int frameHeight = 300;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2;
    setLocation(x, y);
    setResizable(false);
    Container cp = getContentPane();
    cp.setLayout(null);



// start components
    String[] list = {"1", "2", "3"};
    jComboBox1.setModel(jComboBox1Model);
    JComboBox jComboBox1 = new JComboBox(list);
    jComboBox1.setBounds(24, 40, 150, 20);
    jComboBox1.addItemListener(new ItemHandler());
    add(jComboBox1);
    // end components   
    setVisible(true);
  } // end of public TestBox  

  // start methods
  private class ItemHandler implements ItemListener{
    @Override
    public void itemStateChanged(ItemEvent e) {
      System.out.println("Changed Index to: " + jComboBox1.getSelectedIndex());
    }    
  }  
  // end methods 
  public static void main(String[] args) {
    new TestBox("TestBox");
  } // end of main  
} // end of class TestBox

Upvotes: 1

Views: 506

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You're shadowing the jComboBox1 variable by re-declaring it in the constructor. This means that the variable in the class is not the same as the object that is displayed.

Here:

public TestBox(String title) { 
    super(title);
    // ....

    JComboBox jComboBox1 = new JComboBox(list);  // *****
    // ...
}

Solution: don't re-declare the variable!

public class Foo {
   private JTextField bar;

   public Foo() {
     JTextField bar = new JTextField(10);  // re-declaring bar here
   }
}

do:

public class Foo {
   private JTextField bar;

   public Foo() {
     bar = new JTextField(10);  // ** see the difference? **
   }
}

Also

While your gut instinct may be to use a null layout and calling setBounds(...) on your components to place them with absolute positioning, but I'm going suggest that you not do this as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.

Upvotes: 3

Related Questions