Search code examples
javauser-interfacenetbeanslook-and-feel

Why does my GUI never look right?


Built using Netbeans GUI builder on a Mac in Java.

This is what the GUI looks like in Netbeans:

Netbeans GUI

When I click preview, it doesn't look too bad, but there are small changes:

GUI Preview

Lastly, when I run it, it looks like this: - terrible

Look and feel

I assume it's to do with Java's 'Look and Feel'. I've tried removing this and the GUI becomes a shambles. So what are my options? As you can see everything lines up in Netbeans, when I run it, everything is a mess.

Any ideas? Spent a day messing around with this and i'm fed up to say the least


Solution

  • Some where in you code, Netbeans would have created a main method (this will be within the class that Netbeans exeuctes and is probably your master frame - I think that would be the SPPMainGUI)

    It will look something like this (note that it might be "folded" so you will only see the first comment and the decs of the second (Look and feel setting code (optional)))

    /* 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 ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
    

    Unfold the code block by click on the little + in the margin and replace the for-loop with UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); as below

    /* 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 {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
    

    As afsantos identified the problem, I would appriciate it if you could accept his answer as been correct (and an up-vote me instead ;), nudge, nudge, wink, wink, say no more)