aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Centering horizontally a JPanel in a tab of a JTabbedPane containing a JScrollPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Centering horizontally a JPanel in a tab of a JTabbedPane containing a JScrollPane " Watch "Centering horizontally a JPanel in a tab of a JTabbedPane containing a JScrollPane " New topic
Author

Centering horizontally a JPanel in a tab of a JTabbedPane containing a JScrollPane

Charles Van Damme
Greenhorn

Joined: Feb 24, 2009
Posts: 19
Good day.
A community question, I guess ...

When one decides to use a JScrollPanel because the object to display (a JPanel containing a GroupLayout, in my case) is taller than the high of the container (a JPanel tabPanel1 of a JTabbedPane, in my case), while the width of this JPanel object is thinner than the width of the container, one obtains ...
- the vertical scrolling rail (right side of the tabPanel1) without the horizontal scrolling rail (underneath)
- the JPanel positionned at the left side of the tabPanel1

By means of a GridBagLayout, I try to place the JPanel in the horizontal center of the tabPanel1 :But I get this way a wrong effect:
- tabPanel1 comes as wanted in the horizontal center of the tabpanel (surronunded by a thin line, as not wanted).
- The two scrolling rails disappear. When I reduce the main frame to recover one of both of them, the tabPanel1 disappears completely ! Namely when I try to reduce the main frame the slightest bit under the initial width or high of the tabbedPane, by means ofIf I don't implement this 'setPreferredSize()', at the initial display, the GUI is larger than the screen, because of the natural dimension of one of the other 'tabPanel's, and I loose the scrolling rails too.

In other words, how to make coexisting 1) a tabbedPane with a 'setPreferredSize()' and 2) a JPanel containing a GridBagLayout, itself containing at its horizontal center a JScrollPane (, itself containing a GroupLayout()).

Thanks in advance to those who try to help me.
Snorry for my bad English.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Charles Van Damme wrote:


CENTER isn't valid for fill. See the API for the valid constants to use.

Have you tried using setAlignmentX/Y(0.5) ?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Unless I've misunderstood your requirements, this works for me.
Charles Van Damme
Greenhorn

Joined: Feb 24, 2009
Posts: 19
Good day, everybody.
Dear Darryl Burke,

Thanks for your good example; my problem is solved.
The point was : How to have my page content be centered (horizontally) in my JTabbedPane = main frame, with provision of a JScrollPane, when this tabPane is wider than my page content ?
The answer is :
- Use a first JPanel (scrollPanel) to embed my Layout and the whole page content.
- Use a second JPanel (scrollHolder) to hold the JScrollPane (!) and embed the scrollPane.
- Then tabPane.addTab(tabPaneTitle, new JScrollPane(scrollHolder));

My mistake : I was using a single JPanel - equiv. to 'scrollPane' - embedding the large GroupLayout, which was an object of a separated class extending JScrollPanel.
And I was usinginstead of

I seems as if the JPanel scrollHolder in ' scrollHolder.add(scrollPanel); ' does offer the "wanted centering service", and the single JPanel in my previous implementation not. But why ? Can a conflict situation arise between the GroupLayout and the JScrollPane features ? I don't think so.

Unimportant remark : JPannel and JTabbedPane classes both inherit from javax.swing.JComponent and java.awt.Container. Nevertheless, JTabbedPane features additional 'add(Component ...)' methods, while JPanel not.

I will not forget to specify 'Resolved' in a couple of days. Again, many thanks, Darryl.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Centering horizontally a JPanel in a tab of a JTabbedPane containing a JScrollPane