msp
msp

Reputation: 173

Set JTabbedPane width to constant

I'm implementing an UI in Java Swing. Therefore I use a JTabbedPane. The tabbedPane has no components at startup. When i add a tab to the tabbedpane, the width of the tabbedpane increases, when i remove the tab, the width resizes to the width at the startup. This should not happen.

The tabbedpane is placed on a JPanel which has a gridbag layout.

Layout code:

Container contentPane = mainFrame.getContentPane();
contentPane.setLayout( new GridBagLayout() );
GridBagConstraints c = new GridBagConstraints();        
// add the component tree
initComponentTree();
c.gridx = 0;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 1;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 1;
c.weighty = 1;
contentPane.add( componentTree, c );        
// add the tabbed pane
initTabbedPane();
c.gridx = 1;
c.weightx = 10;
contentPane.add( tabbedPane, c );       
// add the status panel
initStatusPanel();
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 0;
c.weighty = 0;
contentPane.add( statusPanel, c );


Hope someone can help!

Upvotes: 0

Views: 684

Answers (3)

Michael
Michael

Reputation: 2726

It has always been my opinion that GridBagLayout should be avoided at all costs. It really offers nothing that can't be achieved by nesting panels with other layouts. The only thing it offers is a lot of frustration.

I would recommend putting your JTabbedPane in a panel using BorderLayout and put it in the CENTER position. If you insist on using GridBayLayout you can try using the setMinimumSize() method, GridBagLayout is one of the layout managers that honors that method (BoxLayout is the other).

Upvotes: 0

StanislavL
StanislavL

Reputation: 57381

Try to set ipadx value of the GridBagConstraints to the desired width.

Upvotes: 2

Peter Ilfrich
Peter Ilfrich

Reputation: 3816

Have you tried setSize()?

JTabbedPane pane;
pane.setSize(SOME_WIDTH_CONSTANT, SOME_HEIGHT_CONSTANT);

Upvotes: -1

Related Questions