Java SE 6: Why is Nimbus L&F cutting 4 pixels in a JTextField?

My JFrame consists of some JTextFields. For Windows OS, I just use the look & feel system. For Linux distros, I am using Nimbus L&F that comes with Java SE 6.

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

Does anyone know how to get rid of this white border in text boxes?

Here's an illustration of the problem:

enter image description here

Here's the 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;

}

      

Thank you very much in advance!

+3


source to share


1 answer


I have no such problem with a halo. You probably have something else that is causing this problem. Check out the example below.

UPDATE



You should never call setPreferredSize()

for any components (I really mean that). This always leads to problems in different looks and feelings. If you want to specify the width of the textbox 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();
            }
        });
    }

}

      

+4


source







All Articles