Question

I'm trying to build a form where one can fill in own's own values into a JTextField or rely a preset option which is depending on a selection from a JComboBox.

This is the JCombobox

    String[] areas = new String [] {"Own Specifications", "SurveySample", "UK", "London", "Surrey"};

    @SuppressWarnings({ "unchecked", "rawtypes" })
    final JComboBox<String> selectedArea = new JComboBox(areas);
    //selectedArea = new JComboBox<String>();
    selectedArea.setModel(new DefaultComboBoxModel<String>(areas));
    selectedArea.setBounds(282, 52, 164, 27);

    contentPane.add(selectedArea);

And this is the JTextField

    tenurePrivateRenters = new JTextField();
    tenurePrivateRenters.setHorizontalAlignment(SwingConstants.CENTER);
    tenurePrivateRenters.setText("Private Renters");
    tenurePrivateRenters.setBounds(58, 213, 134, 28);
    contentPane.add(tenurePrivateRenters);

Depending on the JComboBox Selection of the user, in a JTextField, the value is supposed to change, e.g. if Survey Sample is selected the JTextField should chance its value to 10.

I've tried the two following option:

    selectedArea.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            // TODO Auto-generated method stub
            Object selectedValue = selectedArea.getSelectedItem();
            if(selectedValue.equals("Own Specifications")){
                 tenurePrivateRenters.setText("10");
                 System.out.println("Good choice!");    
            }
        }
    });

and

selectedArea.addActionListener(new ActionListener() {
        public void actionPerformed (ActionEvent e){
            @SuppressWarnings("unchecked")
            JComboBox<String> selectedArea = (JComboBox<String>) e.getSource();
            String selectedItem = (String) selectedArea.getSelectedItem();  
            if(selectedItem.equals("Own Specifications")){
             tenurePrivateRenters.setText("10");
             System.out.println("Good choice!");
            }
        }
    }
    );

}

But for both options nothing happens and the value of the JTextField remains on "Private Renters". Any idea's on where I'm going wrong?

Was it helpful?

Solution

In your itemStateChanged method, you have the following:

Object selectedValue = selectedArea.getSelectedItem();

The getSelectedItem method returns an Object. Then, you call that Object's equals method:

if(selectedValue.equals("Own Specifications")){

This will certainly always return false because the Object equals method is comparing an object of type String to an object of type Object.

Instead, if you want to compare selectedValue to a String:

String selectedValue = (String)selectedArea.getSelectedItem();

Then, the if statement should work as expected.

OTHER TIPS

I have tried your code and it worked perfectly. Are you sure you are properly attaching those listeners to combobox BEFORE you try to change its value? Try to attach them right in the constructor to be sure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top