iridescent
iridescent

Reputation: 305

Hiding setText value of a JButton

I am coding an image puzzle game and one part of the code is to compare the pieces the user has selected to the pieces of the correct image.

Each image piece is already added to a JButton as an ImageIcon.

An identifier is required to differentiate each image piece apart and also for comparision.

I am setting a setText() for each JButton created as the identifier.

But doing so will cause both the ImageIcon and setText to show up on the JButton.

Is there a way to hide the setText value and only display the ImageIcon ?

        private String id;
        private int cc;
        private JButton[] button = new JButton[9];

        cc += 1;
        id += ""+cc;

        for(int a=0; a<9; a++){
            // dd refers to the variable for the image pieces for each JButton
        button[a].setIcon(new ImageIcon( dd ));
        button[a].setText(id);
        }

Upvotes: 2

Views: 554

Answers (1)

tckmn
tckmn

Reputation: 59273

I recommend making another array of Strings:

String[] ids = new String[button.length];

And then button[a]'s ID would be ids[a].

Here is your code with the change:

    private String id;
    private int cc;
    private JButton[] button = new JButton[9];
    private String[] ids = new String[button.length];

    //cc += 1;
    //id += ""+cc;
    id += Integer.toString(++cc); //easier way

    for(int a=0; a<9; a++){
        // dd refers to the variable for the image pieces for each JButton
        button[a].setIcon(new ImageIcon( dd ));
        ids[a] = id;
    }

Upvotes: 2

Related Questions