This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Aji Sree
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Francois Nadeau
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 46
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 46
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Francois ,

    I think I can follow this way.

    Thank you very much for the solution.

    Thank you
    Ajith S
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic