0

As you can see below, I have created a JComboBox with elements in the 'options' array as the options in the list.

When a specific item in the list is selected, I want to display the JLabels 'one' or 'two'. E.g. selecting option one displays 'one' and selecting option two displays 'two' and removes 'one' from the display, and vice versa.

I know how to add the JLabels, with add(); but I don't know how to differentiate between which item is selected.

I have searched online and looked at the java docs but I couldn't find a relevant solution.

Thanks

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.lang.*;
import java.util.*;

public class converterPage extends JFrame {
    private JComboBox box;
    private static String[] options = {"Option one", "Option two"};
    private JLabel one, two;

    public converterPage() {
        super("Convert Units"); 
        setLayout (null);

        box = new JComboBox(options);
        box.setBounds(10, 10, 150, 30);
        add(box);

        one = new JLabel("one");
        two = new JLabel("two");
        one.setBounds(170, 10, 150, 30);
        two.setBounds(170, 10, 150, 30);


        box.addItemListener(
            new ItemListener(){ 
                public void itemStateChanged(ItemEvent event){
                    // depending on what is selected, i want to only display certain stuff 
                }   
            }        
        );
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • [possible duplicate](http://stackoverflow.com/questions/58939/jcombobox-selection-change-listener) - [see this answer](http://stackoverflow.com/a/14424530/679982) it should answer your question. – linski Jul 25 '13 at 19:27
  • Don't use a null layout!!! – camickr Jul 25 '13 at 19:48

3 Answers3

1

One way is to use: JComboBox#getSelectedItem().

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
1

You can use:

event.getItem()

to obtain an Object representing the currently selected item. The object is actually a String, becuase the combo box was created with an array of Strings.

tbodt
  • 16,609
  • 6
  • 58
  • 83
0

I believe this is what you're after:

import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;

public class ConverterPage extends JFrame
{
    private JComboBox box;
    private final Map<String, JLabel> map = new HashMap<>();

    public ConverterPage()
    {
        super("Convert Units");
        super.setLayout(null);

        final JLabel one = new JLabel("one");
        final JLabel two = new JLabel("two");
        one.setBounds(170, 10, 150, 30);
        two.setBounds(170, 10, 150, 30);

        this.map.put("Option one", one);
        this.map.put("Option two", two);
        this.box = new JComboBox(this.map.keySet().toArray(new String[this.map.size()]));
        this.box.setBounds(10, 10, 150, 30);
        super.add(box);

        this.box.addItemListener(new ItemListener()
        {
            @Override
            public void itemStateChanged(ItemEvent event)
            {
                // depending on what is selected, i want to only display certain stuff 
                final JLabel label = map.get((String) event.getItemSelectable().getSelectedObjects()[0]);
                System.out.println(label.getText());
            }
        });
    }

    public static void main(String[] args)
    {
        final ConverterPage c = new ConverterPage();
        c.setSize(400, 400);
        c.setVisible(true);
    }
}

I took the liberty of cleaning up your imports (you never need to import from java.lang as it's automatically imported), and class names should really be in CapitalCase.

MrLore
  • 3,759
  • 2
  • 28
  • 36
  • This prints out the selected option, e.g. 'option one', twice. When I replaced the system.out.println with add, in order to display the JLabel, it returned an error. –  Jul 25 '13 at 19:36
  • @user2587777 It does it twice because there are 2 events: The first item is deselected and the second option is selected (as indicated by `event.getStateChange()`. What do you mean by `add`? What was the error message? If you mean you are wanting to link "Option One" to `JLabel one`, you're going to need a map, or a switch method. – MrLore Jul 25 '13 at 19:41
  • Yes I want JLabel one to display on the JFrame when "Option One" is selected, and same with JLabel two with "Option Two". –  Jul 25 '13 at 19:47
  • @user2587777 See edits. `label` is now the corresponding label, and you can do whatever it is you want with it from there. – MrLore Jul 25 '13 at 19:55