Leave program done in Swing with Windows appearance

Asked

Viewed 2,380 times

8

I am developing application with Java Swing, but the screens are not looking windows window. Example of screen.

inserir a descrição da imagem aqui

I wanted to look like Windows even, with the title bar with minimize, maximize and close button. The way it is is not visually pleasant.

I am using Netbeans.

Thank you in advance.

  • 3

    You can define this using UIManager#setLookAndFeel taking the "style" of the OS in which the application is running. But you want your application to have "Windows face" on all operating systems or only on Windows same?

  • Valew man, I’ll try to use

1 answer

5


This "Lookandfeel" is Nimbus, which netbeans automatically configures when you are using the graphical creation tool it has. To change the LAF(Lookandfeel), just go to the code of main, and find this passage 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 loop for within the 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);
}

Remembering that, if you run your application in some OS other than windows, java will change to the theme "Metal", which is the only theme "Cross Plataform" that comes embedded in JDK. To have the appearance of windows, independent of the system, you may need create a LAF of its own

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

  • Ball show, very well explained. I will test and replenish you. Vlw

  • @Rodrigostortideoliveira if the answer serves you, do not forget to mark it as accepted, to serve as reference for other users :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.