0

I am currently working on a Black Jack game in Java. I am trying to work with Swing and I am stuck right now: I made this method

private void addImage(String path) {

        JLabel imgLabel = new JLabel();
        imgLabel.setIcon(new ImageIcon(getClass().getResource(path)));
        add(imgLabel);
        display.pack();

    }

I want to have a reset button to restart after a round ends, how can I remove these unspecific JLabels? Should I give them an identifier? If yes how? Thanks in advance.

  • *"how can I remove these unspecific JLabels?"* joshpetit identified how to make them .. specific using collections, but just to point out. A `JLabel` with no text or icon is invisible, so .. don't remove them! Instead: 1) establish and add the labels when the container is created, put them in a collection (or array) for later referencing. 2) Use them as needed for whatever task the end user is doing. 3) To clear the labels, loop the array and `setIcon(null)` / `setText("")`. – Andrew Thompson Jun 12 '20 at 00:12
  • I like the idea of just making them invisible, thanks :) – XxDiCaprioxX Jun 12 '20 at 10:00

1 Answers1

-2

You can do this by creating a list that contains all the labels.

List<JLabel> labelsList = new ArrayList<JLabel>();

Make sure to declare this outside of the addImages method so that it can be accessed in other methods. Then, every time an image label is created you can add it to the list by using:

labelsList.add(imgLabel)

To remove the cards, you could implement a reset method that loops through the labels list and removes each component, making sure to also remove it from the labelsList:

public void resetLabels()
{
    for (JLabel label : labelsList) {
        remove(label);
        labelsList.remove(label);
    }
}

I think learning about collections would be very helpful in this case, keep up the good work!

EDIT: After a closer look, you shouldn't modify the list as you go through the loop, instead:

public void resetLabels()
{
    for (JLabel label : labelsList) {
        remove(label);
    }
    labelsList.clear();
}
joshpetit
  • 677
  • 6
  • 17
  • Thanks for your answer, I don't know why I didn't think of it. One question, how exactly does the reset method work (I mean if it works it works but I want to understand it so I can learn from it)? What does ``` for (JLabel label : labelsList)``` mean/do? Thanks in advance :) – XxDiCaprioxX Jun 12 '20 at 09:57
  • @XxDiCaprioxX This is something called an enhanced for loop, it goes through every single item in the specified list and on each iteration, you can use that variable, in this case, we use the remove method to take that specific label off of the JFrame. You can read more about this [here](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html). – joshpetit Jun 14 '20 at 01:04
  • Thanks a lot, this is actually very useful for me on some other occasions, too. – XxDiCaprioxX Jun 14 '20 at 01:10
  • This is the wrong approach and will result in a `ConcurrentModificationException`. See this [post](https://stackoverflow.com/a/62459092/201359) for a correct solution. – Óscar López Jun 19 '20 at 12:15