Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

swing and resolution  RSS feed

 
Ansh Bhai
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
Is there anyway I can set a standard size for my JFrame, even though I might change the resolution of my screen/monitor?
Is there any standard way I can design a swing application to meet standard resolutions?
If you dont know what I am talking about, and have a working swing app...try playing around with different screen resolutions. Then you would know what I am talking about.
Thanks.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure you can!
use the Toolkit class found in the java.awt package.
use this line to find the screen size (which you get as a dimension) and set your JFrame according to this. then it will be what you want , no matter what the resolution is:
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
 
Narayana RM
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u can have a method like this:

public java.awt.Rectangle getScreenBounds(){

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

GraphicsDevice gd = ge.getDefaultScreenDevice();
return gd.getDefaultConfiguration().getBounds();
}
which would give u the screenbounds as a rectangle. from there onwards u can fix the screen size with some thing like this in the applet:
this.resize((int)(getScreenBounds().getWidth() * 1),(int)(getScreenBounds().getHeight()*0.72));
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, after you have set your JFrame to the screen size that you got from Toolkit.getScreenSize() it is a good idea to use Layout Managers for everything inside the JFrame so that they will resize accordingly.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!