I'm trying to set the border of a JPanel by using a child textfield. I've tried using getParent(), but the option for setting the border does't exist. What is the reason for this?
Asked
Active
Viewed 58 times
0
-
Why not ensure that the action listener has a reference to the panel? BTW - what form is the information on the border, in the text field? If it is a number (for example) that is used in the constructor of an `EmptyBorder`, it would probably be more user friendly to have a `JSpinner` with a `SpinnerNumberModel` as shown in [this answer](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Mar 28 '18 at 05:29
1 Answers
1
The getParent()
method returns a Container
object.
The setBorder(...)
method is only defined for JComponent
objects.
So you need to cast the parent Container
to a JPanel
. Something like:
Container parent = textField.getParent();
JPanel panel = (JPanel)parent;
panel.setBorder( new LineBorder(Color.RED) );

camickr
- 321,443
- 19
- 166
- 288