Boa noite pessoal,
Estava aqui brincando com o SWING e algo me chamou atenção enquanto estava desenvolvendo a aplicação.
Pra não enrolar demais fiz um exemplo prático pra entender mais rapidamente.
Adiantando, toda vez que eu dou o set em algum componente, tanto faz o que, alterar nome, setar texto, popular um combobox. Por que quando faço isso ele chama o evento daquele componente ?
No exemplo pode ser visto que ao carregar a tela o combobox1 é populado e já é chamado o evento dele, e quando uso o evento do combobox1 que popula o combobox2 o evento do combobox2 é acionado.
Como poderia resolver isso ?
[code]package testeEvento;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;
import java.awt.Component;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.LayoutStyle.ComponentPlacement;
public class TesteEvento extends JFrame {
private JPanel contentPane;
private ArrayList<Integer> numeros;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TesteEvento frame = new TesteEvento();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TesteEvento() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
numeros = new ArrayList<Integer>();
numeros.add(1);
numeros.add(2);
numeros.add(3);
numeros.add(4);
numeros.add(5);
final JComboBox comboBox2 = new JComboBox();
comboBox2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Evento combobox2 acionado");
}
});
final JComboBox comboBox1 = new JComboBox();
comboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Evento combobox1 acionado");
if(comboBox1.getSelectedItem().equals(1)) {
comboBox2.addItem("");
for(Integer nr : numeros) {
comboBox2.addItem(nr);
}
}
}
});
comboBox1.addItem("");
for(Integer nr : numeros) {
comboBox1.addItem(nr);
}
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addGap(68)
.addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
.addComponent(comboBox2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(58))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(59)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(comboBox2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addContainerGap(173, Short.MAX_VALUE))
);
gl_contentPane.linkSize(SwingConstants.HORIZONTAL, new Component[] {comboBox1, comboBox2});
contentPane.setLayout(gl_contentPane);
}
}
[/code]