Access JLabels without using switch ... case

0

I'm developing a game with JLabel 1 to 50, I can access them in line of code without having to use a switch.

You can verify that what changes in the code and only lblBoard1 or lblBoard2 and so on, who will determine JLabel is the points variable.

There is no problem in the code below, I would just like to use something like (I'm going to invent because that's exactly what I want) lblBoard[points].setIcon . One more detail how I created these JLabel at drawing time did not see how to create an array of them in the netBeans interface. What I really have are JLabel's ending with numbers lblBoard1 , lblBoard2 , and so on.

 switch (points){
        case 1:{
            if (samePlace){
                lblBoard1.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay12) ) ));
            } else {
                if (player == "Player 1"){
                    lblBoard1.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay1) ) ));
                } else {
                    lblBoard1.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay2) ) ));
                }
            }
            break;
        }
        case 2:{
            if (samePlace){
                lblBoard2.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay12) ) ));
            } else {
                if (player == "Player 1"){
                    lblBoard2.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay1) ) ));
                } else {
                    lblBoard2.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay2) ) ));
                }
            }
            break;
        }
        case 3:{
            if (samePlace){
                lblBoard3.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay12) ) ));
            } else {
                if (player == "Player 1"){
                    lblBoard3.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay1) ) ));
                } else {
                    lblBoard3.setIcon(new ImageIcon(ImageIO.read( new File(imagePlay2) ) ));
                }
            }
            break;
        }

In case the question is elucidated, this is the screen of my game. wanted to move the players house to house. This works by doing the swicth but the code is not over. 51 Labels.

    
asked by anonymous 04.02.2018 / 07:39

1 answer

1

The simplest solution I see is to work with an array of JLabels and then only access the index of the JLabel desired, using the variable points . But for this, you'll need to tinker with the code, not the netbeans screen builder.

Instantiate an array of labels at the beginning of your code:

JLabel[] labelBoards = new JLabel[51];

You can use a loop to start, configure, and populate each on your screen:

for(int i = 0; i < labelBoards.lenght; i++) {
    JLabel label = new JLabel();
    // aqui voce configura o label do jeito que precisar
    labelBoards[i] = label;

}

And then to access, just keep in mind that the variable points will represent the position of the desired JLabel in the array:

if (samePlace){
    labelBoards[points].setIcon(new ImageIcon(ImageIO.read(new File(imagePlay12))));
} else {
    if (player == "Player 1"){
        labelBoards[points].setIcon(new ImageIcon(ImageIO.read(new File(imagePlay1))));
    } else {
        labelBoards[points].setIcon(new ImageIcon(ImageIO.read(new File(imagePlay2))));
    }
}

Remembering that if points is incremented elsewhere, you would not need a loop in this case, only this section will be called each time it changes value.

Note that even though you have 51 JLabels in the array, the indexes range from 0 to 50, so the variable points can only have values only within that range, otherwise it will pop up exception ArrayIndexOfBoundException .

There is another way by applying the hint , which consists of picking up the components and filtering when an instance of JLabel and put them in a ArrayList or in an array, with your screen already created the way you are now, but in your case doing that would be a pretty ridiculous workaround, because it would be the same as creating a code patch to fix another badly planned code. The recommended solution would be the same array, would give more work but at least your code gets better with the better organized logic.

Without further details, it is difficult to suggest a solution other than array, if you can not implement it, edit the question and provide a Minimum, Full, and Verifiable example so you can see the code working and suggest something else.

    
04.02.2018 / 11:58