Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Make CardLayout/JTabbedPane the size of the current card/pane?

843806Dec 14 2007 — edited Dec 14 2007
Hi all,

I have a set of panels that a user flips through, which I can implement either as a set of JPanels in a CardLayout or as a JTabbedPane with the tabs hidden -- it's no difference to me.

If all the panels have been instantiated, the current panel is always the size of the largest panel in the set. So if the current panel is significantly smaller than the largest panel, there's a bunch of unnecessary white space.

Is there any way to set things up so that the the currently visible panel is always that size that it would normally want to be (i.e. if it weren'tpart of a CardLayout/JTabbedPane)?

Thanks!
Sam



Below is a self-contained example using a JTabbedPane, but the same applies to a JPanel with CardLayout.
import java.awt.*;
import javax.swing.*;

public class SwingTester extends JFrame{
	
	public SwingTester(){
		JTabbedPane tabbedPane = new JTabbedPane();
		
		JComponent smallComponent = new JLabel("Test");
		tabbedPane.addTab("Small tab", smallComponent);
		
		JComponent largeComponent = new JLabel("Test ... Test ... Test");
		largeComponent.setBorder(BorderFactory.createLineBorder(Color.RED, 20));
		tabbedPane.addTab("Large tab", largeComponent);
		
		getContentPane().add(tabbedPane);
	}
	
	public static void main(String[] args){
		SwingTester st = new SwingTester();
		st.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		st.pack();
		st.setVisible(true);
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 11 2008
Added on Dec 14 2007
2 comments
270 views