Foros del Web » Programación para mayores de 30 ;) » Java »

Problema con JTable

Estas en el tema de Problema con JTable en el foro de Java en Foros del Web. Hola. Ayer comencé a migrar un programa que desarrolle en VB6 a Java, no tengo casi nada de experiencia en este lenguaje, Espero puedan ayudarme. ...
  #1 (permalink)  
Antiguo 03/05/2011, 05:39
 
Fecha de Ingreso: marzo-2011
Mensajes: 75
Antigüedad: 13 años, 2 meses
Puntos: 6
Pregunta Problema con JTable

Hola. Ayer comencé a migrar un programa que desarrolle en VB6 a Java, no tengo casi nada de experiencia en este lenguaje, Espero puedan ayudarme.

Tengo un JTable en el cual quiero listar productos.

CODIGO NOMBRE
01 JUGO RTD
02 CONCENTRADO

El problema es que los productos me los lista todos en el cabezal, me pone el Codigo y el nombre juntos en una columna del cabezal y me los va lostando horizontalmente.

Tengo el siguiente codigo:

Estoo es en el frm
private void formWindowOpened(java.awt.event.WindowEvent evt) {

DefaultTableModel aModel = (DefaultTableModel) jTable1.getModel();
aModel.setColumnIdentifiers(objE.getColClientes()) ;
jTable1.setModel(aModel);

}

Aca hago la conexion a la base de datos
public Connection getConexion(){
if (con==null) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:VariosC ont","sa","");
}
catch (ClassNotFoundException e) { e.printStackTrace();
}
catch (SQLException e) { e.printStackTrace();
}
}
return con;
}

ACA HAGO LA CONSULTA SQL
public void obtenerJugos()
{
try {
Statement stmt = this.getConexion().createStatement();
ResultSet rs = stmt.executeQuery( "SELECT * FROM JUGOS");
while (rs.next()) {
int s = rs.getInt("JUGCOD");
String i = rs.getString("JUGNOM");
jugos objJ = new Jugos(s,i);
this.ColJugos.add(objJ);
}
}
catch(SQLException e){ e.printStackTrace();}

}

Gracias.
  #2 (permalink)  
Antiguo 03/05/2011, 16:23
 
Fecha de Ingreso: abril-2011
Ubicación: lima
Mensajes: 134
Antigüedad: 13 años
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);

}
}

Etiquetas: jtable
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:41.