Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the same font for all component Java

I want to set a specific font for all component in a JPanel but I prefer that the question is still more general and not limited to the JPanel. How can I set the font to a list of component in a container (JFrame or JPanel)?

like image 543
Luca Avatar asked Oct 04 '12 15:10

Luca


People also ask

What fonts are in Java awt font?

Logical Fonts Java defines five logical font families that are Serif, SansSerif, Monospaced, Dialog, and DialogInput. It must be supported by the JRE. Note that JRE maps the logical font names to physical font because these are not the actual font libraries.

Which class encapsulates various information about font?

This extra information is encapsulated in the FontRenderContext class.


1 Answers

Here is a simple method that allows you to specify Font to the whole components tree under any Container (or just a simple Component, doesn't matter):

public static void changeFont ( Component component, Font font )
{
    component.setFont ( font );
    if ( component instanceof Container )
    {
        for ( Component child : ( ( Container ) component ).getComponents () )
        {
            changeFont ( child, font );
        }
    }
}

Simply pass your panel and specific Font into this method and you will get all childs refactored aswell.

like image 172
Mikle Garin Avatar answered Dec 02 '22 17:12

Mikle Garin