Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

JTable using NetBeans

843804Nov 15 2004 — edited Jan 14 2005
Well 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;}
}
}

Comments

843804
You have to put your JTable in a JScrollPane and then add it to your JPanel.
843804
I have done what you tell me. So i do : jPanel1.add(jTable1);
But i haven't my column name :s
camickr
JTable table = new JTable(...);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
843804
excellent it s working fine thx :)
843804
Hi,

We try to build a table to display some information from a database (Access) but we can't do a few things:

* we would like to have more than 4 columns and to be able to edit them...
* we would like to give them title
* how can we send some parameters to the table?

We try to build it not using the design mode but writing the code it itself (with pre-creation code) but we cannot see anything when we run the program or we can simply not compile it because the program does not recognize the functions.

Then simple questions: are the rows (number of rows) will be automatically added when reading the database...?

Thnks in advance for your quick answer.

P.S.: you can reply either in english, french or spannish.

Thanks again.
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Feb 11 2005
Added on Nov 15 2004
5 comments
173 views