• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2 dimensional JTabbedPane

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
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...
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope... the problem is that you are mixing lightweight(Swing) and heavyweight(AWT) components...

Just change the line -


to -
 
This tiny ad will self destruct in five seconds.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic