954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Memory Game JToggleButton icon problem

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);
    }
}
quartaela
Newbie Poster
6 posts since Apr 2011
Reputation Points: 10
Solved Threads: 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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

@quartaela are you everywhere ???

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: