Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset/remove a border in Swing

Here's a very specific coding question:

I've recently been asked to maintain some old-ish Java Swing GUI code at work and ran into this problem:

I've attached my own subclass of InputVerifier called MyFilenameVerifier to a JTextField (but it may as well be any JComponent for these purposes). I've overridden the verify() method such that it calls super.verify(input) (where input is the JComponent parameter to verify()). If super.verify(input) comes back false, I do:

input.setBorder(BorderFactory.createLineBorder(Color.RED));

This is a convention used throughout the UI of this application that started long before me, so I don't have a lot of choice as far as using other ways to get the users attention (wish I did). This is just the way it works.

Problem is, once the user goes back and types something valid into the text field, I need a way to set it back to default border (instead of just saying set it to Color.GRAY or whatever, which is a different color from its original border). I need a way to say, "remove the extra decoration and go back to normal" or just set the border to its default, in other words.

like image 595
Troy Nichols Avatar asked Dec 12 '25 20:12

Troy Nichols


1 Answers

Couldn't you just call input.getBorder() and cache it somewhere before setting the border to red?

like image 143
Michael Myers Avatar answered Dec 15 '25 10:12

Michael Myers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!