Search code examples
javaswingjtextfieldnimbus

Java SE 6: Why does Nimbus L&F cut for 4 pixels in JTextField?


My JFrame consists of some JTextFields. For Windows OS I simply use the system look&feel. For Linux distributions I use Nimbus L&F shipped with Java SE 6.

In Nimbus L&F, the lower border of the JTextField cuts-off 4 pixels. Thus, letters like "g" are cutted and look like an "a".

Does somebody know how to get rid of this white border within the text fields?

Here's an illustration of the issue:

enter image description here

Here's an SSCCE (example code):

public class NimbusTextFieldIssue extends javax.swing.JFrame {

    public NimbusTextFieldIssue() {
        initComponents();
    }

    private void initComponents() {

        txtField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtField.setText("The letters \"g\" and \"p\" are not shown correctly.");
        txtField.setPreferredSize(new java.awt.Dimension(234, 22));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }


    public static void main(String args[]) {
        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) {

        } catch (InstantiationException ex) {

        } catch (IllegalAccessException ex) {

        } catch (javax.swing.UnsupportedLookAndFeelException ex) {

        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NimbusTextFieldIssue().setVisible(true);
            }
        });
    }

    private javax.swing.JTextField txtField;

}

Many thanks in advance!


Solution

  • I don't have this kind of issue with Nimbus. You probably have something else that causes this problem. Check out the example below.

    UPDATE

    You should never call setPreferredSize() on any components (I really mean that). It always leads to problem across different look and feels. If you want to indicate the width of the textfield, use JTextField.setColumns(int).

    import java.awt.FlowLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UIManager.LookAndFeelInfo;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestNimbusTextField {
    
        private void initUI() {
            JFrame frame = new JFrame(TestNimbusTextField.class.getSimpleName());
            frame.setLayout(new FlowLayout());
            JTextField textfield = new JTextField(20);
            textfield.setText("good morning. Look like I have no problems with 'g' and 'p'.");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(textfield);
            frame.setSize(300, 300);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
                UnsupportedLookAndFeelException {
            LookAndFeelInfo[] installedLookAndFeels = UIManager.getInstalledLookAndFeels();
            for (LookAndFeelInfo lafInfo : installedLookAndFeels) {
                if (lafInfo.getName().equals("Nimbus")) {
                    UIManager.setLookAndFeel(lafInfo.getClassName());
                    break;
                }
            }
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TestNimbusTextField().initUI();
                }
            });
        }
    
    }