Using NetBeans, How to set size of JFrame at loadtime?


  I am using a BorderLayout. The main JFrame container that holds all other components for the application doesn't have a variable name. Nor can I set the width, height, or position of the initial frame. setSize() doesn't work.

  I just don't understand. I can setSize for every other component no problem. Just the very initial JFrame doesn't do anything when I try to setSize.

  Visually, this is very annoying. I cannot get things "just the way I want."

  How to setSize() with a JFrame - using NetBeans IDE?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

in ur JFrame Constuctor or in the main method before displaying the JFrame like this:

myFrame.setSize(450, 450);
sapbucketAuthor Commented:
Argh. So annoying.

Ok, so I think the window size is determined by the components the JFrame contains.

So I put a button in, set it for SOUTH. Now there are three sizes: minimum, maximum, and preferred. I cannot set any of them. This should be straightforward and it isn't.

Am I thinking "bad" here? I read the book - they just use setSize() everywhere. For some reason I cant!
sapbucketAuthor Commented:

  there is no variable name for the JFrame Window. So in your code sample, you use myFrame.  I don't see any in my code:

public class ImageViewer extends javax.swing.JFrame {
    /** Creates new form ImageViewer */
    public ImageViewer() {
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
    private void initComponents() {
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();

        setTitle("Blackjack Image Viewer");
        setForeground(new java.awt.Color(236, 233, 216));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
            public void windowClosing(java.awt.event.WindowEvent evt) {

        jButton1.setText("Killah Nigga");
        jButton1.setBackground(new java.awt.Color(204, 204, 204));
        getContentPane().add(jButton1, java.awt.BorderLayout.SOUTH);

        jMenuBar1.setPreferredSize(new java.awt.Dimension(300, 25));
        jMenu1.setBackground(new java.awt.Color(236, 233, 216));





you can see that all of the components IN THE JFRAME have names (jMenu2, jMenu3, etc.). The JFrame doesn't?!! argh I can't stand being a newbie!!!

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

it can be desciding by the following:
1- JFrame.setSize()
2- or take a look at this sample:

class TestFrame extends JFrame
  public TestFrame() {
    super( "Graphics demo" );
    getContentPane().add(new JCanvas());

  public static void main( String args[] ) {
    TestFrame mainFrame = new TestFrame();
    mainFrame.setVisible( true );

class JCanvas extends JComponent {

   public JCanvas() {

      public void paintComponent(Graphics g) {

      public Dimension getPreferredSize() {
        return new Dimension(400,400);

      public Dimension getMinimumSize() {
        return getPreferredSize();

      public Dimension getMaximumSize() {
        return getPreferredSize();
then simply do this:

    public ImageViewer() {
         setSize(450, 450);
>         pack();

replace that line with:

setSize(300, 300);

or change the ctor to be:

    public ImageViewer() {
         setSize(450, 450);

sapbucketAuthor Commented:
Sorry everyone.

It was because of the layout.

It expands to the size of its components.

Thanks for the suggestions!

I am going to close the question

> It expands to the size of its components.

not if you remove the call to pack() as was suggested to you already.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.