When does a JTabbedPane set the size of the component inside a tab?

I would like to know how big a component inside a tab is directly after I've added it to a tab in a JTabbedPane.

I thought once I've "added" a component via the JTabbedPane.add(String, Component) method, the Component would be realized with correct sizes.

Where or when should I request the information about how big a component has become inside a tab?

Example:

1. "Add" a tab via the menu.

2. Notice that the size of the panel has not changed even though we see it on the screen.

-Js

import java.awt.Color;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.JTabbedPane;

publicclass TabbedPaneShowingTestextends JFrame

{

//GUI

private JTabbedPane tabbedPane;

private JPanel tabPanel;

//MENU

private JMenuBar mainMenuBar;

private JMenu actionMenu;

private JMenuItem addTabMenuItem;

public TabbedPaneShowingTest()

{

this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

this.setContentPane(getTabbedPane());

this.setJMenuBar(getMainMenuBar());

this.pack();

this.setVisible(true);

}

//GUI

private JTabbedPane getTabbedPane()

{

if(tabbedPane ==null)

{

tabbedPane =new JTabbedPane();

tabbedPane.setPreferredSize(new Dimension(100,100));

}

return tabbedPane;

}

private JPanel getTabPanel()

{

if(tabPanel ==null)

{

tabPanel =new JPanel();

tabPanel.setBackground(Color.GREEN);

}

return tabPanel;

}

//MENU

private JMenuBar getMainMenuBar()

{

if(mainMenuBar ==null)

{

mainMenuBar =new JMenuBar();

mainMenuBar.add(getActionMenu());

}

return mainMenuBar;

}

private JMenu getActionMenu()

{

if(actionMenu ==null)

{

actionMenu =new JMenu("Action");

actionMenu.add(getAddLineMenuItem());

}

return actionMenu;

}

private JMenuItem getAddLineMenuItem()

{

if(addTabMenuItem ==null)

{

addTabMenuItem =new JMenuItem("Add Tab");

addTabMenuItem.addActionListener(new ActionListener()

{

publicvoid actionPerformed(ActionEvent e)

{

System.out.println("BEFORE TAB SIZE: " + getTabPanel().getWidth() +"," + getTabPanel().getHeight());

getTabbedPane().add("Tab",getTabPanel());

System.out.println("AFTER TAB SIZE: " + getTabPanel().getWidth() +"," + getTabPanel().getHeight());

}

});

}

return addTabMenuItem;

}

publicstaticvoid main(String args[])

{

new TabbedPaneShowingTest();

}

}

[5785 byte] By [JSnakea] at [2007-11-27 3:45:57]
# 1

Once again... a little experimenting is a good thing. Just use SwingUtilities.InvokeLater() to retrieve the proper size.

-Js

import java.awt.Color;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.JTabbedPane;

import javax.swing.SwingUtilities;

public class TabbedPaneShowingTest extends JFrame

{

//GUI

private JTabbedPane tabbedPane;

private JPanel tabPanel;

//MENU

private JMenuBar mainMenuBar;

private JMenu actionMenu;

private JMenuItem addTabMenuItem;

public TabbedPaneShowingTest()

{

this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

this.setContentPane(getTabbedPane());

this.setJMenuBar(getMainMenuBar());

this.pack();

this.setVisible(true);

}

//GUI

private JTabbedPane getTabbedPane()

{

if(tabbedPane == null)

{

tabbedPane = new JTabbedPane();

tabbedPane.setPreferredSize(new Dimension(100,100));

}

return tabbedPane;

}

private JPanel getTabPanel()

{

if(tabPanel == null)

{

tabPanel = new JPanel();

tabPanel.setBackground(Color.GREEN);

}

return tabPanel;

}

//MENU

private JMenuBar getMainMenuBar()

{

if(mainMenuBar == null)

{

mainMenuBar = new JMenuBar();

mainMenuBar.add(getActionMenu());

}

return mainMenuBar;

}

private JMenu getActionMenu()

{

if(actionMenu == null)

{

actionMenu = new JMenu("Action");

actionMenu.add(getAddLineMenuItem());

}

return actionMenu;

}

private JMenuItem getAddLineMenuItem()

{

if(addTabMenuItem == null)

{

addTabMenuItem = new JMenuItem("Add Tab");

addTabMenuItem.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

System.out.println("BEFORE TAB SIZE: " + getTabPanel().getWidth() + "," + getTabPanel().getHeight());

getTabbedPane().addTab("Tab",getTabPanel());

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

System.out.println("AFTER TAB SIZE: " + getTabPanel().getWidth() + "," + getTabPanel().getHeight());

}

});

}

});

}

return addTabMenuItem;

}

public static void main(String args[])

{

new TabbedPaneShowingTest();

}

}

JSnakea at 2007-7-12 8:49:42 > top of Java-index,Desktop,Core GUI APIs...