Ver Mensaje Individual
  #2 (permalink)  
Antiguo 03/05/2011, 16:23
lincolnf_2
 
Fecha de Ingreso: abril-2011
Ubicación: lima
Mensajes: 134
Antigüedad: 13 años, 1 mes
Puntos: 3
Respuesta: Problema con JTable

aki hay un ejemplo de lo que buscas, pero en estoy utilizando procedimientos almacenados. espero que te sirva


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* principal.java
*
* Created on 21/04/2011, 05:19:05 AM
*/
package ejmeplojatbeldatabase;

import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/**
*
* @author Angelical
*/
public class principal extends javax.swing.JFrame {
Connection cnn=null;
CallableStatement cst=null;
ResultSet rs=null;
DefaultTableModel dtm=null;
ResultSetMetaData rsmt=null;
/** Creates new form principal */
public principal() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf. windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
initComponents();
}
private void cleanModel()
{
for (int i =dtm.getRowCount()-1; i >=0; i--) {
dtm.removeRow(i);
}


}

/** 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

jPanel1.setBorder(javax.swing.BorderFactory.create TitledBorder("Datos"));

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {

},
new String [] {
"Id", "Nombres", "Direccion", "Telefono"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false
};

public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable1.getTableHeader().setReorderingAllowed(fals e);
jScrollPane1.setViewportView(jTable1);
jTable1.getColumnModel().getColumn(0).setResizable (false);
jTable1.getColumnModel().getColumn(3).setResizable (false);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 454, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
);

jButton1.setText("Mostrar datos..");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Quitar");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jButton1))
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(19, Short.MAX_VALUE))
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

try {
Class.forName("com.mysql.jdbc.Driver");
cnn=DriverManager.getConnection("jdbc:mysql://localhost/ejemplo",
"root","2311046");
cst=cnn.prepareCall("call mostrar()");
rs=cst.executeQuery();
rsmt=rs.getMetaData();
ArrayList<Object[]> datos=new ArrayList<Object[]>();
while (rs.next())
{
Object[] filas=new Object[rsmt.getColumnCount()];
for (int i = 0; i < filas.length; i++)
{
filas[i]=rs.getObject(i+1);
}

datos.add(filas);
}

dtm=(DefaultTableModel)this.jTable1.getModel();
cleanModel();
for (int i = 0; i <datos.size(); i++)
{
dtm.addRow(datos.get(i));
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}

}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
dtm.removeRow(this.jTable1.getSelectedRow());
}//GEN-LAST:event_jButton2ActionPerformed

/**
* @param args the command line arguments
*/


// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration//GEN-END:variables
}


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.

*/
package ejmeplojatbeldatabase;

/**

*
* @author Angelical
*/

public class Ejmeplojatbeldatabase {


/**
* @param args the command line arguments
*/

public static void main(String[] args) {

new principal().setVisible(true);

}
}