accessing a jTable problem

M

Mike

Hi,

I have a jtable in a scrollpane generated by forte and I don't know how to
access the data. I have written manual code before where I add a table
model when instantiating the jtable, but now I am using forte and it has
generated the code for the table for me and I don't know how to access the
table model. The forte code I have is below:

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null, null}
},
new String [] {
"Column 1", "Column 2", "Column 3", "Column 4", "Column 5",
"Quantity", "Column 6"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class,
java.lang.String.class, java.lang.String.class, java.lang.String.class,
java.lang.String.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false
};

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

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

And the code I have to link it with I would normally do like:

Vector data = themodel.v.tabmod.getDataVector();

having created the table thus:

DefaultTableModel tabmod = new DefaultTableModel(){
blah
};
JTable mytable = new JTable(tabmod);

I also need to know how to add and remove rows from the jtable and have the
same problem re the table model.

I need to get this working urgently and would welcome any help,

Mike
 
D

Dieter Decavele

All data operations on a table or any graphical component in Swing
should be done through their model. Read more about the MVC pattern to
find out why. So if you want to add/remove/update the model, you should
perform the actions on the model. The model will sent the events to the
controller (here the table) and the controller to the view.
You can access the model in your case by (DefaultTableModel)
jTable1.getModel ().
Make sure you can edit your data in the view by setting the appropriate
fields in the boolean array canEdit to true for whatever column you need
to edit.

I have a very good book on Swing which explains all methods and how
things work in combination with a very good structure: "Java Swing" from
O'Reilly. I think they now have a "Java Swing Revisited" for the 1.4
version.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top