Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 333
Markus V. Avatar asked Sep 06 '25 12:09

Markus V.


2 Answers

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.

like image 99
Björn Avatar answered Sep 10 '25 10:09

Björn


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!
    }
};
like image 33
Tom Hawtin - tackline Avatar answered Sep 10 '25 10:09

Tom Hawtin - tackline