hi there i am working on a memory game project and my main problem is when i start the game user must not see the icons of buttons, when he/she press on a button then the i con should reveal, but some methods like




dont work if i dont create the button with icon. so you can see a very simple code example which explains my problem. and i will appreciate if you can help me. thanks anyway : )

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonsIcon extends JFrame {

    private static final long serialVersionUID = 1L;
    Icon icon = new ImageIcon("images\\dino1.gif");
    private ImageIcon infoIcon = (ImageIcon) UIManager.getIcon("OptionPane.informationIcon");
    private ImageIcon warnIcon = (ImageIcon) UIManager.getIcon("OptionPane.warningIcon");

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                ButtonsIcon t = new ButtonsIcon();

    public ButtonsIcon() {
        setLayout(new GridLayout(1, 1, 4, 4));

        final JToggleButton toggleButton = new JToggleButton();
        toggleButton.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                if (toggleButton.isSelected()) {


A simple solution may be to create a blank icon of exactly the same size, and just switch the icons when you want to show/hide the real one.

This article has been dead for over six months. Start a new discussion instead.