-1

Every time I run my Java SE(swing) program it runs with Nimbus Look and Feel, but my design is based on Windows Look and Feel, how can I make it so that my default program running has Windows look and feel and not Nimbus?

I have searched in few places, they are saying to change "Nimbus" to "Windows" which does not work for me.

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 ex) {
    java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I think you're looking for [`getSystemLookAndFeelClassName()`](https://docs.oracle.com/javase/7/docs/api/javax/swing/UIManager.html#getSystemLookAndFeelClassName()), although this would only work if the program was running on a windows machine. – Vince May 19 '18 at 21:17
  • @VinceEmigh UIManager.getSystemLookAndFeelClassName(); returns **com.sun.java.swing.plaf.windows.WindowsLookAndFeel**. My code is:- `try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e){e.printStackTrace();}` It is not working in Netbeans and gives the same Nimbus LookAndFeel. – Jagan Mohan Sahu May 19 '18 at 21:48
  • 1
    Are you applying this code before deploying the GUI? Is there an error? "*Not working*" doesn't give us enough information to help you. I recommend creating an [MCVE](https://stackoverflow.com/help/mcve) – Vince May 19 '18 at 21:50
  • *"but my design is based on **Windows** Look and Feel"* What does the 'based on' mean? Does the app. use layout managers? And further, what is supposed to happen on Mac OS and *nix machines? The Windows PLAF will not be available on either (fortunately - given it would irritate those users). – Andrew Thompson May 20 '18 at 05:42

1 Answers1

0

Replace...

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 ex) {
    java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

With something more like...

try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(ValidFileGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
}

You can read How to Set the Look and Feel more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Apparently he already tried this (check out the comments). Isn't it possible for the L&F to not appear if you change it *after* the window has been displayed, or am I remembering it incorrectly? – Vince May 19 '18 at 23:21
  • @VinceEmigh You are right, but it doesn't change the fact that this is how to change the look and feel - if the OP is still getting Nimbus, then they've established the look and feel before showing anything (as Metal is the default) - more likely they need to do a clean and build - You can use `updateUI` to apply the changes - but it's best to establish the L&F before hand – MadProgrammer May 19 '18 at 23:48