JTable using NetBeans
843804Nov 15 2004 — edited Jan 14 2005Well i m using netbeans 4.0 beta2 and i want to create a table. Then i drag and drop a component jTable on my pane and i want to edit it. So i create an class who extends AbstractTableModel. And it 's ok but the colum haven't a name. I can't understand why ? Can you help me once more ?
Here is the source code :
/*
* frm.java
*
* Created on 15 novembre 2004, 11:58
*/
package javaapplication13;
/**
*
* @author oleksand
*/
import DataBase.BeanDataBase;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;
public class frm extends javax.swing.JFrame {
/** Creates new form frm */
public frm() {
initComponents();
try
{
BeanDataBase bdb = new BeanDataBase();
bdb = new BeanDataBase("login","password");
bdb.setDataBase("jdbc:mysql://localhost/db");
bdb.creerConnexionBD();
ModeleTabOutil mto = new ModeleTabOutil(bdb);
jTable1.setModel(mto);
mto.listOutil(1);
}
catch(Exception e)
{
System.out.println("Erreur");
}
}
/** 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.
*/
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jTable1 = new javax.swing.JTable();
jPanel2 = new javax.swing.JPanel();
jTree1 = new javax.swing.JTree();
getContentPane().setLayout(new java.awt.GridLayout(1, 2));
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setLayout(new java.awt.GridLayout());
jTable1.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"
}
));
jPanel1.add(jTable1);
getContentPane().add(jPanel1);
jPanel2.setLayout(new java.awt.GridLayout());
jPanel2.add(jTree1);
getContentPane().add(jPanel2);
pack();
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTable jTable1;
private javax.swing.JTree jTree1;
// End of variables declaration
class ModeleTabOutil extends AbstractTableModel
{
private Vector outil;
private BeanDataBase bdb = null;
public ModeleTabOutil(BeanDataBase db)
{
outil = new Vector(); // Creating an vector
bdb = db;
}
public void listOutil(int famille)
{
try
{
outil.removeAllElements(); // Clear the list
ResultSet rs = bdb.executeRequeteSelection("SELECT * FROM Tools");
while(rs.next()) // Create the list
outil.add(new loutil(rs.getInt("num"),rs.getString("Id"),rs.getString("Desc"),rs.getString("A"),rs.getString("B"),rs.getString("C")));
fireTableStructureChanged(); // Notify the table that there are perhaps some rows
}
catch(SQLException e)
{
System.out.println("Erreur "+e.toString());
}
}
final String[] colonnes = {"ID", "Desc", "A", "B","C"};
public String getColumnName(int column){return colonnes[column].toString();}
public int getColumnCount(){return colonnes.length;}
// Aller chercher dans le vecteur
public int getRowCount(){return outil.size();}
public Object getValueAt(int lig,int col){return ((loutil)outil.elementAt(lig)).getData()[col];}
public Object getValueAt(int lig){return ((loutil)outil.elementAt(lig)).getId();}
//public boolean isCellEditable(int lig,int col){if (col == 4)return true;else return false;} // Define wich column is editable
public void setValueAt(Object value, int lig, int col) // Value change in a combo
{
((loutil)outil.elementAt(lig)).setValue(value.toString());
fireTableCellUpdated(lig, col);
}
}
class loutil {
/* Variables for init table */
private int id = 0;
private String numT = null;
private String description = null;
private String arme = null;
private String piece = null;
private String atelier = null;
public loutil(int i,String t,String d,String ar,String p,String at)
{
id = i;
numT = t;
description = d;
arme = ar;
piece = p;
atelier = at;
}
public Object[] getData()
{
Object[] data = {numT,description,arme,piece,atelier};
return data;
}
public int getId(){return id;}
public void setValue(String a){atelier = a;}
public String getValue(){return piece;}
}
}