0

Do I can fill 2D array with icon file names like this? Im getting error that says

Type mismatch: cannot convert from String to Icon

So Question is, do imposible fill 2D array with icon file names, or I made small error here? I keep getting error only on array.

 public Icon cards[][] = {{"Diamonds 2.png", "Diamonds 3.png", "Diamonds 4.png", "Diamonds 5.png", "Diamonds 6.png", "Diamonds 7.png", "Diamonds 8.png",
                "Diamonds 9.png", "Diamonds 10.png", "Diamonds JACK.png", "Diamonds QUEEN.png", "Diamonds KING.png", "Diamonds ACE.png"},
                {"Hearts 2.png", "Hearts 3.png", "Hearts 4.png", "Hearts 5.png", "Hearts 6.png", "Hearts 7.png", "Hearts 8.png",
                "Hearts 9.png", "Hearts 10.png", "Hearts JACK.png", "Hearts QUEEN.png", "Hearts KING.png", "Hearts ACE.png"},
                {"Clubs 2.png", "Clubs 3.png", "Clubs 4.png", "Clubs 5.png", "Clubs 6.png", "Clubs 7.png", "Clubs 8.png",
                "Clubs 9.png", "Clubs 10.png", "Clubs JACK.png", "Clubs QUEEN.png", "Clubs KING.png", "Clubs ACE.png"},
                {"Spades 2.png", "Spades 3.png", "Spades 4.png", "Spades 5.png", "Spades 6.png", "Spades 7.png", "Spades 8.png",
                "Spades 9.png", "Spades 10.png", "Spades JACK.png", "Spades QUEEN.png", "Spades KING.png", "Spades ACE.png"}};

I have changed array name to check, maybe when im using it, that cause problem, but no, its same error on array.

2
  • You can either change array to be a string or change values to be of type Icon (new ImageIcon(imgURL, description);) Commented Aug 19, 2013 at 18:51
  • If I change it to String, then i cant use it as icon when I setting it on label. @Reimeus Commented Aug 19, 2013 at 18:51

1 Answer 1

1

As others have said, you are creating a 2D array of strings.

So, change your 2D array definition to:

public String[][] cards = //...

Then, iterate over your 2D array, and load an icon for each string:

Icon[][] icons = new Icon[cards.length][];
for(int i=0;i<cards.length;i++){
    icons[i] = new Icon[cards[i].length];
    for(int j=0;j<cards[i].length;i++){
        icons[i][j] = //load icon from cards[i][j];
    }
}

And use the icons 2D array to set icons on your labels you mentioned in comments.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.