user2587777
user2587777

Reputation:

Differentiating between selected items in a JComboBox

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

Upvotes: 0

Views: 196

Answers (3)

MrLore
MrLore

Reputation: 3780

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.

Upvotes: 0

tbodt
tbodt

Reputation: 17007

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.

Upvotes: 1

Eng.Fouad
Eng.Fouad

Reputation: 117655

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

Upvotes: 1

Related Questions