The Student Room Group

[Solved] JTable - quick help needed.

Im currently updating a program I wrote a while back and want to use a JTable instead of a list box. Basically.. I need it to display search results (searching through a database).

In short, my question is... how do you add and delete rows from a JTable that has already been created. if i knew how to do this I could sort my main problem out quite easily.

Would be happy to supply any additional information needed.

Thanks =]
I am guessing you are using Joomla when you say JTable. Yes?
No, Im using Netbeans.
I dont even know what Joomla is.

Its Java not Javascript. I probably should have said what language I was using :P
He He. Joomla is a PHP CMS. The prefix all their class names with a J. Sorry :frown:
Dont worry =]
Doesnt look like anyone can help me anyway

time to go sifting through all those tutorials and API and try and mak sense again
Reply 5
Don't know if you ever got this sorted.

Lots of Swing components such as JLists or JTables are backed by Model objects for storing their data.

JTable has a getModel() method which returns an instance of the TableModel interface. Provided you haven't used the setModel() method to give the JTable a custom TableModel, you can cast this to a DefaultTableModel instance.

DefaultTableModel has a addRow method which takes an array of Objects.
Baron
Don't know if you ever got this sorted.

Lots of Swing components such as JLists or JTables are backed by Model objects for storing their data.

JTable has a getModel() method which returns an instance of the TableModel interface. Provided you haven't used the setModel() method to give the JTable a custom TableModel, you can cast this to a DefaultTableModel instance.

DefaultTableModel has a addRow method which takes an array of Objects.

Ive tried this.
I havnt used the setModel() method i don't think
(unless mainTable = new JTable(data, columnNames); ) uses the setModel() method.

But when trying to cast the JTable.getModel() to DefaultTableModel I get an error :redface:
(Exception in thread "main" java.lang.ClassCastException: javax.swing.JTable$1 cannot be cast to javax.swing.table.DefaultTableModel)

Any Idea what the problem is..

P.S. Thanks for the help =]
I solved this in the end
the easiest way I have found to do it
is when creating a table do as so..

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

},
new String [] {
"Name", "Machine", "Part Number", "Min. Stock", "Act. Stock"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false
};

public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}

public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});




I found this using Netbeans built in GUI editor,
this code is usable to set the model to the default model so you can
then use the addRow() method.

yay
I'd just like to say, thanks for (a) posting the solution and (b) changing the thread title to include [Solved]. That's perfect - it stops people wasting time coming in here to answer the question, and also allows other people to use the search facility to find the solution if they have the same problem. Other posters take note.

Latest

Trending

Trending