Markus V.
Markus V.

Reputation: 357

Swing JTextField how to remove the border?

Is there anyway to remove a border in a JTextField?

txt = new JTextField();
txt.setBorder(null);   // <-- this has no effect.

I would really want it to look like a JLabel - but I still need it to be a JTextField because I want people to be able highlight it.

Upvotes: 27

Views: 72413

Answers (7)

Divin Irakiza
Divin Irakiza

Reputation: 357

You can simply

textField.setBorder(null);

or

textField.setBorder(new EmptyBorder(0, 0, 0, 0))

Upvotes: 0

gerardw
gerardw

Reputation: 6330

txt.setBorder(new LineBorder(Color.BLACK,0));

might work.

Upvotes: 1

stuklist
stuklist

Reputation: 39

No you can't remove the border. Especially over the display of the AWT components. They use the native widget set (are drawn outside Java).

Try to make the line that is similar to your background... for example if your background is white then you have to:

setBorder(BorderFactory.createLineBorder(Color.white));

Then set the background to white:

setBackground(Color.white);

Upvotes: 3

Tomek
Tomek

Reputation: 181

The only way to make it works in ALL circumstances is the following setting:

setBorder (BorderFactory.createLineBorder (new Color (0, 0, 0, 0), 2));

otherwise (when you have null background of the parent container) you will see the "I" cursor remaining forever at the left edge of your JTextField. (Simply make some tests for different border thickness and observe quite strange way the JTextField places the cursor when you activate it first time.)

Alternatively you can set:

setBorder (BorderFactory.createLineBorder (getBackground (), 2));

but you will obtain the field opticaly larger by 2 pixels in all four directions. If you don't specify the border thickness, you will see the cursor BETWEEN this border and the field remaining forever.

Upvotes: -1

Uri
Uri

Reputation: 89749

Try setting it to BorderFactory.createEmptyBorder() instead of null. Sometimes this "does the trick" because setting it to null actually has a different meaning.

If that does not work, it is possible that the look and feel you are using is overriding something. Are you using the default or something custom?

Upvotes: 3

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

From an answer to your previous question you know that some PL&Fs may clobber the border.

The obvious solution is to therefore override the setBorder method that the PL&F is calling, and discard the change.

JTextField text = new JTextField() {
    @Override public void setBorder(Border border) {
        // No!
    }
};

Upvotes: 23

Bj&#246;rn
Bj&#246;rn

Reputation: 29381

JTextField textField = new JTextField();
textField.setBorder(javax.swing.BorderFactory.createEmptyBorder());

http://java.sun.com/javase/6/docs/api/javax/swing/BorderFactory.html

When setting the border to 'null', you're actually telling the look & feel to use the native border style (of the operating system) if there is one.

Upvotes: 77

Related Questions