943,699 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 756
  • Java RSS
Feb 2nd, 2009
0

About JTable

Expand Post »
Hi!!
I have created JTable with fixed no of rows and columns.Then depending upon some conditions I want to hide one column,but the methods removeColumn or setting maxWidth to 0 are not working.I am not getting any error.
How can I do this?
Similar Threads
Reputation Points: 9
Solved Threads: 0
Light Poster
srs_grp is offline Offline
34 posts
since Sep 2008
Feb 2nd, 2009
0

Re: About JTable

hi, i spend many sleeples nights trying to solve it i did it this way:
first create custom table model like this, and ascribe it to the table:
Java Syntax (Toggle Plain Text)
  1. DefaultTableModel tableModel = new DefaultTableModel(null,new String[]{"Column1", "Column2", "Column3"});
then to remove column from view you do
Java Syntax (Toggle Plain Text)
  1. table.removeColumn(table.getColumnModel().getColumn(columnId));
where columnId is column number from table model
to bring it back
Java Syntax (Toggle Plain Text)
  1. table.addColumn(new TableColumn(columnId));
you have to remember about:
  • repaint() each time you add, remove column
  • always remove columns from the last one, because if you have columns 0,1,2,3 and remove column(0) the indexes are recount and you will have columns 0,1,2
  • use
    Java Syntax (Toggle Plain Text)
    1. table.convertColumnIndexToModel(table.getSelectedRow());
    first if you want to read data
  • add, change, remove rows should be done on tableModel, not on the table directly
i guess there is many ways to do this, but i found only this one to work
its get a little bit more tricky if you want to sort the table too...
Last edited by arseniew; Feb 2nd, 2009 at 1:44 pm.
Reputation Points: 14
Solved Threads: 3
Newbie Poster
arseniew is offline Offline
14 posts
since Jan 2009
Feb 3rd, 2009
0

Re: About JTable

I think you have to call the revalidate method on your JTable, after removing or adding a column, to make it work. Its been a while, but I think that's the advice Ezzaral gave me when I asked a similar question before (I could be misquoting him; whatever advice he gave me worked).
Last edited by BestJewSinceJC; Feb 3rd, 2009 at 12:45 am.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Can Someone Help?
Next Thread in Java Forum Timeline: Java Coin Purse problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC