4
I am with the following doubt, I have a combobox, and I want that when you click on one of the combo options, it does something. I wanted to know how I put a variable or method "linked" to combo options, for example if I clicked on option 1, it lists me something, if it is in 2, it just issued a Joption pane (just an example).
package teste;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Exemplo extends JFrame
{
private String t;
public Exemplo()
{
setTitle("Exemplo combo");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel jp = new JPanel();
jp.setLayout(null);
add(jp);
JComboBox combo = new JComboBox();
combo.addItem("Opção 1");
combo.addItem("Opção 2");
jp.add(combo);
combo.setBounds(180, 200, 125, 30);
}
public static void main(String[] args)
{
Exemplo ex = new Exemplo();
ex.setVisible(true);
}
}
Sorry, I still don’t understand very well, how you will differentiate different options there ?
– Java
@Java taking the selected item and making a condition, type
if(item == algumacoisa){//faz isso}else{ //faz aquilo}
– user28595
Got it, thanks to the two of you !
– Java
@Java See the answer now as I added more details.
– Murillo Goulart
@Java but for something simple, there is no need to create a class separately, you can use class anonima http://pastebin.com/6eTkYZ1c
– user28595
Right, and thank you again !
– Java