Search code examples
javaswingjpaneljtextfield

Trying to set border on a JPanel using a child textfield


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?


Solution

  • 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) );