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");
tabbedPane.setSelectedIndex(0);
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");
secondtabbedPane.setSelectedIndex(0);
// Make another panel for the 2nd dimension JTabbedPane
// panel22 will have the button on it.
JPanel panel22 = new JPanel(false);
secondtabbedPane.addTab(
"Two", icon, panel22, "Does twice as much nothing");
//Add the tabbed pane to this panel.
panel3.setLayout(new GridLayout(1, 1));
panel3.add(secondtabbedPane);
// I want the button to be on the second tab (panel22) so I'm selecting it
secondtabbedPane.setSelectedIndex(1);
// And getting the component.
JPanel ModPanel = (JPanel) secondtabbedPane.getSelectedComponent();
// Now I reset just for fun so I'm not looking at it.
tabbedPane.setSelectedIndex(0);
secondtabbedPane.setSelectedIndex(0);
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");
//AddButton.setVisible(false);
ModPanel.add(AddButton);
// 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));
add(tabbedPane);
}
protected Component makeTextPanel(
String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
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(),
BorderLayout.CENTER);
frame.setSize(400, 125);
frame.setVisible(true);
}
}
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
time.
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...