Ver Mensaje Individual
  #3 (permalink)  
Antiguo 11/08/2014, 09:32
Avatar de elpivelardin
elpivelardin
 
Fecha de Ingreso: agosto-2014
Mensajes: 8
Antigüedad: 9 años, 9 meses
Puntos: 0
Respuesta: addItem en JComboBox

Tengo un JFrame con 3 pestañas para la gestion de alumnos, guardando los datos en una tabla de MySQL. al dar de alta un alumno o recuperar los alumnos de la tabla, hago un additem para refrescar un jcombobox que hay tanto en la pestaña de bajas como modificaciones, pero al añadir se activa el actionListener y me muestra los datos de lo añadido automáticamente, sin necesidad de pulsar en el jcombobox

Este es el codigo de Altas

Código:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import java.sql.*;

public class PanelAltas extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;
	private JTextField clave = new JTextField();
	private JTextField nombre = new JTextField();
	private JTextField apellidos = new JTextField();
	private JTextField edad = new JTextField();
	private JTextField calle = new JTextField();
	private JTextField numero = new JTextField();
	private JTextField cp = new JTextField();
	
	private JButton limpiar = null;
	private JButton alta = null;
		
	public PanelAltas(){
				
		//configuracion JPanel
		this.setLayout(new BorderLayout());
		
		//parte de datos personales + direccion
		JPanel panel1 = new JPanel();
		panel1.setLayout(new GridLayout(4,4));
		panel1.setBorder(new TitledBorder("Datos personales:"));
		
		panel1.add(new JLabel("Clave"));
		clave.setHorizontalAlignment(SwingConstants.RIGHT);
		panel1.add(clave);
		
		panel1.add(new JLabel("Nombre"));
		nombre.setHorizontalAlignment(SwingConstants.RIGHT);
		panel1.add(nombre);
		
		panel1.add(new JLabel("Apellidos"));
		apellidos.setHorizontalAlignment(SwingConstants.RIGHT);
		panel1.add(apellidos);
		
		panel1.add(new JLabel("Edad"));
		edad.setHorizontalAlignment(SwingConstants.RIGHT);
		panel1.add(edad);
		
		this.add(panel1,BorderLayout.NORTH);
		
		panel1 = new JPanel();
		panel1.setLayout(new GridLayout(3,3));
		panel1.setBorder(new TitledBorder("Dirección:"));
		
		panel1.add(new JLabel("Calle"));
		calle.setHorizontalAlignment(SwingConstants.RIGHT);
		panel1.add(calle);
		
		panel1.add(new JLabel("Número"));
		numero.setHorizontalAlignment(SwingConstants.RIGHT);
		panel1.add(numero);
		
		panel1.add(new JLabel("Código Postal"));
		cp.setHorizontalAlignment(SwingConstants.RIGHT);
		panel1.add(cp);
		
		this.add(panel1,BorderLayout.CENTER);
		
		//configuracion Jbutton
		panel1=new JPanel();
		panel1.setLayout(new FlowLayout());
		limpiar = new JButton("Limpiar");
		limpiar.setMnemonic('L');
		limpiar.addActionListener(this);
		panel1.add(limpiar);
		alta = new JButton("Dar de alta");
		alta.setMnemonic('D');
		alta.addActionListener(this);
		panel1.add(alta);
		
		this.add(panel1,BorderLayout.SOUTH);		
	}

	
	public void actionPerformed(ActionEvent ev) {
		String aux = ((JButton)ev.getSource()).getText();
		
		if(aux.equals("Limpiar"))	limpiar();
		else	alta();
	}
	
	public void limpiar(){
		
		clave.setText("");
		nombre.setText("");
		apellidos.setText("");
		edad.setText("");
		calle.setText("");
		numero.setText("");
		cp.setText("");
	}
	
	public void alta(){
		try{
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/gestion","root","root");
			PreparedStatement pstmt = con.prepareStatement("INSERT INTO alumnos VALUES(?,?,?,?,?,?,?)");
			pstmt.setString(1,clave.getText());
			pstmt.setString(2,nombre.getText());
			pstmt.setString(3,apellidos.getText());
			pstmt.setInt(4,Integer.parseInt(edad.getText()));
			pstmt.setString(5,calle.getText());
			pstmt.setInt(6,Integer.parseInt(numero.getText()));
			pstmt.setString(7,cp.getText());
			pstmt.executeUpdate();
			pstmt.close();
			con.close();
		}
		catch(ClassNotFoundException ex){ex.printStackTrace();}
		catch(SQLException ex){ex.printStackTrace();}
		PanelBajas.c.addItem(clave.getText());
		PanelModificaciones.cb.addItem(clave.getText());
		limpiar();
	}
}
Y este el de Bajas

Código:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;


public class PanelBajas extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;
	private JTextField nombre = new JTextField();
	private JTextField apellidos = new JTextField();
	private JTextField edad = new JTextField();
	private JTextField calle = new JTextField();
	private JTextField numero = new JTextField();
	private JTextField cp = new JTextField();
	
	private JButton baja = null;
	
	static JComboBox<Object> c = null;
	
	public PanelBajas(){

		//configuracion JPanel
		this.setLayout(new BorderLayout());
		
		//configuracion apartado alumnos
		JPanel panel1 = new JPanel();
		panel1.setLayout(new GridLayout(1,1));
		panel1.setBorder(new TitledBorder("Alumnos:"));
		panel1.add(new JLabel("Clave"));
		c = new JComboBox<Object>();
		c.addActionListener(this);
		panel1.add(c);
		
		this.add(panel1,BorderLayout.NORTH);
		
		//parte de datos personales + direccion
		panel1 = new JPanel();
		panel1.setLayout(new GridLayout(2,0));
		JLabel aux = new JLabel();
		aux.setLayout(new GridLayout(3,3));
		aux.setBorder(new TitledBorder("Datos personales:"));
		
		aux.add(new JLabel("Nombre"));
		nombre.setHorizontalAlignment(SwingConstants.RIGHT);
		nombre.setEditable(false);
		aux.add(nombre);
		
		aux.add(new JLabel("Apellidos"));
		apellidos.setHorizontalAlignment(SwingConstants.RIGHT);
		apellidos.setEditable(false);
		aux.add(apellidos);
		
		aux.add(new JLabel("Edad"));
		edad.setHorizontalAlignment(SwingConstants.RIGHT);
		edad.setEditable(false);
		aux.add(edad);
		
		panel1.add(aux,BorderLayout.CENTER);
		
		aux = new JLabel();
		aux.setLayout(new GridLayout(3,3));
		aux.setBorder(new TitledBorder("Dirección:"));
		
		aux.add(new JLabel("Calle"));
		calle.setHorizontalAlignment(SwingConstants.RIGHT);
		calle.setEditable(false);
		aux.add(calle);
		
		aux.add(new JLabel("Número"));
		numero.setHorizontalAlignment(SwingConstants.RIGHT);
		numero.setEditable(false);
		aux.add(numero);
		
		aux.add(new JLabel("Código Postal"));
		cp.setHorizontalAlignment(SwingConstants.RIGHT);
		cp.setEditable(false);
		aux.add(cp);
		
		panel1.add(aux,BorderLayout.SOUTH);
		
		this.add(panel1,BorderLayout.CENTER);
		
		//configuracion Jbutton
		panel1=new JPanel();
		panel1.setLayout(new FlowLayout());
		baja  = new JButton("Dar de baja");
		baja.setMnemonic('D');
		baja.addActionListener(this);
		panel1.add(baja);
		
		this.add(panel1,BorderLayout.SOUTH);
		
	}
	
	public void actionPerformed(ActionEvent ev) {
		if(ev.getSource() instanceof JButton)	baja();
		if(ev.getSource() instanceof JComboBox)	consultar();
	}
	
	public void baja(){
		String x = (String) c.getSelectedItem();
		int indice = c.getSelectedIndex();
		
		try{
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/gestion","root","root");
			PreparedStatement pstmt = con.prepareStatement("DELETE FROM alumnos WHERE CLAVE=?");
			pstmt.setString(1,x);
			pstmt.executeUpdate();
			pstmt.close();
			con.close();
		}
		catch(ClassNotFoundException ex){ex.printStackTrace();}
		catch(SQLException ex){ex.printStackTrace();}
		
		c.removeItemAt(indice);
		PanelModificaciones.cb.removeItemAt(indice);
		
		limpiar();
	}
	
	public void limpiar(){
		
		nombre.setText("");
		apellidos.setText("");
		edad.setText("");
		calle.setText("");
		numero.setText("");
		cp.setText("");
	}
	
	public void consultar(){
		String x = (String) c.getSelectedItem();
		
		try{
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/gestion","root","root");
			PreparedStatement pstmt = con.prepareStatement("SELECT * FROM alumnos WHERE CLAVE=?");
			pstmt.setString(1,x);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				nombre.setText(rs.getString(2));
				apellidos.setText(rs.getString(3));
				edad.setText(Integer.toString(rs.getInt(4)));
				calle.setText(rs.getString(5));
				numero.setText(Integer.toString(rs.getInt(6)));
				cp.setText(rs.getString(7));
			}
			rs.close();
			pstmt.close();
			con.close();
		}
		catch(ClassNotFoundException ex){ex.printStackTrace();}
		catch(SQLException ex){ex.printStackTrace();}
	}
}
Gracias.