3

I have to write a card game. When a card is clicked a random card image is generated but because you can only click on the card once, the button is set to be disabled after clicked. How can I stop the card image from going gray once it's clicked so that the new generated card image is clearly visible?

//Actions performed when an event occurs
public void actionPerformed(ActionEvent e)
{

    if (e.getSource() == card1)
    {
    randomInteger();
    card1.setIcon(cardImages[randomInt]);
    card1.setEnabled(false);

    }
    else if (e.getSource() == card2)
    {
    randomInteger();
    card2.setIcon(cardImages[randomInt]);
    card2.setEnabled(false);
    }
    else if (e.getSource() == card3)
    {
    randomInteger();
    card3.setIcon(cardImages[randomInt]);
    card3.setEnabled(false);
    }
    else if (e.getSource() == card4)
    {
    randomInteger();
    card4.setIcon(cardImages[randomInt]);
    card4.setEnabled(false);
    }
    else
    {
    randomInteger();
    card5.setIcon(cardImages[randomInt]);
    card5.setEnabled(false);
    }

}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1823262
  • 31
  • 1
  • 2
  • May be I don't understand clearly what you want to do but I think that better to use JToggleButton for your task – Aliaksei Bulhak Nov 14 '12 at 09:52
  • We have to use a simple JButton that will have a pre-set back of card image, then when you click on the card, a method will generate a random card like king,queen, but once the card is clicked the button is disabled so the image generated is faded. How can I stop that from happening? – user1823262 Nov 14 '12 at 09:56
  • 2
    I think you can't do this. so I advice to use toggle button and write listener which would be checking button position. – Aliaksei Bulhak Nov 14 '12 at 09:59
  • apparently I can remove the action listener and then reapply it when the user clicks reset but it didn't seem to work for me. – user1823262 Nov 14 '12 at 10:03
  • I think it's not good idea to delete actionListener from button – Aliaksei Bulhak Nov 14 '12 at 10:11
  • What about not disabling it, but ignoring all subseqent clicks to the button? – Jakub Zaverka Nov 14 '12 at 10:18
  • use JToggleButton / JButton, doeasn't matter for Icon, but with Swing Action, – mKorbel Nov 14 '12 at 10:31
  • user1823262 post an [SSCCE](http://sscce.org/) otherwise this question isn't answerable, dot ..., hardcode Icon with private Icon loadIcon = UIManager.getIcon("OptionPane.errorIcon"); private Icon saveIcon = UIManager.getIcon("OptionPane.informationIcon"); private Icon subscribeIcon = UIManager.getIcon("OptionPane.warningIcon"); private Icon unsubscribeIcon = UIManager.getIcon("OptionPane.questionIcon"); – mKorbel Nov 14 '12 at 10:49

1 Answers1

14

You simply need to set the disabled icon of the button to the same value as the icon of the button. See this example:

On the left a button where I have set both icon and disabledIcon. On the right I have only set the icon:

Example

import java.awt.BorderLayout;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TestDisabledButtons {

    public static final String CARD_URL = "http://assets0.wordansassets.com/wvc-1345850020/wordansfiles/images/2012/8/24/156256/156256_340.jpg";

    protected void createAndShowGUI() throws MalformedURLException {
        JFrame frame = new JFrame("Test button");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ImageIcon imageIcon = new ImageIcon(new URL(CARD_URL));
        JButton button = new JButton(imageIcon);
        JButton button2 = new JButton(imageIcon);
        button.setDisabledIcon(imageIcon);
        button.setEnabled(false);
        button2.setEnabled(false);
        frame.add(button, BorderLayout.WEST);
        frame.add(button2, BorderLayout.EAST);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new TestDisabledButtons().createAndShowGUI();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • 1
    `plus 1` for sure; but does anyone know how you would prevent standard text on `JButton` from "graying out" – aaiezza Nov 24 '17 at 20:21