Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Remembering JTable column widths  RSS feed

 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to remember column widths when a column has been added or deleted. This is proving to be much harder than I expected. I've tried overriding getDefaultColumnsForModel to actually remember the existing column widths, but I found I had to know whether a column had been added or deleted, in order to do the right thing. So I turned off autoCreateColumnsFromModel, but then my table wouldn't initialize. So I created an init method and called it from the constructor. But it's still not working right. I'm thinking now, there must be an easier way...... thoughts?? Thanks!
 
John Zukowski
Author
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not get the widths before adding/removing columns?
------------------
John Zukowski Author of "Definitive Guide to Swing for Java 2" and "Java Collections"
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I tried doing that in the createDefaultColumnsFrom Model method--I stored all the column widths in an array, and read them back in. But if you don't autoCreateColumnsFromModel, you don't have to remember anything. If you look at the code for autoCreateColumnsFromModel (which gets called when you fire a tree structure changed event) you'll see that it just blows away your existing columns and builds new ones based on your model. Say good-bye to your collumn widths. But if you set autoCreateColumnsFrom Model to false, the information about each column's width can be found from its getPreferredSize method.... (You then need call autoCreateColumnsFromModel in your constructor, or you won't get any columns in your table!)
If the user resizes, the column's width becomes it's preferred size, but..... I'll bet I need to insure that this still happens when I resize programmatically....
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem is this. When I call addColumn on the columnModel, I can give the column any INDEX I want, but it always gets added to the END of the table. So I've been trying to extend the columnModel adding in an insertColumn method. Problem is, I can't seem to figure out how to get the table to put the column in the same place I'm adding it to the model.
 
jeremy hull
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a java.util.Properties file to save off the current state of the table model before you exit the app. When you load the app, load the settings from the properties file.
Properties props = new Properties(new FileInputStream("table.properties");
props.get("column1.width");
etc... I usually loop through the column headers saving off the setting. It creates the file when you do: props.save();
If you need sample code I got it. email me at jcahull@earthlink.net
You can use this to remember size, location and anything else you can assign to a variable and store as a string, like color and font too.
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to remember widths when I add or delete a column. No need to persist it.
Thanks anyway!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!