Unable to remove JLabel from JPanel

SKLAK

I have a chessboard with 64 JPanels representing each square on the board. The pieces are represented using JLabels which are placed on the JPanels. I am trying to remove all the JLabels off the board. I am confused why this doesn't work:

private void removePieces()
{
    for(int i = 0; i < 64; i ++)
    {       
        Component c = chessBoard.getComponent(i);
        if(c instanceof JLabel)
        {
            Container parent = c.getParent();
            parent.remove((JLabel)c);
            parent.revalidate();
            parent.repaint();
        }
    }
}

chessboard is the big JPanel with the 64 JPanels inside it. After some debugging it looks like the if loop is never being entered. I don't understand why it wouldn't enter the if loop if one of the components is a JLabel?

Rudi Kershaw

Looks like your trying to remove your JPanels from your chessboard if they are JLabels (which obviously makes no sense, and is why the if code is never firing). Instead you want to remove the chessBoard's components' JLabel component. Example below.

private void removePieces() {
        for(int i = 0; i < 64; i ++) {   
            if(chessBoard.getComponent(i) instanceof JPanel) {
            JPanel c = (JPanel)chessBoard.getComponent(i);
            c.removeAll();
            c.revalidate();
            c.repaint();
            }
        }
    }

I am using removeAll() because I am presuming your JPanels have no other components in them other than the potential JLabels.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to remove component from jpanel

From Dev

Removing unspecific JLabel from JPanel

From Dev

Remove the underline from a JLabel text

From Dev

JLabel not Truncating Within JPanel

From Dev

How should I drag a JLabel from one JPanel in a JFrame onto a JTextField in another JPanel in the same JFrame?

From Dev

How should I drag a JLabel from one JPanel in a JFrame onto a JTextField in another JPanel in the same JFrame?

From Dev

Remove maximize, minimize, close button from JPanel

From Dev

How to align a JLabel to the bottom of a JPanel

From Dev

Can't add JLabel to JPanel

From Dev

Positioning JLabel in JPanel below the image

From Dev

Can JLabel replace Jpanel as container?

From Dev

Can't add JLabel to JPanel

From Dev

Unable to remove "Optional" from String

From Dev

Unable to remove symlink from cygwin

From Dev

Unable to remove an item from lsitviewbox

From Dev

Unable to remove control @ from file

From Dev

How would I dynamically remove swing component from JPanel

From Dev

How do I remove this JPanel from my JFrame?

From Dev

JLabel/JPanel positioning issues. (GridBagLayout)

From Dev

How to add a JLabel to a class which extends JPanel

From Dev

put JButton on the top of JLabel inside JPanel

From Dev

Only able to add JLabel background or JPanel background

From Dev

Set specific location of multi JLabel in JPanel

From Dev

Positioning of JButtons in a JPanel with BoxLayout over JLabel in JPanel with Overlaylayout

From Dev

Unable to draw over a JPanel

From Dev

Unable to display image on JPanel

From Dev

add and remove element to jPanel

From Dev

How to dynamically remove a JPanel?

From Dev

Unable to access jlabel across the program

Related Related

HotTag

Archive