0

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

.setPressedIcon()

,

.setRollOverIcon()

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

            @Override
            public void run() {
                ButtonsIcon t = new ButtonsIcon();
            }
        });
    }

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


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

            @Override
            public void itemStateChanged(ItemEvent e) {
            	
                if (toggleButton.isSelected()) {
                	
                }
            }
        });
        add(toggleButton);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
}
3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by mKorbel
0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.