Centering an Applet in a JTabbedPane?

I’ve been playing with this for some time and I haven’t been able to get my applet to center in the middle of a tabbed pane in my jframe:


            JFrame.setDefaultLookAndFeelDecorated(false);
            
            setSize(500,500);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBackground(Color.gray);
            JTabbedPane tabbedPane = new JTabbedPane();
            
            JPanel appletpanel = new JPanel(new BorderLayout(),false);
            applet=new jsgcApplet();
        appletpanel.add(applet,BorderLayout.CENTER);
        
        editor=new JEditorPane();
            JScrollPane editorpane = new JScrollPane(editor);
            
            tabbedPane.add("Display",appletpanel);
            tabbedPane.add("Script Editor",editorpane);
            
            getContentPane().add(tabbedPane,BorderLayout.CENTER);
            
            applet.init();
            applet.start();
            
            setVisible(true);

Does anyone see anything blatantly wrong with my code?

Edit: With this code it is set at the top right corner

Hmm. Seperate the issues. See if it centers in the BorderLAyout pane (it should) and then see what happens if you just add something ofa known size to the tabbed pane.

Also you might want to try doing a pack() befoer you set it visible

BorderLayout.CENTER will add the applet to the panel’s center region, but that doesn’t say anything about its actual alignment, does it? I would have expected it to be expanded to fill the whole panel, but you say that’s not what’s happening. Maybe it is, but the panel isn’t where you think it is?

If you’re using layout managers, a pack() in the right place can mean the difference between expected operation and absolute bedlam, in my experience - Jeff is dead right about that.

Otherwise, if you know the sizes of everything involved, you might find it easier to set the layout manager to null and position the applet in absolute coordinates. Be careful of insets though.

When debugging GUI layouts, it’s often handy to give everything different background colours until you’re sure things are performing as you’d expect - this makes unexpected behaviour much easier to spot.

Sometimes different background colors don’t help if the component is not opaque. You can call setOpaque(true), or sometimes I use borders to get a similar effect.

Well, I’ve using pack() and no effect on the problem. I replace the applet with a label and the label centered fine.

My next question is: Is a JApplet a heavyweight component? In looking over the jdocs I see that JApplet inherits from java.awt.Applet and not javax.swing. So maybe thats my problem…

UMph.

Are you trying to use JAPplet AS a component?

Lots of luck, its a base container, like a JFrame. And yes I believe its heavyweight.