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?
It’s not a duplicate of that? Perform actions from the selected item in Jcombobox
– user28595
Oh truth ! It is that as the question is another, I thought I would not have the answer I wanted... Vlw !
– Daniel Santos
You want a Generic way to display the
Enum
without having to switch is that right? If yes, it’s not duplicate.– user28595
Exactly, I was trying to change my code with some things you put in the other question, but I couldn’t get kkk
– Daniel Santos
Add your Enum to the question, I’m about to answer but without seeing your Enum maybe the answer won’t answer.
– user28595
To do what I want, I’ll use an Actionlistener or Itemlistener ?
– Daniel Santos
See the answer below.
– user28595