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()) {


Recommended Answers

All 2 Replies

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.

@quartaela are you everywhere ???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.