user2221125
user2221125

Reputation: 156

JComboBox selection to a variable

A pretty noobish question so it should be an easy answer for you. I have some code here:

     //Severity Row
     severity = new JLabel("Severity:");
     c.fill = GridBagConstraints.HORIZONTAL;
     c.gridx = 0;
     c.gridy = 4;
     c.gridwidth = 1;
     pane.add(severity, c);

     severityBox = new JComboBox(SEVERITY);
     c.fill = GridBagConstraints.HORIZONTAL;
     c.gridx = 1;
     c.gridy = 4;
     c.gridwidth = 1;
     pane.add(severityBox, c);
     severityBox.addActionListener(this);

The options for the user to choose in the JComboBox are: 'Critical', 'Major', and 'Minor'.

How do I get it so that if the user selects 'Major' from the ComboBox, I can have it print out "red" rather than using getSelectedItem() which prints out 'Major'?

Thank you in advance for the help!

Upvotes: 0

Views: 5437

Answers (2)

Michał Ziober
Michał Ziober

Reputation: 38635

OOP suggests to use specific object which represents "name of status" and "color of status". For example, this class could looks like this:

class Item {

    private String name;
    private String color;

    public Item(String name, String color) {
        this.name = name;
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return name;
    }
}

Now, you can build combobox using instances of above class. Please see my example:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;

public class SourceCodeProgram {

    public static void main(String argv[]) throws Exception {
        JComboBox<Item> comboBox = new JComboBox<Item>(new Item[] {
                new Item("Major", "red"), new Item("Critical", "dark"),
                new Item("Minor", "green") });
        comboBox.addActionListener(new ActionListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox<Item> comboBox = (JComboBox<Item>) e.getSource();
                Item item = (Item) comboBox.getSelectedItem();
                System.out.println(item.getColor());
            }
        });
        JFrame frame = new JFrame();
        frame.add(comboBox);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

As you can see, I bind color and name in one class. I create 3 instances of this class and pass it into JComboBox<Item> constructor.
We can use Map class for linking these properties but specific class is the best solution, I think.

Upvotes: 1

Vishal K
Vishal K

Reputation: 13066

Just change the value that you want to return :

private String sValue;
@Override
public void actionPerformed(ActionEvent evt)
{
  if (evt.getSource() == severityBox )
  {
     sValue = (String)severityBox.getSelectedItem();
     if ( "Major".equals(sValue))
     {
        sValue = "Red";
     }
    System.out.println(sValue);
  }
}

Upvotes: 1

Related Questions