ActionPerformed - JComboBox
Saludos.
En tenido un problema, tengo un JComboBox el cual es editable y me sirve para buscar una informacion en un base de datos, y tiene un ActionPerformed el cual al dar enter sobre el JComboBox realiza una accion, el tema es que este JComboBox me esta generando 2 eventos, no se por que al dar entre sobre el JComboBox el automaticamente hace lo que debe hacer en el metodo, pero lo hace 2 veces.
Aqui un ejemplo muy sencillo para que vean el error.
public class prueba extends javax.swing.JFrame {
static int key;
/** Creates new form prueba */
public prueba() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jComboBox1 = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jComboBox1.setEditable(true);
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(91, 91, 91)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(175, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(52, 52, 52)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(228, Short.MAX_VALUE))
);
pack();
}//
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
even();
System.out.println("Comienzo -----");
}
public void even(){
System.out.println("Termino------");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new prueba().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox jComboBox1;
// End of variables declaration
}
Ya intente con objeto del evento y utilizando el metodo getsource, pero sin embargo el evento se esta realizando 2 veces.
gracias.
J Franco.
- Inicie sesión o regístrese para enviar comentarios
Problema evento JCombox resuelto
Hola me encontraba con ese problema y la verdad no sabia porque pasaba. Lo solucione agregando esta pequeña línea de codigo
private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
if (evt.getStateChange() == ItemEvent.SELECTED){
// TU EVENTO
}
}
De esta manera solo una vez se disparara el evento
Problema
Ok, voy a probar y te cuento.
Gracias.