• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of JScrollPane causes header to lose component orientation  RSS feed

 
S. Lai
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Per recommendation from a comment, I'm cross-posting my question here. Here's the question I posted:


The problem: header is left aligned and the table doesn't auto-resize.

My application is divided up into into two main components:

- a header consisting of a JLabel and JTableHeader
- a JScrollPane containing a JTable

The reason the JTableHeader is in a separate panel is because it should not scroll. The columns in the table all have preferred/max width set (except for column in the middle) so when the frame is resized only column should be changing its width. I believe I can use JScrollPane.setColumnHeaderView( JTable.getTableHeader() ); to get get the table header to remain static - the code I got had it like that and changing it doesn't impact the issue at hand as far as I can tell.

I have a problem with JScrollPane breaking the right-to-left (RTL) orientation of the JTableHeader. The JTableHeader will be aligned on the left whereas the JTable will be aligned on the right.

The question is similar to what was posted here which points to this unresolved bug. I'm not entirely sure it's the issue I'm seeing since the JTableHeader shouldn't be in the scroll pane. Additionally, the proposed solution doesn't work since I do need the scroll bar to be on the left as it would be expected in RTL orientation.

How can I fix the orientation for the JTableHeader while still maintaining the ability to auto-resize the columns?

EDIT: I have updated this code to include a JButton below the table in tablePanel.



Please feel free to post any questions/response here or StackOverflow. Thanks!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is that you're wrapping the table in a JPanel(BorderLayout). Why?If that doesn't produce the result you needed, post more details.
 
S. Lai
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does end up producing the correct alignment but I do believe I need the panel because in the actual code, I have a button below the table. This was something I overlooked, I apologize for that! I have updated the code to reflect that.

Basically I have something like:


EDIT: This is what it would normally look like in left-to-right orientation (button is only visible if scrolled to towards the end):
 
S. Lai
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't edit my first post, but hopefully this picture will clarify what's wrong:


That lovely button is the reason why the table is in a panel (and that panel inside a JScrollPane), otherwise Darryl's solution would have solved this issue.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whatever you do, the table needs to be the viewport view to behave correctly. Here's an approach using a slightly modified version of Rob Camick's Component Border (Thanks, Rob!). This also necessitates overriding getPreferredSize() of JTable.


Change in ComponentBorder:

Have you noticed that repeatedly resizing the resizable column shrinks the others?
 
S. Lai
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works perfectly! Thanks!

Yes, I do notice the button jiggle a little (resizes repeatedly?) as I resize the window but only if I drag from the right side.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:This also necessitates overriding getPreferredSize() of JTable.

I wondered about that when I did it, but focused on getting the desired result. There are a couple of related bug reports*, and I find it odd that they have been neglected for years; it would be trivial to incorporate the same logic used for other JComponents, namely to respect the border Insets in the UI delegate's getPreferredSize().

Darryl Burke wrote:Change in ComponentBorder:

Please note that this is a minimal change to obtain the desired result. A much better enhancement would be to either add enum constants (maybe ABOVE, BELOW, LEADING, TRAILING) that cause the border component to fill the width/height of the parent component (the one to which the border is set) or a setFills(boolean). Rob?

* Bug reports:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4307724
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4222732
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Darryl

- try to use CompoundBorders, whatever Empty + Etched/Bevel, then seems like as coordinates are properly computed, for standard JTable in JScrollPane too

- still missunderstand idea to laid separate JTable and JTableHeader, because works for me with, together Column/RowHeader(based on JList, JTable)


 
S. Lai
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did end up make a "setFill" type method so I'd have some flexibility in the future.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!