set and disable icons of JToggleButton
Asked Answered
A

2

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);
    }
}
Acne answered 24/10, 2011 at 14:55 Comment(1)
yeah but i dont asking you for code. i explained my problem and if i can fix it, i will finish it.Acne
G
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);
        }
    }
}
Gonion answered 24/10, 2011 at 19:11 Comment(1)
@Acne this is your solution +1Unsearchable
U
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);
    }
}
Unsearchable answered 24/10, 2011 at 15:12 Comment(25)
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_?Acne
test if Icon is/isn't null, because null Icon doesn't generated any Error, nothing else :-)Unsearchable
if i use new JToggleButton(icon); then there is no problem it shows the icon but otherwise impossible...Acne
@Acne no idea what's happen ...there must be some another issue with Icon or JToggleButtonUnsearchable
@Andrew Thompson hmmm as I see now I'm wrong with casting (ImageIcon) for plain Icon,Unsearchable
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) method.Lanctot
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.Acne
@Acne better would be post here code based on my SSCCE, because we are still on theoretical levelUnsearchable
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 workAcne
answer is here, constructor download.oracle.com/javase/6/docs/api/javax/swing/…, hmmm final JToggleButton toggleButton = new JToggleButton(infoIcon);Unsearchable
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.Acne
hehehe, you are right, this Icon Engine must be (initialized one from these two ways) started somehow :-)Unsearchable
i dont wanna give up and this looks very silly. there must be a way ... :)Acne
@trashgod thanks !!, I tried that but now I sure that something *** happens betweens Connection_From_Head_To_HandsUnsearchable
Huhh. Nice, though I suspect trashgod won't get a notification of any comment (since they made no comment themselves).Lanctot
Part of my ongoing struggle to code the to the interface. :-)Gonion
@Acne 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.Lanctot
@Gonion 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.Lanctot
@Andrew well you are rigth but there isnt enough space for paste whole code _?Acne
@Acne I would not irritate bare foot tiger, everything is about your willingnessUnsearchable
@Unsearchable 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?Acne
@Acne 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.Lanctot
"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.Lanctot
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 :)Acne
@Acne but you forgot to accepted post by (@trashgod), +1 for you, for nice threadUnsearchable
G
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);
        }
    }
}
Gonion answered 24/10, 2011 at 19:11 Comment(1)
@Acne this is your solution +1Unsearchable

© 2022 - 2024 — McMap. All rights reserved.