JTable Help

Hi, I am having an issue with my JTable i hope someone can help me out on.

I have a JTable with 10 columns. The user has an option on the JMenuBar to hide columns 5,6,7 and 8. To do this i use the Menu Listener:

if(e.getSource()== hideColumns)

{

TableColumnModel columnModel1 = list.getColumnModel();

if (hideColumns.isSelected())

{

columnModel1.removeColumn( columnModel1.getColumn( 8 ) );

columnModel1.removeColumn( columnModel1.getColumn( 7 ) );

columnModel1.removeColumn( columnModel1.getColumn( 6 ) );

columnModel1.removeColumn( columnModel1.getColumn( 5 ) );

}

That works fine. But my issue comes when i want to bring those columns back. The rest of the listener goes as follows:

elseif (!(hideColumns.isSelected()))

{

TableColumn fareCol=new TableColumn();

TableColumn paidCol=new TableColumn();

TableColumn checkCol=new TableColumn();

TableColumn chargeCol=new TableColumn();

fareCol.setHeaderValue("Fare");

paidCol.setHeaderValue("Paid");

checkCol.setHeaderValue("Check");

chargeCol.setHeaderValue("Charge");

columnModel1.addColumn(fareCol);

columnModel1.addColumn(paidCol);

columnModel1.addColumn(checkCol);

columnModel1.addColumn(chargeCol);

columnModel1.moveColumn(5,9);

}

This second part of the listener creates 4 new columns, assigns headers to them, and places them on the end of the table. The last line moves what used to be Column 10, back to the end. This results in the columns ending back up in the correct order.

When i run the program, data gets entered into the table just fine. All of the columns populate correctly etc. I activate the menu control, make the columns disappear, data still is correct. Only columns 1,2,3,4,5, and 10 show, and the data in them is correct.

When i activate the menu control again, to make the columns reappear, the columns reappear, but they only show data that is in the first column.

First off, is there a more effiecient way of doing what I'm trying to do? If not, how would i fix the problem i'm having?

*** Note, i'm sort of a n00b stumbling through this program. You may have to be a bit detailed. Thanks **

[2777 byte] By [rojmaba] at [2007-10-3 4:33:50]
# 1
Generally speaking, the most safe and stable way for JTable programming is handling the table via table model(s). In your case, prepare two table models, one for ten columns and another for six columns. And call JTable.setModel() in your menu handler.
hiwaa at 2007-7-14 22:37:28 > top of Java-index,Java Essentials,Java Programming...
# 2

Swing related questions should be posted in the Swing forum.

> prepare two table models, one for ten columns and another for six columns.

I don't believe this is the best approach. Swing uses an MVC design. So you don't want to change the model only the view of the model. The OP has the right idea, change the view by removing/adding TableColumns from the TableColumnModel.

> the columns reappear, but they only show data that is in the first column.

That would be because the TableColumn maps directly to a column in the TableModel. Since you didn't specify the model column when you created the TableColumn, it defaults to the first column.

However, a better solution is to simply save the TableColumn when it is removed from the TableColumn and then add it back in as required. This is especially important if you column uses custom renderers. These renderers will be lost if you recreate the TableColumn. Also if the user change the width of the column this will be preserved when it is restored.

Here is some simple code that manages the TableColumns in the TableColumnModel. You should be able to use the hide/show methods if you desire:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=743041&start=7

camickra at 2007-7-14 22:37:28 > top of Java-index,Java Essentials,Java Programming...
# 3

I know camickr is always right and respectable.

I used to feel view manipulation of Swing mega-components, JTable et al, is somewhat awkward, clumsy and can necessitate tour-de-force coding effort.

For example, TableColumnModel has addColumn() method, but not insertColumn(position) method.

Simple table.setModel() gives much peace of mind, at least for me. :)

hiwaa at 2007-7-14 22:37:28 > top of Java-index,Java Essentials,Java Programming...