How to operate a combobox with an if

0

How can I use a comparative if with an item combo box, if I want to push a button I draw a geometric figure

    
asked by Oswaldo 20.05.2017 в 06:12
source

1 answer

1

To do that, you should first create a listener.

The listener will tell you that the user chose a combobox option, that is:

combo.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        comboActionPerformed(evt);
    }
});

Then, within the comboActionPerformed function, you can identify which option was selected and then make a decision.

In your case, I would recommend using a switch, if I select option 1, that I do a task, if I select option 2, another, and so on.

private void comboActionPerformed(java.awt.event.ActionEvent evt) {
    switch (combo.getSelectedIndex()) {
        case 0:
            // Dibujar rectangulo
            break;
        case 1:
            // Dibujar circulo
            break;
        case 2:
            // Dibujar línea
            break;
    }
}
    
answered by 20.05.2017 / 06:38
source