Enable a JButton when some component changed its border color [closed]

Tags: , , , ,

My goal is to enable a JButton only when some JTextFields and JComboBox change its border color from red to green.
These components are included in three different JPanel.
I try to create a function that reads all components in a JPanel but, when I’m going to compared the colors, the program returns me that I’m casting the variables in a bad way.
Below there’s my function.
Can someone help me?

    public static boolean countBoards(JPanel panel){
        boolean red = false;
        for(Component control : panel.getComponents())
            if(control instanceof JTextField)
                JTextField ctrl = (JTextField) control; 
                Color lineColor = ((LineBorder)ctrl.getBorder()).getLineColor();
                    red = true;                
            else if(control instanceof JComboBox)
                JComboBox ctr = (JComboBox) control; 
                Color lineColor = ((LineBorder)ctr.getBorder()).getLineColor();
                    red = true;               
        return red;


When you change the border of a component, a property listener will be fired. You can register a property listener to the combobox/textfield and enable/disable the button according to the new border.

An example:

public void test() {
    JButton myButton = new JButton();
    JComboBox<String> combo = new JComboBox<>();
    combo.addPropertyChangeListener("border", e -> {
        if (e.getNewValue() != null && e.getNewValue() instanceof LineBorder) {
            LineBorder border = (LineBorder) e.getNewValue();
    assertTrue(myButton.isEnabled(), "Button should be initially enabled.");

    assertFalse(myButton.isEnabled(), "Button should be disabled when red line border occurs.");

    assertTrue(myButton.isEnabled(), "Button should be enabled when green line border occurs.");

Source: stackoverflow