Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Making a column invisible in a JTable  RSS feed

 
santi galay
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I've been trying to make a column invisible in my JTable (the whole column, not the data in it), but found it impossible. I've tried to make its length 0, but it doesn't work. Could anyone help me on this?
Thanks
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
here is a part of my code which is working perfectly.

TableColumn idClmn= entryTable.getColumn("Property Id");
idClmn.setMaxWidth(0);
idClmn.setMinWidth(0);
idClmn.setPreferredWidth(0);

regards
deekasha

 
santi galay
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<QUOTE>
TableColumn idClmn= entryTable.getColumn("Property Id");
idClmn.setMaxWidth(0);
idClmn.setMinWidth(0);
idClmn.setPreferredWidth(0);
</QUOTE>
Thanks a lot deekasha. I worked beautifully!
I've tried with -1 instead of 0, and now the column is utterly
invisible!
Thanks again,
Santi

 
Paul Bailey
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works great, but now when the user tabs through the columns, the cursor appears to dissapear for one tab! how annoying!
Can anybody think of a way of preventing this?
Alternately, how do you know when a new cell has been selected? I tried setting the TableCellEditor, but that didn't seem to get all the events. any ideas?
 
Ashish Majmundar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found this technique more helpful than setting the column width...
Put all your invisible columns at the end of the data model. For example,
if you have the following data:
Col1 Col2 Col3 Col4
Row1: "abc", "def", "ghi", 123
Row2: "jkl", "mno", "pqr", 456
and you want to hide the third and fourth columns,
all you have to do is
1) Return colNames = {"Col1", "Col2", "Col3" } for your table header
2) return 3 for getColumnCount();
3) in getColumnName(int col)
return (col > 2) ? "" : colNames[col];
Hope that helps. I keep several columns for computation and export purposes only, and have found this method to be very effective.
Thanks,
Ashish
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make a column invisible, just delete that column from JTable - this would delete the column from VIEW not from the MODEL.
You can add/delete a column to the table as and when needed without affecting the Model.
 
pride Jimmy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think have a easier method to do this:
JTable table = new JTable();
Object[] header = new object[]{"COL1", "COL2"};
Object[][] data = new Object[][]{{"A", "B"}, {"C","D"}};
DefaultTableModel model = new DefaultTableMdoel(data, header);
table.removeColumn(table.getColumn("COL1"));
do as this, your table(view) will donn't display the first column(COL1), but the model remain to store the data of first column.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!