elwynn
elwynn

Reputation: 247

Why won't this Swing tabbed pane display?

Could someone explain what I am doing wrong with my classes that my JTabbedPane doesn't display when the JFrame.setvisible is set to true?

Yes, the main method of the program (which I won't put here) uses the event dispatching thread to initiate ArionGUI.

Here is my code for the JFrame:

import javax.swing.*;


public class ArionGUI extends JFrame {

    public ArionGUI() {
        // Set up GUI frame for Arion
        JFrame arionFrame = new JFrame("Arion v 0.01");
        // Add Arion Tabbed Pane
        arionFrame.getContentPane().add(new ArionTabbedPane());
        // Terminate the application when closed
        arionFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        // Set the size of the frame
        arionFrame.setSize(500, 500);
        // Center window
        arionFrame.setLocationRelativeTo(null);
        // Prevent user from resizing window
        arionFrame.setResizable(false);
        // Make Arion frame visible on screen
        arionFrame.setVisible(true);                
    }
} 

And here is my code for the JTabbedPane:

import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JComponent;

public class ArionTabbedPane extends JComponent {

    JTabbedPane arionTabbedPane;

    public ArionTabbedPane() {

        arionTabbedPane = new JTabbedPane(JTabbedPane.TOP);
        arionTabbedPane.addTab("Characters", new JLabel("This is the characterz tab"));
        arionTabbedPane.addTab("Miscellaneous", new JLabel("This is the miscellaneous tab"));
        add(arionTabbedPane);

    }

}

Upvotes: 0

Views: 1432

Answers (2)

Daniel Bingham
Daniel Bingham

Reputation: 12924

Because ArionTabbedPane isn't actually a tabbed pane. It's a wrapper for one. So you're just adding a component to your JFrame not a TabbedPane. If you want to be able to add ArionTabbed pane to your JFrame it needs to extend JTabbedPane. If you want to add the Pane it's wrapping, then you need a function that returns a reference to it's internal tabbed pane and you need to add that to your JFrame. Something like this:

ArionTabbedPane tabbedPane = new ArionTabbedPane();
arionFrame.getContentPane().add(tabbedPane.getPane());

Where ArionTabbedPane.getPane() is something like this:

Public JTabbedPane getPane() {
return arionTabbedPane;
}

Edit: Hmm.. the other thing you could do that mioght work, if you don't want to do either of those is have ArionTabbedPane extend JPanel instead of JComponent. Java knows JPanel is a container and so when it's added to your JFrame it should check inside the JPanel for things to show. The only thing you'd have to change for that would be having ArionTabbedPane extend JPanel instead of JComponent.

Edit again, if you extend JTabbedPane then you'll need to remove the internal JTabbedPane. The new ArionTabbedPane should look something like this:

public class ArionTabbedPane extends JTabbedPane {

public ArionTabbedPane() {
    super(JTabbedPane.TOP); // Calls JTabbedPane's constructor.
    this.addTab("Characters", new JLabel("This is the characterz tab"));
    this.addTab("Miscellaneous", new JLabel("This is the miscellaneous tab"));
}

}

Much simpler really.

Upvotes: 3

Related Questions