JTabbedPane
won't be sized until it is attached to a container that is capable of laying it out.
Window#pack
s job is to determine the preferred size of the child components and size them based on the requirements of the layout manager.
Only at this time would a component (and your JTabbedPane
) be sized.
Components may be resized in response to changes in the container hierarchy as well, but the basic process is the same. The JTabbedPane
s parent container will decide when it needs to update it's contents and perform a new layout operation, resizing the components based on it's needs
None of your examples show the tabbed pane been added to a parent container...
Updated with simple test...
The following example basically demonstrates when a component would be resized.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestTabSize {
public static void main(String[] args) {
new TestTabSize();
}
public TestTabSize() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JPanel panel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
};
panel.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
System.out.println("Resized to " + e.getComponent().getSize());
}
@Override
public void componentMoved(ComponentEvent e) {
System.out.println("Moved to " + e.getComponent().getLocation());
}
});
System.out.println("Before been added = " + panel.getSize());
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("test", panel);
System.out.println("Before been added to tab = " + panel.getSize());
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
System.out.println("Before been added to frame = " + panel.getSize());
frame.add(tabbedPane);
System.out.println("After been added to frame = " + panel.getSize());
frame.pack();
System.out.println("After been packed = " + panel.getSize());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
System.out.println("After been visible = " + panel.getSize());
}
});
}
}
Output...
As you can see from the output, until the frame is packed
, nothing is sized
Before been added = java.awt.Dimension[width=0,height=0]
Before been added to tab = java.awt.Dimension[width=0,height=0]
Before been added to frame = java.awt.Dimension[width=0,height=0]
After been added to frame = java.awt.Dimension[width=0,height=0]
After been packed = java.awt.Dimension[width=200,height=200]
After been visible = java.awt.Dimension[width=200,height=200]
Resized to java.awt.Dimension[width=200,height=200]
Moved to java.awt.Point[x=11,y=33]