views:

340

answers:

2

Hi all,

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);

    }

}
+3  A: 

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.

Daniel Bingham
+1 I had to recreate the code to see the problem... Well done
OscarRyz
Thank you. Your initial answer ArionTabbedPane should extend JTabbedPane works! That's all I need.
elwynn
Careful though, you need to remove your internal JTabbedPane if you're going to extend JTabbedPane. You won't be using it anymore. You're new class should look something like what I will now go edit into the answer.
Daniel Bingham
+1  A: 
OscarRyz
Thanks Oscar, that's very helpful!
elwynn