Hi!

I'm trying to create borders around JLabel components. However, the line c[i].setBorder(border) cannot be compiled. Please, help me to improve this code. Thanks!

public void mouseReleased(MouseEvent e) {
        if (Menu.getgroup()) {
            Component[] c = getComponents();
            Border border = LineBorder.createGrayLineBorder();
            for(int i = 0; i < c.length; i++)
            {
                if(c[i] instanceof JLabel)
                {
                    Rectangle selectedLab = c[i].getBounds();
                    if(selection.contains(selectedLab))
                    {
                        c[i].setBorder(border);
                    }
                }
            }
            selection = null;
            repaint();
        }
    }

Recommended Answers

All 2 Replies

Because Component is an awt element not a swing element. Cast to JComponent.

Ok, the following line ((JComponent)(c[i])).setBorder(border); works! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.