Aviv Cohn
Aviv Cohn

Reputation: 17173

Regarding the Look and Feel in Java

In Java, I want to set the Look and Feel of the program (specifically to the 'get system look and feel').

Is it necessary to set the look and feel inside each component (aka, each JPanel, each JFrame)?

Or is setting the look and feel once, enough to affect the whole application?

If once is enough, then where should I place the code to do so (in what class?)

Upvotes: 1

Views: 102

Answers (2)

Henrik
Henrik

Reputation: 1807

You define it in the main method and it will be used everywhere.

This is the main screen's main method:

Example:

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainScreen mainWindow = new MainScreen();
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                mainWindow.frame.setVisible(true);
                } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

Upvotes: 3

AnatolyG
AnatolyG

Reputation: 1587

Or is setting the look and feel once, enough to affect the whole application?

Correct

If once is enough, then where should I place the code to do so (in what class?)

Usually, in a class which creates the application frame (before it is created):

    UIManager.setLookAndFeel(lookAndFeelName);
    ...
    JFrame frame = new JFrame(...);

Also, you can change LnF on the fly for the whole application, if you need:

 UIManager.setLookAndFeel(lookAndFeelName);
 SwingUtilities.updateComponentTreeUI(frame);
 frame.pack();

Upvotes: 1

Related Questions