views:

18

answers:

1

I'm building a Java Applet in NetBeans that has a TabbedPane on the bottom of the applet and a regular Panel on the top. I would like the top Panel to switch depending on which tab is selected on the bottom. (I would attach a screenshot to clarify, but being new here it isn't allowed.)

A Button has ActionPerformed, but a TabbedPane doesn't seem to have StateChange for each of the seperate tabs (at least not readily visible in NetBeans).

Any ideas?

A: 

According to here, you can do something like this:

// Create the tabbed pane
JTabbedPane pane = new JTabbedPane();

// Add tabs...; see Adding a Tab to a JTabbedPane Container

// 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();
    }
});

Then, use some switch statement to direct the flow of the program.

Continuation: Last time I used a JTabbedPane in Netbeans, all that I had to do was to add a new tab and simply build the gui for it. If you are having trouble with this, you might want to take a look at the Card Layout.

If you go through the tabs on the right hand side of your development screen, you should come accross a list of events. What you need to do is to select the appropriate event from that list and Netbeans will do it for you. On the other hand, you can open the .java file (while not open in netbeans) with a text editor (WordPad, NotePad++, etc) modofiy the code you want and save it. When you will open the file back through netbeans, you should see that your changes have been loaded as well.

npinti
Thanks for the response. Here are two further questions.
ssvarc
@npinti -- Thanks for the response. Here is the second half of the original question. 1. NetBeans builds the UI and you can't change the generated code. How can I show a different panel depending on which tab is selected? (The code for detecting which panel is selected you have provided -- I'll have to test it to see if it works.) Thanks!
ssvarc
I have modified my answer.
npinti
@npinti: I'm trying to use the tab change on the bottom half of the applet to control which panel is shown on the top half of the applet (which has a regular JPanel on top and a JTabbedPane on bottom). As the GUI code is auto generated I'm wondering how this can be done in NetBeans.
ssvarc
Modified my Answer.
npinti