3

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!

salocinx
  • 3,715
  • 8
  • 61
  • 110

1 Answers1

4

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();
            }
        });
    }

}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • thanks for your help. your example works fine, as long as the preferred size is not changed. per default the textfields come with a height of 20, if I call textfield.setPreferredSize(new Dimension(200, 22)); then the textfield suddenly shrinks and results in my problem. Is this a bug in Nimbus ? – salocinx Jan 24 '13 at 13:21
  • 2
    @salocinx No it is a bug in your code: you should never call setPreferredSize(). Calling setPreferredSize results in the problems you are observing, it always leads to problems across different L&F. If you want to influence the size of a JTextField, set the number of "Columns" of that textfield but do not call `setPreferredSize()`. – Guillaume Polet Jan 24 '13 at 13:27
  • Thanks for the fast answer. How do I define the height? – salocinx Jan 24 '13 at 13:34
  • 1
    @salocinx You don't. You can modify the font-size and change the border (possibly use a CompoundBorder, check empty- and matte-border) which in the end will modify the height of the textfield. – Guillaume Polet Jan 24 '13 at 13:37