Search code examples
javadatabaseswingjframepreferredsize

Can't change JFrame dimensions when set visible


I'm an SQL-beginner, I'm trying to write a simple application that allows me to manage my games library. I tried doing various SELECT from the db and all works properly. Now the problem: when I click on btnAdd I want to set my addGame frame visible.

I tried this:

addGame.setVisible(true);

but when I set it visible, that's the result.

My Main Activity is this.

The Event bound to the button is:

private void btnAggiungiActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
    addGame.pack();
    //Dimension d = new Dimension(500, 100);
    //addGame.setPreferredSize(d);
    addGame.setVisible(true);
}      

But with addGame.pack(); it only fits horizontal dimension. What's wrong?


Solution

  • It's hard to know what is specifically wrong without more code, preferably a small compilable program, but in general you should

    • make sure that your GUI uses layout managers sensibly and avoids use of null layout if at all possible.
    • avoid calling setSize(...) or setPreferredSize(...) if possible.
    • pack your GUI's before displaying them by calling pack() on the top-level window prior to calling setVisible(true)
    • let the GUI's components and layout managers size themselves.