Access Jlabels without using switch case...

Asked

Viewed 61 times

0

I’m developing a game with JLabel from 1 to 50, I can access them online without having to use a switch.

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

There is no problem in the code below, I just want to use something like (I’ll make it up because this is exactly what I want) lblBoard[points].setIcon. One more detail as I created these JLabel in drawing time I didn’t 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;
        }

If you clarify more the question this is the screen of my game. I wanted to move the players house by house. It is run by swicth but the code is not over. 51 Labels.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • What problem are you having ? switch case no need to open and close {} because there already exists the break.

  • Maybe I haven’t been clear, I don’t have code problem. I would like to use something like lblBoard[i]. setIcon to help me not have to make the switch case.

  • still thanks for the tip...

  • Please provide a [mcve] so that it is possible to test the code.

1 answer

1


The simplest solution I see is to work with an array of Jlabels and then just access the JLabel desired, using the variable points. But for that, you’ll need to tweak the code, not the netbeans screen builder.

Install an array of Labels at the beginning of your code:

JLabel[] labelBoards = new JLabel[51];

You can use a loop to start, set up and fill each of them 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 represent the position of JLabel desired 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, would not need to loop in this case, only this snippet be called each time it changes value.

Just note that although you are 51 JLabels in the array, the indexes range from 0 to 50, hence the variable points can only have values only within this range, otherwise will burst exception ArrayIndexOfBoundException.


There is another way, applying the tip of this answer, which consists of taking the components and filtering when it is an instance of JLabel and put them in a ArrayList or an array even, with your screen already created the way it is now, but in your case, doing so would be a workaround well ridiculous, because it would be the same as creating a code patch to get another poorly planned code. The recommended solution would be the same array, would give more work but at least your code gets better with better organized logic.

Without more details it is difficult to suggest a solution other than an array, if you cannot implement it, edit the question and provide an Minimum, Complete and Verifiable Example so that it is possible to see the code working and suggest something alternative.

  • Thanks @Articuno. From what I understand there is no option I would like. I have to create jLabels by line of code. I don’t know which is easier, create a difficulty at the beginning to draw the screen or draw via netbeans handle without being array. Even so thank you very much, really that’s what I had thought, there is no solution for when creating via drawing in netbeans. (I say this because it is much easier to draw via netbeans than via code, that I have to determine the position, size, all the characteristics of each of the jLabels and it is nothing visual). Thank you very much.

  • @Adrianomenezes there are other ways without array, but Oce would need to change your screen anyway. If you present your code in the form of a [mcve] I can see if it is possible to suggest something on top of your code.

  • as I am beginner I have doubt how to do complete (I imagined it was complete kkk) are three . java has how I post them?

  • @Adrianomenezes to make a minimal, complete and verifiable example you must isolate only the problem. In your case, Voce will probably have to create.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.