• Post Reply Bookmark Topic Watch Topic
  • New Topic

2 dimensional JTabbedPane  RSS feed

Duane Riech
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to have a 2 dimensional JTabbedPane?

I want to display several selection along the top.
If the user selects a particular tab, Items, I want to display another JTabbedPane
along the right side. Each of these tabs represent a category of items. Then for
each item category, I display a pane with a JTable, several text fields and
some action buttons.
My classes are:
Main_class - which builds the JTabbedPane displayed on top.
Item_tab - which builds the JTabbedPane displayed on the right side.
ItemCategory - Has the JTable, fields, etc.

1. I create a new Item_tab().
2. For each Item_tab tab, I create a new ItemCategory to display item data for
that category.
3. I then create the Main_class JTabbedPane and pass the Item_tab JTabbedPane
as the component.
This DOES work, EXCEPT that when the application first runs, it displays an
instance of ItemCategory, as if the user had already selected the Item tab on
the top JTabbedPane.
Confused??? So am I. I just wondered if anyone had already tried this and if
so how they did it.
Thanks in advance....
Nathan Pruett
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the kind of thing you're looking for?

Duane Riech
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, thanks for the reply...
Your example doesn't really address the problem I was having. I can create
a 2 dimensional JTabbedPane application, the problem is when I start
putting Buttons, Textfields, etc. on one of the selected tabs.
To show you what I mean, I modified the TabbedPaneDemo example as posted
for Java tutorials.
Here's the code...

import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;

public class TabbedPaneDemo extends JPanel {
public TabbedPaneDemo() {
ImageIcon icon = new ImageIcon("images/middle.gif");
JTabbedPane tabbedPane = new JTabbedPane();
Component panel1 = makeTextPanel("Blah");
tabbedPane.addTab("One", icon, panel1, "Does nothing");
Component panel2 = makeTextPanel("Blah blah");
tabbedPane.addTab("Two", icon, panel2, "Does twice as much nothing");
// Component panel3 = makeTextPanel("Blah blah blah");
// Exchange panel3 (above) with the following...

JPanel panel3 = new JPanel(false);
// create the 2nd dimension JTabbedPane...
JTabbedPane secondtabbedPane = new JTabbedPane();
// Make a panel for the 2nd dimension JTabbedPane.
Component panel11 = makeTextPanel("Blah");
secondtabbedPane.addTab("One", icon, panel11, "Does nothing");
// Make another panel for the 2nd dimension JTabbedPane
// panel22 will have the button on it.
JPanel panel22 = new JPanel(false);
"Two", icon, panel22, "Does twice as much nothing");
//Add the tabbed pane to this panel.
panel3.setLayout(new GridLayout(1, 1));

// I want the button to be on the second tab (panel22) so I'm selecting it
// And getting the component.
JPanel ModPanel = (JPanel) secondtabbedPane.getSelectedComponent();
// Now I reset just for fun so I'm not looking at it.

if ( ModPanel == panel11 )
System.out.println(" modpanel is panel11" );
else if ( ModPanel == panel22 )
System.out.println(" modpanel is panel22" );
// Add the button to panel22
Button AddButton = new Button("hi");

// The rest of the code is as the original example.

tabbedPane.addTab("Three", icon, panel3, "Still does nothing");
Component panel4 = makeTextPanel("Blah blah blah blah");
tabbedPane.addTab("Four", icon, panel4, "Does nothing at all");
//Add the tabbed pane to this panel.
setLayout(new GridLayout(1, 1));
protected Component makeTextPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
panel.setLayout(new GridLayout(1, 1));
return panel;
public static void main(String[] args) {
JFrame frame = new JFrame("TabbedPaneDemo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
frame.getContentPane().add(new TabbedPaneDemo(),
frame.setSize(400, 125);
When you run the application, notice that the "Hi" button appears on the
screen. IF you tab between some of the tabs on the UPPER tab pane, you'll
notice that the "Hi" button disappears. At this point, the application
works just fine with respect to the display of the button at the proper
To "FIX" the problem, uncomment the line AddButton.setVisible(false);,
compile and re-run. The button no longer appears AT INITIAL STARTUP.
So you could probably set all the button, textfields, etc. to
setVisible(false) and get it to work, but then you couldn't see them after
the initial startup when you DID click on the correct tab.
My guess this is outside the scope of the original intent of JTabbedPane,
but if anyone has some comments, I'd like to hear them. For my implementation, it would be nice to use a 2d JTabbedPane.

Thanks alot...
Nathan Pruett
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope... the problem is that you are mixing lightweight(Swing) and heavyweight(AWT) components...

Just change the line -

to -
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!