Mohit Agrawal
Mohit Agrawal

Reputation: 36

Change the decoration style of the Java Swing JFrame

I'm trying to change the decoration style of the JFrame. I have tried the following code.

//...
static void main(String[] args) {
    EventQueue.invokeLater(()-> {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("This is Title");
        frame.setSize(500, 600);
        frame.setVisible(true);
    });
}

It changes the decoration style of the JFrame but it only works if the look and feel is set to Metal. If the look and feel is changed somewhere in the code in the run-time somewhere in the code as:

for (UIManager.LookAndFeelInfo laf: UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(laf.getName()) {
        UIManager.setLookAndFeel(laf.getClassName());
        SwingUtilities.updateComponentTreeUI(frame);
    }
}

Then, the look and feel get changed but the decoration of the frame get disappeared. As a result, I'm not able to move, resize, maximize, minimize and close the frame.

Question 1:

Is it possible to fix this problem?

Question 2:

Can I change the decoration style of the JFrame to make look like Visual Studio title bar?

Upvotes: 1

Views: 1619

Answers (1)

David Lavender
David Lavender

Reputation: 8311

You can set the JFrame instance itself to be undecorated:

static void main(String[] args) {
    EventQueue.invokeLater(()-> {
        JFrame frame = new JFrame("This is Title");
        frame.setUndecorated(true);
        frame.setSize(500, 600);
        frame.setVisible(true);
    });
}

If you want it decorated differently (rather than undecorated) you'll have to write your own LookAndFeel.

Or try using the "SystemLookAndFeel" - this will roughly look like whichever Operating System you're running on. If that's Windows, it might end up looking a bit like Visual Studio...

Upvotes: 1

Related Questions