Pick selected value from Jcombobox populated with Enum

Asked

Viewed 441 times

1

I have at my window one JComboBox with items from an Enum, and a JLabel to show the value of the selected item in the combo.

So far so good, I’m able to do this, only I’m a little unsure if I’m doing it the right way, because I don’t know if there’s a more efficient way to do it.

Event of the Jcombobox :

form.cmbPizza.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                if(form.cmbPizza.getSelectedItem() != null){
                    if(Pizzas.CALABRESA == (Pizzas) form.cmbPizza.getSelectedItem()){
                        form.lblPrecoPizza.setText("R$ " + Pizzas.CALABRESA.preco);
                    }
                }else{
                    form.lblPrecoPizza.setText("R$ 0,00");
                }
            }
        });

Enum :

        public enum Pizzas {

            CALABRESA("25,99"), MUSSARELA("25,99"), PALMITO("19,99"), PORTUGUESA("19,99"),
 CATUPIRY("25,99"), PROVOLONE("19,99"), LOMBO("29,99");

            public String preco;

            private Pizzas(String s) {
                preco = s; 
            }

        }

The point is that I have several items on Enum, hence my doubt, I will have to do what I just did with all the items or is there a less complicated and more efficient way to do this?

  • 1

    It’s not a duplicate of that? Perform actions from the selected item in Jcombobox

  • Oh truth ! It is that as the question is another, I thought I would not have the answer I wanted... Vlw !

  • You want a Generic way to display the Enum without having to switch is that right? If yes, it’s not duplicate.

  • Exactly, I was trying to change my code with some things you put in the other question, but I couldn’t get kkk

  • Add your Enum to the question, I’m about to answer but without seeing your Enum maybe the answer won’t answer.

  • To do what I want, I’ll use an Actionlistener or Itemlistener ?

  • See the answer below.

Show 2 more comments

1 answer

2


First add to your Enum a method that returns the price of the option:

public enum Pizzas {

    CALABRESA("25,99"), MUSSARELA("25,99"), PALMITO("19,99"), PORTUGUESA("19,99"),
    CATUPIRY("25,99"), PROVOLONE("19,99"), LOMBO("29,99");

    public String preco;

    private Pizzas(String s) {
        preco = s;
    }
    public String getValue(){
        return preco;
    }
}

As I replied in another question, just adapt the code from there to your Enum. And to monitor selection changes of JComboBox, one should use ItemStateChanged:

form.cmbPizza.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {

             if (e.getStateChange() == ItemEvent.SELECTED) {
                Pizzas p = (Pizzas) e.getItem();
                 jLabel1.setText("R$" + p.getValue());
            }
        });

See working:

inserir a descrição da imagem aqui

  • @Danielsantos yes, I forgot to change at the time of pasting here.

  • If I understand correctly, you are taking the name of the selected item and comparing if there is an equal name in the Enum list, this is it ?

  • In fact, the Enum objects are already loaded in the combobox, what the code does is to take the selected item and call the method that will bring the value loaded in preco.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.