Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

JTable - problem when resizing columns

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two JTables which share the same column model. When the user resize the columns in the "main" table, the other table automatically follows. This didn't work due to a bug in Swing (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4816146). I applied the suggested fix, and seemed to work ok. However, if the user drags the column margins to the extreme right (click on one of the column margins, drag right until the cursor is outside of the frame), it will now crash with a StackOverFlowError. I must admit I have no idea about how to fix that.

In the attached example, I've only included ONE table. It still crashes. I know that this example will work OK if I remove the columnMarginsChanges method, but then the bug described above will make it impossible to have two tables with the same ColumnModel.

 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
works correctly, bunch of code example about, but

- override those events from TableColumnModelListener, not subclass from JTable directly,

________________________

- I din't see that there are two JTables,

- the same about TableColumnModel,

- rest of events must be delayed by Swing Timer (call timer.restart()) until resize ended, this events is fired per every of pixel, everything inside your columnMarginChanged should be called only one time

- doLayout can be usefull for JTextArea/Pane as Renderer/Editor

- whats real reason for resizingColumn.setPreferredWidth(resizingColumn.getWidth()); default TableCellEditort is JTextField, how, where and why this code to set something for

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bottolf, the solution is the same as the one recommended by Rob Camick in your other thread. Let us know if this poses any problems with two tables.Also added an 'else' to get rid of the 'return' statement.

edit As a courtesy to those who copy/paste/run your posted code, please include a line to set the defaultCloseOperation of a JFrame to EXIT_ON_CLOSE.
 
Bottolf Knotten
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. It fixed the StackOverflowError, and it works OK with ONE table, but not with two. I can drag one column margin once, but then the columns seem to freeze.

I've modified my example to include two tables. The idea is that when moving or resizing columns in table 1, table 2 should follow.


 
Bottolf Knotten
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW: I discovered that if I add , the original solution works, but then the table with seems to become totally independent of the scrollpane width, which is not what i want...
 
Bottolf Knotten
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixed it, but I don't understand why it works :-).

The problem turned out to be that the call to doLayout causes columnMarginChanged to be called recursively. Ususally a limited number of times, but when you drag the column margins to the extreme right, this goes on until memory is exhausted. I added a hack to avoid the method being called recursively (see isColumnMarginChangeInProgress in the code below), and now it works perfectly, at least from what I can see.

I've tried both with invokeLater and swing.Timer, but without success. The problem remains the same, although the GUI behaves somewhat differently.


 
I found some pretty shells, some sea glass and this lovely tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic