[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 917
  • Last Modified:

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

Hello,

  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?

  Thanks!
0
sapbucket
Asked:
sapbucket
  • 3
  • 3
  • 2
1 Solution
 
petmagdyCommented:
in ur JFrame Constuctor or in the main method before displaying the JFrame like this:

myFrame.setSize(450, 450);
0
 
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!
0
 
sapbucketAuthor Commented:
petmagdy,

  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() {
        initComponents();
    }
   
    /** 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) {
                formWindowActivated(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(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.setText("Menu");
        jMenu1.setBackground(new java.awt.Color(236, 233, 216));
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Menu");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Menu");
        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        pack();
    }



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!!!

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
petmagdyCommented:
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.pack();
    mainFrame.setVisible( true );
  }
}

class JCanvas extends JComponent {


   public JCanvas() {
        setDoubleBuffered(true);
        setOpaque(true);
      }

      public void paintComponent(Graphics g) {
        super.paintComponent(g);
    }

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

      public Dimension getMinimumSize() {
        return getPreferredSize();
      }

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

    public ImageViewer() {
         setSize(450, 450);
        initComponents();
    }
0
 
objectsCommented:
>         pack();

replace that line with:

setSize(300, 300);

or change the ctor to be:

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

0
 
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

0
 
objectsCommented:
> It expands to the size of its components.

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now