Reputation: 357
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
Reputation: 357
You can simply
textField.setBorder(null);
or
textField.setBorder(new EmptyBorder(0, 0, 0, 0))
Upvotes: 0
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
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
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
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
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