About JTable

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 34
Reputation: srs_grp is an unknown quantity at this point 
Solved Threads: 0
srs_grp srs_grp is offline Offline
Light Poster

About JTable

 
0
  #1
Feb 2nd, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 14
Reputation: arseniew is an unknown quantity at this point 
Solved Threads: 3
arseniew arseniew is offline Offline
Newbie Poster

Re: About JTable

 
0
  #2
Feb 2nd, 2009
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:
  1. DefaultTableModel tableModel = new DefaultTableModel(null,new String[]{"Column1", "Column2", "Column3"});
then to remove column from view you do
  1. table.removeColumn(table.getColumnModel().getColumn(columnId));
where columnId is column number from table model
to bring it back
  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
    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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,595
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 200
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: About JTable

 
0
  #3
Feb 3rd, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC