3

hi there i am trying to make a matching memory game which i use JToggleButton. the main thing is when i press to button it must show a picture and i must find the other same picture. so the problem is when i create a button without any icons i cant use other other methods for example .setRollOverIcon(), .setPressedIcon() etc. so i appreciated if you can help me . and thanks anyway :)

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

public class ButtonsIcon extends JFrame {

    private static final long serialVersionUID = 1L;
    private ImageIcon errorIcon = (ImageIcon) UIManager.getIcon("OptionPane.errorIcon");
    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.setIcon((errorIcon));
        toggleButton.setRolloverIcon((infoIcon));
        toggleButton.setPressedIcon(warnIcon);
        toggleButton.setDisabledIcon(warnIcon);
        toggleButton.addItemListener(new ItemListener() {

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

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
}
quartaela
  • 2,579
  • 16
  • 63
  • 99

2 Answers2

8

1) for JToggleButton is better to implement ItemListener

2) here is SSCCE

enter image description here

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

public class ButtonsIcon extends JFrame {

    private static final long serialVersionUID = 1L;
    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon = 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(2, 2, 4, 4));

        JButton button = new JButton();
        button.setBorderPainted(false);
        button.setBorder(null);
        button.setFocusable(false);
        button.setMargin(new Insets(0, 0, 0, 0));
        button.setContentAreaFilled(false);
        button.setIcon((errorIcon));
        button.setRolloverIcon((infoIcon));
        button.setPressedIcon(warnIcon);
        button.setDisabledIcon(warnIcon);
        add(button);

        JButton button1 = new JButton();
        button1.setBorderPainted(false);
        button1.setBorder(null);
        button1.setFocusable(false);
        button1.setMargin(new Insets(0, 0, 0, 0));
        button1.setContentAreaFilled(false);
        button1.setIcon((errorIcon));
        button1.setRolloverIcon((infoIcon));
        button1.setPressedIcon(warnIcon);
        button1.setDisabledIcon(warnIcon);
        add(button1);
        button1.setEnabled(false);

        final JToggleButton toggleButton = new JToggleButton();
        toggleButton.setIcon((errorIcon));
        toggleButton.setRolloverIcon((infoIcon));
        toggleButton.setPressedIcon(warnIcon);
        toggleButton.setDisabledIcon(warnIcon);
        toggleButton.addItemListener(new ItemListener() {

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

        final JToggleButton toggleButton1 = new JToggleButton();
        toggleButton1.setIcon((errorIcon));
        toggleButton1.setRolloverIcon((infoIcon));
        toggleButton1.setPressedIcon(warnIcon);
        toggleButton1.setDisabledIcon(warnIcon);
        toggleButton1.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (toggleButton1.isSelected()) {
                } else {
                }
            }
        });
        add(toggleButton1);
        toggleButton1.setEnabled(false);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • i just used toggleButton part for check something bu it doesnt work. i am going to get crazy. i just use simple `toggleButton.setPressedIcon(icon);` and when i press the button there isnt any icons_? – quartaela Oct 24 '11 at 15:53
  • test if Icon is/isn't null, because null Icon doesn't generated any Error, nothing else :-) – mKorbel Oct 24 '11 at 15:56
  • if i use `new JToggleButton(icon);` then there is no problem it shows the icon but otherwise impossible... – quartaela Oct 24 '11 at 16:11
  • @quartaela no idea what's happen ...there must be some another issue with Icon or JToggleButton – mKorbel Oct 24 '11 at 16:52
  • @Andrew Thompson hmmm as I see now I'm wrong with casting (ImageIcon) for plain Icon, – mKorbel Oct 24 '11 at 17:12
  • 2
    Oooh, right you are. In fact I vaguely recall having been bitten by the same thing. I think it was in early versions of the FileBro code. AFAIR it was trashgod that put me onto how to convert the icon to a `BufferedImage` that could then be used for an `ImageIcon` - using the [`Icon.paintIcon(Component,Graphics,int,int)`](http://download.oracle.com/javase/7/docs/api/javax/swing/Icon.html#paintIcon%28java.awt.Component,%20java.awt.Graphics,%20int,%20int%29) method. – Andrew Thompson Oct 24 '11 at 17:22
  • in your example if i not use `togglebutton.setIcon(icon)` then the other functions don't work. the first of all i must not use `.setIcon();` because user should not see the icons if dont press them. icons must reveal when user just press on them. – quartaela Oct 24 '11 at 17:25
  • @quartaela better would be post here code based on my SSCCE, because we are still on theoretical level – mKorbel Oct 24 '11 at 17:31
  • `final JToggleButton toggleButton = new JToggleButton(); //toggleButton.setIcon((icon)); toggleButton.setRolloverIcon((infoIcon)); toggleButton.setPressedIcon(warnIcon); toggleButton.setDisabledIcon(warnIcon); toggleButton.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (toggleButton.isSelected()) { } else { } } });` like here if disable `.setIcon()` then the other functions dont work – quartaela Oct 24 '11 at 17:34
  • answer is here, constructor http://download.oracle.com/javase/6/docs/api/javax/swing/JToggleButton.html#JToggleButton%28javax.swing.Icon%29, hmmm `final JToggleButton toggleButton = new JToggleButton(infoIcon);` – mKorbel Oct 24 '11 at 18:03
  • yess but as i mentioned before i if pass the icon in constructor it will reveal when i started the program. i want the thing that when i start the program icon must not reveal, it should reveal when i press on it. – quartaela Oct 24 '11 at 18:13
  • hehehe, you are right, this Icon Engine must be (initialized one from these two ways) started somehow :-) – mKorbel Oct 24 '11 at 18:15
  • i dont wanna give up and this looks very silly. there must be a way ... :) – quartaela Oct 24 '11 at 18:16
  • @trashgod thanks !!, I tried that but now I sure that something *** happens betweens Connection_From_Head_To_Hands – mKorbel Oct 24 '11 at 19:01
  • Huhh. Nice, though I suspect trashgod won't get a notification of any comment (since they made no comment themselves). – Andrew Thompson Oct 24 '11 at 19:15
  • 2
    Part of my ongoing struggle to code the to the interface. :-) – trashgod Oct 24 '11 at 19:15
  • @quartaela Note that while mKorbel posted an SSCCE, so far, you have not. Even if the code compiles cleanly, it is still trying to use images that exist on your file-system, but not ours. This is why using the icons from the JDK is so clever. It means the code should run on *any* (1.2+) JRE. – Andrew Thompson Oct 24 '11 at 19:18
  • @trashgod Coding to interface - v. good call. I try to do that, but keep finding myself slipping into naming the specific sub-class that I want to use. – Andrew Thompson Oct 24 '11 at 19:19
  • @Andrew well you are rigth but there isnt enough space for paste whole code _? – quartaela Oct 24 '11 at 19:27
  • @quartaela I would not irritate bare foot tiger, everything is about your willingness – mKorbel Oct 24 '11 at 19:30
  • @mKorbel first of all i dont have any idea about that what is SSCCE clearly_?. i read the documentation and just understand that is a documentation about suggest some useful things for explain your problem and minimize your code. then i tried to minimize my code any paste here but i couldnt cause , message box cant contain all the useful code. and sorry for my english guys. by the way i dont know where should i copy my sscce code _?do you mean that i must edit my main post_? – quartaela Oct 24 '11 at 19:39
  • @quartaela At what point did I say 'whole code'? Please follow the link to the SSCCE (kindly added by mKorbel), and make sure you understand the difference. Note also that while your current code comes to 150 lines, the SSCCE weighs in at a mere 88. – Andrew Thompson Oct 24 '11 at 19:42
  • *"do you mean that i must edit my main post?"* Yes. That would be the way to do it. Some might add it as an update in addition to the original code, but you might also just replace the original code with new code. As to the meaning if the SSCCE - anything you do not understand, please ask. Any of the 3 of myself, trashgod or mKorbel should be able to help clear up any misunderstandings. *"sorry for my english"* Sorry for my inability to speak anything *but* English. I am 'language challenged' in that way. – Andrew Thompson Oct 24 '11 at 19:47
  • ok finally i learnt about SSCCE and knew how to use it and updated my main post also. :). so thank for your patience to understand me :) – quartaela Oct 24 '11 at 20:02
  • @quartaela but you forgot to accepted post by (@trashgod), +1 for you, for nice thread – mKorbel Oct 24 '11 at 20:56
2

One approach is to do the following:

  • Use the selected state to indicate whether to show or hide the Icon.
  • Use the enabled state to indicate that a pair has been matched.

Code outline:

/** Handle ItemEvents. */
@Override
public void itemStateChanged(ItemEvent e) {
    GameButton gb = (GameButton) e.getItem();
    gb.setState();
}

/** Remove a and b from play. */
private void retirePair(GameButton a, GameButton b) {
    a.setSelected(true);
    a.setEnabled(false);
    b.setSelected(true);
    b.setEnabled(false);
}

class GameButton extends JToggleButton {
    ...
    public void setState() {
        if (this.isSelected() || !this.isEnabled()) {
            this.setIcon(icon);
        } else {
            this.setIcon(hidden);
        }
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045