4
votes

So I am new to Netbeans and Swing GUI development in general and I am trying to change the Look and Feel of a JFrame. When I create a JFrame form Netbeans by default make it Nimbus theme.

I tried to change to the Windows theme (if ("Windows".equals(info.getName())) {) and the Metal theme (if ("Metal".equals(info.getName())) {) and it worked flawlessly with these 2 themes.

But when I try to change it to the Dark Nimbus theme (if ("Dark Nimbus".equals(info.getName())) {) it didn't work.

I also tried doing Right Click > Preview Design > Dark Nimbus and yes it previews the Dark Nimbus theme as expected. But not when I actually compile and run the program (by clicking the play button).

Does anyone know how to change the theme to "Dark Nimbus"?

Here is the relevant code:

    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Dark Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(TestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(TestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(TestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(TestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
    //</editor-fold>
2
Dark Nimbus You mean the Sith? No.. wait.. that's Darth Nimbus. What's Dark Nimbus? BTW - did you consider printing all the alternatives and looking at what is available?Andrew Thompson

2 Answers

14
votes

I recently had the same concern and I found these excelent tips from Geertjan Wielenga and from Neil C Smith. So the answer to your question would be:

public static void main(String[] args) {
  UIManager.put( "control", new Color( 128, 128, 128) );
  UIManager.put( "info", new Color(128,128,128) );
  UIManager.put( "nimbusBase", new Color( 18, 30, 49) );
  UIManager.put( "nimbusAlertYellow", new Color( 248, 187, 0) );
  UIManager.put( "nimbusDisabledText", new Color( 128, 128, 128) );
  UIManager.put( "nimbusFocus", new Color(115,164,209) );
  UIManager.put( "nimbusGreen", new Color(176,179,50) );
  UIManager.put( "nimbusInfoBlue", new Color( 66, 139, 221) );
  UIManager.put( "nimbusLightBackground", new Color( 18, 30, 49) );
  UIManager.put( "nimbusOrange", new Color(191,98,4) );
  UIManager.put( "nimbusRed", new Color(169,46,34) );
  UIManager.put( "nimbusSelectedText", new Color( 255, 255, 255) );
  UIManager.put( "nimbusSelectionBackground", new Color( 104, 93, 156) );
  UIManager.put( "text", new Color( 230, 230, 230) );
  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 e) {
    e.printStackTrace();
  } catch (InstantiationException e) {
    e.printStackTrace();
  } catch (IllegalAccessException e) {
    e.printStackTrace();
  } catch (javax.swing.UnsupportedLookAndFeelException e) {
    e.printStackTrace();
  } catch (Exception e) {
    e.printStackTrace();
  }
  // Show your JFrame
}
0
votes

The code runs better in this state:

// Dark LAF
try {
    UIManager.setLookAndFeel(new NimbusLookAndFeel());
    UIManager.put("control", new Color(128, 128, 128));
    UIManager.put("info", new Color(128, 128, 128));
    UIManager.put("nimbusBase", new Color(18, 30, 49));
    UIManager.put("nimbusAlertYellow", new Color(248, 187, 0));
    UIManager.put("nimbusDisabledText", new Color(128, 128, 128));
    UIManager.put("nimbusFocus", new Color(115, 164, 209));
    UIManager.put("nimbusGreen", new Color(176, 179, 50));
    UIManager.put("nimbusInfoBlue", new Color(66, 139, 221));
    UIManager.put("nimbusLightBackground", new Color(18, 30, 49));
    UIManager.put("nimbusOrange", new Color(191, 98, 4));
    UIManager.put("nimbusRed", new Color(169, 46, 34));
    UIManager.put("nimbusSelectedText", new Color(255, 255, 255));
    UIManager.put("nimbusSelectionBackground", new Color(104, 93, 156));
    UIManager.put("text", new Color(230, 230, 230));
    SwingUtilities.updateComponentTreeUI(this);
} catch (UnsupportedLookAndFeelException exc) {
    System.err.println("Nimbus: Unsupported Look and feel!");
}