Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

JFrame's size is not loading correctly

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am currently working on using Swing and here is my code to initialize a Jframe



Let's say row and col = 200, when the program runs, it displays a window which is 184 x 160, not 200 x 200. This results in part of the graphic not being displayed. How can I fix this?
 
Bartender
Posts: 2702
130
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to what Carey mentioned,
The constructor for Dimension that you are using in that code takes the parameters as width and height. I think calling your variables row and col will add to confusion since if you look at a table visually:
It still does not fit in the analogy of width and height as you might think.


 
Bartender
Posts: 4109
156
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't set the size of a JFrame yourself. Let its containing JComponents decide their own value, and then use: frame.pack();

For instance:
 
pshivvy pat
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Don't set the size of a JFrame yourself. Let its containing JComponents decide their own value, and then use: frame.pack();

For instance:



This sets the window size correctly, but it does not display my graphics. I tried adding "this" to my frame and panel but it still does not work.
 
Carey Brown
Saloon Keeper
Posts: 7395
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with pack() is that if all panels and sub-panels are not filled before you call pack you'll end up with a frame smaller than desired.
 
Marshal
Posts: 70711
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our GUIs forum.

As Carey says, you have to have at least setPreferredSize() called on a complete row of child components and also on a complete column in order for pack() to give you a sensible size.
 
Rancher
Posts: 3208
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use setPreferredSize() anywhere.

It is the responsibility of each Swing component to know its own preferred size.

If you are doing custom painting then override the getPreferredSize() method of your component.

Then you add the component to the frame and then you pack() the frame.

Now the size of the frame will be the preferred size of the custom component plus the size required for the title bar and borders.
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic