How to make a swing application have dark nimbus theme netbeans

Viewed 12987

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 Answers

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!");
}
Related