Search code examples
javaswingjtextfield

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.


Solution

  • 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!
        }
    };