Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JFrame position and size  RSS feed

 
Ian Cockcroft
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am instantiating a JFrame, hoe do I set the position and size. The API specs say nothing about a setLocation method, is there such a method?
Thanks,
Regards
Ian
 
Ian Cockcroft
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found it, sorry[LIST]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what's the answer?
 
Ian Cockcroft
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I knew how to read the API specs, I would have seen that setLocation and setSize are inherited. I was looking for methods defined by that class not the parent class.
cheers
ian
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood. Thanks.
 
Qusay Jaafar
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Use setBounds(int xUpperLeftCorner,
int yUpperLeftCorner,
int frameWidth,
int frameHeight)
Regards
 
Sloan Bowman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always use the Toolkit class to get the specs of your currenct system and set the frame accordingly. Example below:
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
final int WIDTH = screenSize.width;
final int HEIGHT = screenSize.height;
// Setup the frame accordingly
// This is assuming you are extending the JFrame //class
this.setSize(WIDTH \ 2, HEIGHT \2);
this.setLocation(WIDTH \ 4, WIDTH \ 4);
Hope this helps someone..
--Sloan
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
if your layout management works fine, use pack() and let the components compute their preferred size (of course you should set the preferred size of tables, trees etc.). pack() should be called instead of setSize() (it will overrule setSize if called afterwards, or setSize will overrule pack() in the opposite case), after adding all components, and before calling setVisible().
if you use >1.4 you can place the frame in the center of the screen by calling:
frame.setLocationRelativeTo(null);
(this is a method of java.awt.Window)
regards,
Chantal
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!