New To Java - Using tabs and the like.

The information on the Sun website wasn't too helpful when dealing with a tabbed pane. Maybe I can get the solution I want here.

First of all, how would you deal with making use of the JTabbedPane class and the tabs you want to use in them? In short, how do you make each tab do its function on click? Second, what is the best method of changing the objects within the GUi frame using the tabs?

Using a GUI builder(either NetBeans, Eclipse's own, or Jigloo), I assumed the best action was to construct panels before hand in seperate classes, and have the individually created panels imported and called within the frame on initialization or on the click of a tab. As much as I'd like to think that is it, there could be an easier way I don't know of. Or I could be taking the wrong approach altogether.

[826 byte] By [Tweaklinga] at [2007-11-26 23:11:54]
# 1

In my application, I have a Class extends JFrame, with a JPanel on it, that has a JTabbedPane with 3 tabs, and each of these tabs houses a smaller JPanel, on which the components are placed.

All of this is placed in one class, in one method.

tabbedPane = new JTabbedPane();

panelName1= new JPanel();

panelName1.setLayout(formLayout1);

panelName2= new JPanel();

panelName2.setLayout(formLayout2);

panelName3= new JPanel();

panelName3.setLayout(formLayout3);

tabbedPane.add("Tab 1", panelName1);

tabbedPane.add("Tab 2", panelName2);

tabbedPane.add("Tab 3", panelName3);

//and with panelName1.add(componentName); you can add components to each panel easily.

panel = new JPanel(new BorderLayout());

panel.add(tabbedPane, BorderLayout.CENTER);

add(panel);

panel.setVisible(true); //sets visibility on ALL components in the JPanel

N00dlesa at 2007-7-10 14:09:21 > top of Java-index,Java Essentials,New To Java...
# 2

A tabbed pane is a container that displays only one child component at a time. Typically, the children are themselves containers of other components. Each child is associated with a visible tab on the tabbed pane. The user can choose a child to display by selecting the tab associated with that child.

// Create a child container which is to be associated with a tab

JPanel panel = new JPanel();

// Add components to the panel...

// Specify on which edge the tabs should appear

int location = JTabbedPane.TOP; // or BOTTOM, LEFT, RIGHT

// Create the tabbed pane

JTabbedPane pane = new JTabbedPane();

// Add a tab

String label = "Tab Label";

pane.addTab(label, panel);

// Create a tabbed pane

JTabbedPane pane = new JTabbedPane();

// Add a tab with a label taken from the name of the component

component1.setName("Tab Label");

pane.add(component1);

// Add a tab with a label at the end of all tabs

String label = "Tab Label";

pane.addTab(label, component2);

// Add a tab with a label and icon at the end of all tabs

Icon icon = new ImageIcon("icon.gif");

pane.addTab(label, icon, component3);

// Add a tab with a label, icon, and tool tip at the end of all tabs

String tooltip = "Tool Tip Text";

pane.addTab(label, icon, component4, tooltip);

// Insert a tab after the first tab

int index = 1;

pane.insertTab(label, icon, component5, tooltip, index);

//A tabbed pane fires a change event whenever the selected tab is changed either by the user or programmatically.

// Register a change listener

pane.addChangeListener(new ChangeListener() {

// This method is called whenever the selected tab changes

public void stateChanged(ChangeEvent evt) {

JTabbedPane pane = (JTabbedPane)evt.getSource();

// Get current tab

int sel = pane.getSelectedIndex();

}

});

java_2006a at 2007-7-10 14:09:21 > top of Java-index,Java Essentials,New To Java...