26

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.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
Markus V.
  • 347
  • 2
  • 4
  • 6
  • would be interesting to know what system/environment you are using. Or there is more code involved... It (again) works perfectly for me. See this image with some results (and code): http://img709.imageshack.us/img709/9034/fields.jpg – user85421 Feb 17 '10 at 16:15

7 Answers7

74
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.

Björn
  • 29,019
  • 9
  • 65
  • 81
  • 10
    +1 for mentioning the reason WHY null does not work as expected – Nils Schmidt Feb 17 '10 at 15:37
  • should be -1. because it does not work. the solution that works is the one suggested that says: override setBorder to do nothing – Markus V. Feb 17 '10 at 15:40
  • 2
    This may or may not work depending upon context, for non-obvious reasons. http://stackoverflow.com/questions/2281539/setborder-on-jtextfield-does-not-work-or-does-it However, it is good information and I don't see any reason for downvoting it. – Tom Hawtin - tackline Feb 17 '10 at 15:55
  • As Tom says, this may work upon context. Given the context from OP, this would work splendidly. Thanks Tom, btw. :) – Björn Feb 17 '10 at 15:56
23

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!
    }
};
Community
  • 1
  • 1
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
3

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?

Uri
  • 88,451
  • 51
  • 221
  • 321
  • 3
    @Markus, in my experience with Swing, problems with it are often not limited to one issue, but rather have to be peeled like an onion. without seeing your entire code, I don't know if this would completely solve the problem. If I was dealing with the bug, this would be the first thing I would try. If it didn't work, I'd try to investigate whether the look-and-feel (if you're using something fancy) is interfering. – Uri Feb 17 '10 at 15:51
3

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);
Oleg
  • 9,341
  • 2
  • 43
  • 58
stuklist
  • 39
  • 1
1
txt.setBorder(new LineBorder(Color.BLACK,0));

might work.

gerardw
  • 5,822
  • 46
  • 39
0

You can simply

textField.setBorder(null);

or

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

Divin Irakiza
  • 317
  • 1
  • 7
-1

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.

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Tomek
  • 181
  • 1
  • 3