Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

JTabbedPane with non-tabbed text

843804Mar 11 2005 — edited Mar 12 2005
OK, I'm stuck (again)

What I would like to do is have a tabbed pane (2 tabs) and then put some text (maybe in a JLable) on the same line as the tabs. I'll try to draw a picture for clearity. Anyone have any ideas about how to do this?

 ______    ______
/ tab1 \ /tab2  \         Here is some text
================================
|    This is what changes                             |
|            when you switch tabs                     |
|                                                                        |
================================
I think you can get the idea from that 'picture.'

Comments

camickr Mar 11 2005
The correct way is probably to override the TabbedPaneUI, but that too complicated for me, so here's a quick hack that might give you some ideas:
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;

public class TabbedPaneWithText extends JFrame
{
	public TabbedPaneWithText()
	{
		JTabbedPane tabbedPane = new JTabbedPane()
		{
			public void paintComponent(Graphics g)
			{
				super.paintComponent(g);

				Rectangle lastTab = getUI().getTabBounds(this, getTabCount() - 1);
				int tabEnd = lastTab.x + lastTab.width;

				String text = "Some Text";
				FontMetrics fm = getFontMetrics( getFont() );
				int stringWidth = fm.stringWidth( text ) + 10;
				int x = getSize().width - stringWidth;

				if (x < tabEnd)
					x = tabEnd;

				g.drawString(text, x + 5, 18);
			}
		};
		tabbedPane.add("1", new JTextField("one"));
		tabbedPane.add("2", new JTextField("two"));
		getContentPane().add(tabbedPane);
	}

	public static void main(String args[])
	{
        TabbedPaneWithText frame = new TabbedPaneWithText();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
	}
}
843804 Mar 11 2005
Thank you, what you posted got me on the right track.

Thanks again!

Brent
843804 Mar 12 2005
The usual solution to this kind of problem is to use a renderer or editor.
camickr has posted a paintComponent method that acts like a tabbed pane renderer, which renders text.

The problem is that you can't add a component to more than one container, so if you have a JPanel you can only put it in one tab. When the user changes tabs, you can remove the component and add it to the next tab. If you use this approach, make sure to use invokeLater() to do the adding/removing of the tab's component.

Or you can override paintComponent and render the tab. This works fine unless you want to edit the text, then you'll need a tabbed pane editor. So basically each tab would contain a tabbed pane editor, and the editor displays a shared JPanel that has text you can edit.
1 - 3
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Apr 9 2005
Added on Mar 11 2005
3 comments
292 views