soy principiante estoy trabajando con netbeans y estoy haciendo un programas que me muestre en un jtable las datos de un base de datos.
el programa funciona tomando el valor del jlist jcombobx y de radiobotton luego dependiendo la selecion me muestra la informacion que pido pero cuando hago una nueva seleccion el programa me tira el mismo dato al jtable aqui les muestro mi codigo.
import java.sql.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class Examen extends javax.swing.JFrame {
Connection con;
Statement stm;
ResultSet rs;
Vector ls =new Vector();
Vector cb =new Vector();
Vector tb =new Vector();
Vector rd =new Vector();
DefaultListModel mlista = new DefaultListModel();
DefaultComboBoxModel mcombo = new DefaultComboBoxModel();
DefaultTableModel mtabla = new DefaultTableModel();
public Examen() {
initComponents();
mtabla.addColumn("Resevacion");
mtabla.addColumn("Fecha");
mtabla.addColumn("Lote");
mtabla.addColumn("Area");
mtabla.addColumn("Valor");
cmdlista.setModel(mlista);
cmdcombo.setModel(mcombo);
cmdtabla.setModel(mtabla);
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Reservacion","root","root");
stm=con.createStatement();
cargarlista();
cargarcombo();
}catch(Exception e){JOptionPane.showMessageDialog(this,e.getMessage());}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
peso = new javax.swing.JRadioButton();
gringo = new javax.swing.JRadioButton();
jScrollPane1 = new javax.swing.JScrollPane();
cmdlista = new javax.swing.JList();
cmdcombo = new javax.swing.JComboBox();
jScrollPane2 = new javax.swing.JScrollPane();
cmdtabla = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Procesar");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
peso.setText("Lempiras ");
gringo.setText("Dolares");
cmdlista.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings; }
});
jScrollPane1.setViewportView(cmdlista);
cmdcombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
cmdtabla.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane2.setViewportView(cmdtabla);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 101, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(gringo)
.addComponent(peso)
.addComponent(cmdcombo, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addGap(59, 59, 59))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(60, 60, 60)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(cmdcombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(peso)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(gringo)
.addGap(18, 18, 18)
.addComponent(jButton1)))
.addGap(60, 60, 60))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int p=0;
if (peso.isSelected())
p=1;
if (gringo.isSelected())
p=2;
try{
//el query para mostrar lainformacion que necesito
rs=stm.executeQuery("select ReservacionID, Fecha, p.Nombre as Proyecto, v.Nombre as Vendedor, Lote, Area, Valor, m.Nombre as moneda from Reservacion as r inner join proyecto as p on r.ProyectoID = p.ProyectoID inner join Moneda as m on r.MonedaID = m.MonedaID inner join Vendedor as v on r.VendedorID = v.VendedorID where r.ProyectoID="+ls.get(this.cmdlista.getSelectedIndex())+" and r.VendedorID="+cb.get(this.cmdcombo.getSelectedIndex())+" and r.MonedaID="+p);
while(rs.next()){
tb.addElement(rs.getInt("ReservacionID"));
tb.addElement(rs.getDate("Fecha"));
tb.addElement(rs.getInt("Lote"));
tb.addElement(rs.getFloat("Area"));
tb.addElement(rs.getFloat("Valor"));
mtabla.addRow(tb);
}
}catch(Exception e){JOptionPane.showMessageDialog(this,e.getMessage());}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Examen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Examen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Examen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Examen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Examen().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox cmdcombo;
private javax.swing.JList cmdlista;
private javax.swing.JTable cmdtabla;
private javax.swing.JRadioButton gringo;
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JRadioButton peso;
// End of variables declaration
public void cargarlista(){
try{
rs=stm.executeQuery("select * from Proyecto");
while(rs.next()){
mlista.addElement(rs.getString("Nombre"));
ls.addElement(rs.getInt("ProyectoID"));
}
}catch(Exception e){JOptionPane.showMessageDialog(this,e.getMessage());}
}
public void cargarcombo(){
try{
rs=stm.executeQuery("select * from vendedor");
while(rs.next()){
mcombo.addElement(rs.getString("Nombre"));
cb.addElement(rs.getInt("VendedorID"));
}
}catch(Exception e){JOptionPane.showMessageDialog(this, e.getMessage());}
}
}