Leave program done in Swing with Windows appearance

8

I'm developing application with Java Swing, but the screens are not getting windowed out of windows. Example screen.

I wanted to leave Windows with the same face, with the title bar with minimize button, maximize and close. The way it is is not visually pleasing.

I'm using NetBeans.

Thank you in advance.

    
asked by anonymous 20.08.2016 / 04:20

1 answer

5

This " LookAndFeel " is Nimbus, which netbeans automatically configures when you are using the graphing tool it has. To change the LAF (LookAndFeel), just go in the code of main , and find this section here:

try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            javax.swing.UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

Changing the if line to:

if ("Windows".equals(info.getName()))

or even

if ("Windows Classic".equals(info.getName()))

The first style will try to adapt the appearance to the current version of windows that is running the application, the second will try to adapt to a classic version, similar to windows 98.

It is possible to remove that for loop within try/catch , and make the call below, which will invoke the system theme currently used:

try {
    javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());

} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

Remember that if you run your application in some OS different from windows, java will change to the theme "Metal", which is the only "cross platform" theme that comes embedded in the JDK. In order to look like windows regardless of system, you may need to create your own LAF

In this link there is a list of other LAF's existing in java-swing.

    
20.08.2016 / 15:26