*
The moose likes Swing / AWT / SWT and the fly likes How to place components beneath tabs in right oriented JTabbedPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to place components beneath tabs in right oriented JTabbedPane" Watch "How to place components beneath tabs in right oriented JTabbedPane" New topic
Author

How to place components beneath tabs in right oriented JTabbedPane

Ez Hudsy
Greenhorn

Joined: Mar 29, 2011
Posts: 6
So I just stumbled across placement of tabs in a JTabbedPane to the right and left (i.e. setTabPlacement(JTabbedPane.RIGHT)) which I love the look of. What I need is to utilize the space this leaves beneath the tabs. I currently have a column of JButtons, but they get pushed to the side, leaving a lot of blank space.

Any thoughts on how to do this? some kind of custom overlay or something? I've place with absolute position some (setBounds) but that doesn't seem to do it.

I've attached a screenshot... in the code I basically have one horizontally aligned Box, with the JTabbedPane added to it, then the column of buttons after that.

boxOfEverything.add(tabbedPane);
boxOfEverything.add(boxColumnButtons);

I am excited to see if someone knows how to pull this off. =)



[Thumbnail for java_jtabbedpane.png]

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

I had the exact problem. Setbounds didn't work. For it to work, you have to set a layout. If you prefer to use setBounds() for all of your components, I recommend using this line:

This sets a Layout (of type null), so you can call setBounds. It will work after this.
If you need a working example, I will be more than happy to provide you with one. Happy coding!!!
Cheers,
John Price aka cc11rocks


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Ez Hudsy
Greenhorn

Joined: Mar 29, 2011
Posts: 6
hey john,

I certainly would rather stick with a Layout if it were possible. I am currently using BoxLayout:

panelOfEverything.setLayout(new BoxLayout(panelOfEverything, BoxLayout.PAGE_AXIS));

I would love to see an example of how you did this!

Ez
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Here's a solution that uses the glass pane and a couple of layouts -- Border and GridBag, to be specific. For the right-aligned tabs, it should work by changing the anchor to SOUTHWEST, and modifying the insets.**

I wrote this code a long time ago and there's a magic number 15 in the Insets ... that was just to make the layout correspond to the image posted. Also, this may not be useful if other components are to be displayed alongside/above/below the JTabbedPane.

If the approach doesn't suit, do post a SSCCE that we can play with.

** No, it'll need a lot more changes than that!


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Darryl Burke wrote:** No, it'll need a lot more changes than that!

Not really.
Ez Hudsy
Greenhorn

Joined: Mar 29, 2011
Posts: 6
Wow, this glasspane is great! I hadn't come across it before now.

So I fumbled around with your original code and got it working perfectly! This ended up being enough:

Rectangle tabBounds = tabbedPaneLibrary.getBoundsAt(0);
Container glassPane = (Container) frame.getRootPane().getGlassPane();
glassPane.setVisible(true);
glassPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(tabBounds.y, 0, 0, 15);
gbc.anchor = GridBagConstraints.SOUTHEAST;
glassPane.add(boxLibraryButtons, gbc);

I see you took out the tabBounds in your latest post's code. I wasn't sure what that was doing anyway so I'll see what it does without it. I am amazed at how quick and on it you guys are. I'll be stopping by the Big Moose again soon... =)
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Check out the code I posted here. Specifically, the insets you copied from the other forum post don't make sense in this context.
Ez Hudsy
Greenhorn

Joined: Mar 29, 2011
Posts: 6
I compiled your example, and its perfect. When I add those changes into my app verbatim, the column of buttons is actually floating in the middle of the JTabbedPane. There must be additional settings (setPreferredSizes?) in my GUI that effect things.

When I take out the tabBounds completely, and just set the fourth Inset parameter to 10, it nicely sets in the middle of the space and looks great. My question would be what is the purpose of getting the Rectangle tabBounds and adjusting the Inset to that?


[Thumbnail for Screenshot-2.png]

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

the column of buttons is actually floating in the middle of the JTabbedPane

You have to pack() the frame so as to realize the tabbed pane before obtaining the tab bounds. Did you do that?

The computation I used is valid for a GridLayout or other layout that respects the set preferredSize of the container. BoxLayout doesn't, so you would need to getPreferredSize of the panel after adding all buttons and use that to compute the right inset.

How do you want your button panel aligned -- right justified with the tabs or centered below them?
Ez Hudsy
Greenhorn

Joined: Mar 29, 2011
Posts: 6
I would center the column of buttons ideally. And you were correct, sure enough, I threw in a frame.pack() before this bit of code, and the buttons appeared beneath the tabs as expected.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

So, to center the button panel
-- compute the space between the right of the tab bounds and the right of the frame as already done in my code above
-- compute the additional space to center the panel
-- set the insets as in my code above
-- don't set the preferredSize of the panel
Note: I haven't tested this, so do post back whether that gives the desired location
Ez Hudsy
Greenhorn

Joined: Mar 29, 2011
Posts: 6
Oddly enough, your latest += margin snippet gives me a negative margin integer, and the buttons are off the frame. I think I just have a GUI setup thats too convoluted -- boxes insides panels inside frames, each having various layouts, etc. In time, I must try to clean it up.

I am so happy with how this worked out, though, I'm interested in how far one can take this glassPane technique. Is it possible to specify a second component and place it absolutely on the same glasspane? I don't get the impression that multiple glasspanes are possible, is that so?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

your latest += margin snippet gives me a negative margin integer, and the buttons are off the frame.

Works for me.

Is it possible to specify a second component and place it absolutely on the same glasspane?

Why not try it and see for yourself?

I don't get the impression that multiple glasspanes are possible, is that so?

Correct, there can be only one glass pane. For better understanding, read the API for JRootPane.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to place components beneath tabs in right oriented JTabbedPane
 
Similar Threads
How can i space JTabbedPane Tabs
How to make a JPanel a readonly component?
PopupMenu In JTabbedPane
Overlapping tabs in Windows XP LAF
Rearrange Tabs via Drag and Drop