• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Making a column invisible in a JTable

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
We begin by testing your absorbancy by exposing you to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic