Just perform the desired action within the itemStateChanged
:
seuJCombo.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
//
if (e.getStateChange() == ItemEvent.SELECTED) {
//ação aqui
}
}
});
Basically you are creating a listener
that will be monitoring selection changes in JComboBox
, and when there is a change, if
checks if the item's status is "selected".
Once you confirm that the item has been selected, simply handle the selected option using e.getItem()
":
seuJCombo.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
//
if (e.getStateChange() == ItemEvent.SELECTED) {
//pegando o texto do item selecionado
String valorSelecionado = e.getItem().toString();
if(valorSelecionado.equals("ativo")){
//altere aqui quando ativo selecionado
}else{
//altere aqui quando inativo selecionado
}
}
});
Note: e.getItem()
returns a type Object
, so it is necessary to cast
for the expected type in JComboBox
, in the case of
For example, only the text of the selected option is expected,
made cast
to String
.