Question

I have swing based application made in neatbeans and want to make it according to screen resolution ie Jframes and its component also resized according to screen size so that we see all frame in any resolution. Thankx in advance

Was it helpful?

Solution

At work we have also had the same problem and solved parts of it thusfar. In our case, the JFrame is the size of the screen (not resizable). This means we only need to set the size of Fonts (and other resolution dependent aspects of the JComponents) once.

But if you can resize your JFrame, you can update the JComponents by traversing its hierarchy of JComponents, as soon as the size of the JFrame is changed (using, for instance, its method addComponentListener(ComponentListener)).

Now to the solution itself. If you provide a reference resolution, in addition to your current resolution. Then all you really need to do, is scale up or down, based on which is the larger one.

We defined our reference resolution as width 800 and height 600. Your current resolution can easily be obtained by the following code (only showing how to get the width part):

GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getWidth();

The code we used to change widths is the following (the same concept applies to height):

public static synchronized int getWidthForCurrentResolution(int width) {
    return (int)((currentResolutionWidth / referenceResolutionWidth) * width);
}

The following will be our code for changing the size of Fonts:

public static synchronized int getFontSizeForCurrentResolution(int fontSize) {
    return (int)(Math.min(currentResolutionHeight / referenceResolutionHeight, currentResolutionWidth / referenceResolutionWidth) * fontSize);
}

One important thing to remember, is that the parameter arguments provided should be in your reference resolution. And, of course, this is by no means a perfect solution. But a good start.

OTHER TIPS

Thanx lot for your time,

Like Jörgen Lundgren solution i have edited my netbeans initComponents(); Get Solution Which Became Very complicated not easily editable for my application .Actually Its Nice Solution.

Also I Found 2 silly solutions like: I have Developed My frames all possible Resolutions and called according to present Resolutions. I know Its very Bad Idea ,but 1 way we think.

Last 1, I have Develop my frame in Very small resolution 800*600 ,By default it runs on any resolutions without problem

My application is already Running and Made in netbeans, Steel I cant get help from layout managers..

And searching for Best solutions. Steel working with Jörgen solutions. I write Solutions for whom have same problem they get way in between .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top