JFrame - Setting the inner dimension
I'm looking for an (efficient) way to specify the inner dimension of a JFrame. I have both menues, toolbars and a JTabbedPane so if I set the JFrame dimension to say W*H the client area (or whatever it's called) of the JTabbedPane will be less than that.
I want a way to tell the JFrame to resize itself so the client area of the JTabbedPane will be exactly the given width*height.
[396 byte] By [
Magosa] at [2007-10-3 8:37:55]

Camikr's method works well if you want the JFrame to be resized around the JComponent. If you want the other way around then you have a bit or work ahead.
you have to determine the current size of the JFrame's contentPane and also resize everything if the user changes the size. To do that you must register a ComponentAdapter to the component events (and override the componentResized(ComponentEvent ce)method).
Then within the componentResized() method you can use myJFrame.getContentPane().getSize() to determine the size of the content-pane. Now depending on the layout manager on the content-pane that contains your JTabbedPane, you set the Preferred Size of the JTabbedPane to be somewhat less than the Dimension object you got back from the getSize() method. Then call revalidate() from the content-pane.
NOTE: If you use myJFrame.getContentPane() to get the content-pane (instead of having a reference to it and setting it using setContentPane()) ...you can't call revalidate() without first casting it to a JComponent.
So you'd need to do something like
((JPanel)myJFrame.getContentPane()).revalidate();
Message was edited by:
mr.v.
If you want the other way around then you have a bit or work ahead.
(snip)
Are you on crack?
It's two lines of code:
import java.awt.Insets;
import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public final class SetFrameInnerSize implements Runnable
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new SetFrameInnerSize());
}
public static void setInnerSize(Window window, int w, int h)
{
Insets i = window.getInsets();
window.setSize(w + i.left + i.right, h + i.top + i.bottom);
}
public void run()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new JLabel("Hello", JLabel.CENTER));
setInnerSize(f, 200, 200);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}