wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Adjust font size according to resolution change (or different screen size) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adjust font size according to resolution change (or different screen size)" Watch "Adjust font size according to resolution change (or different screen size)" New topic
Author

Adjust font size according to resolution change (or different screen size)

Aji Sree
Greenhorn

Joined: Jun 19, 2009
Posts: 18
Hi,

I am new to swing programming. I have to develop an application which basically runs in 800*600 resolution, but it should work in other resolutions too.

The problem I am facing now is with the font size of different components.

Suppose I have size of font as 12. When I am in 1280*1024 it is looking nice. But when I switch to 800*600 it is not displaying the text fully (ie some text is displayed as ....).

I want to set the font that could be automatically adjust to different resolutions(screen size).

Can anybody please help to solve this issue?

Thank you
Ajith
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Ajith,
Welcome to the Ranch.

I would suggest having a properties file or some pre defined external resource which would contain the mappings for screen resolution v/s font size. In your code you would need to load these properties and switch font size accordingly. A font factory would be helpful here.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Francois Nadeau
Ranch Hand

Joined: Apr 09, 2009
Posts: 46
Hi Ajith,

I would strongly suggest not to change font sizes to resolve this problem. Instead you should use layout managers and create a UI design which will display properly in a 800*600 resolution. What ever you do, don't set the Layout manager to null. Doing so will cause you a great deal of grief down the road.

Cheers.


[url]http://FrancoisNadeau.com[/url]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

I fail to understand how a layout manager would solve the problem.
Usually, the correct thing to do is pick up and use the system font. But since the OP's requirement is to explicitly change the font size, I don't think the layout manager would be of help here.
Francois Nadeau
Ranch Hand

Joined: Apr 09, 2009
Posts: 46
Hi Maneesh,

I am assuming a few things.

  • Ajith's problem is that some of the components (labels, buttons ect.) are not fully viewable at lower resolutions. The reason he wants to vary the font size is the solve this problem, not because of a requirement stating that font sizes should vary due to the screen size.
  • This is a business application, and therefore should follow Java's GUI best practices. Games on the other hand would not have to respect this.
  • Ajith stated that the application needs to run in 800*600. So the GUI needs to be designed to fit this resolution. Because of this, there should not be any cut-off text at that resolution (unless in tables or input fields where it is more acceptable).
  • The application can run at higher resolutions. That's where Layout managers come into play. They will allow a user to use the application at a higher resolution, and maintain the component's correct layout.


  • Layout managers will allow you to create GUIs which can be displayed at different resolutions without having to change the font sizes.

    Cheers.
    Aji Sree
    Greenhorn

    Joined: Jun 19, 2009
    Posts: 18
    Thanks Maneesh and Francois.

    I am afraid, Layout manager wouldn't do it for my page.

    I used Grid Layout and Border Layout.
    The Panel is using GridLayout of 1 row and 3 columns and this panel is added to content pane which is using border layout.

    In grid I have added a JLabel which has a long text. in 1280*1024 resolution , I can fully read this text. But in 800*600 resolution I can see the label fully but I cannot see the text fully. The last part of the text is .... .
    The components are adjusted to the screen size but the text in it is not adjusted accordingly. This is what exactly the problem I am facing.

    Thank you
    Ajith
    Francois Nadeau
    Ranch Hand

    Joined: Apr 09, 2009
    Posts: 46
    Hi Ajith,

    In that case you could use a JTextArea in the following manner.



    You may have better result if you use GridBagLayout instead of GridLayout. GridLayout will only create grids of the same size, while GridBagLayout will allow you to resize components as required.

    Cheers.
    Aji Sree
    Greenhorn

    Joined: Jun 19, 2009
    Posts: 18
    Thank you very much Francois.

    I think I can use JTextArea as in your reply.

    By the way is there any way to adjust font size if we get screen size using Toolkit.getDefaultToolkit().getScreenSize(); ?

    Thank you
    Ajith
    Francois Nadeau
    Ranch Hand

    Joined: Apr 09, 2009
    Posts: 46
    Hi Ajith,

    Your are very persistent with wanting to change the font sizes. If I had to do it, and I mean after putting up a fight not to do so and threatening to quick my job, I would set the font sizes on the LookAndFeel manager before creating the GUI. To do this your main function would need to be changed as follows:



    Each of the UIManger.put calls will change the default setting. Note that this will not work if programmers use JLabel.setFont function, since the latter function override the default font. Also, the put function will not update any GUI that have already been created, that's why you need to set the font size before creating your GUI components.

    I have not tested this code, so don't be too harsh on me if it does not work the first time.

    Cheers.
    Aji Sree
    Greenhorn

    Joined: Jun 19, 2009
    Posts: 18
    Thanks Francois ,

    I think I can follow this way.

    Thank you very much for the solution.

    Thank you
    Ajith S
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Adjust font size according to resolution change (or different screen size)
     
    Similar Threads
    screen resolution
    GUI and screen resolution
    Develop an application for several screen resolutions
    swing components size based on screen resolution
    swing and resolution