Click j/ToggleButton then set Icon/Image

6.2k views Asked by At

I am making a 4x4 board kinda like minesweeper. Each button has a bomb or another image.

Here's my code:

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
    this.jToggleButton1.setIcon(new javax.swing.ImageIcon("bombaa.png"));          
}         

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    this.jToggleButton1.setIcon(new javax.swing.ImageIcon("bombaa.png"));
}                    

also tried this way...

private void setIcon1(){
    setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("bombaa.png")));
}

and call setIcon() in the jButton1ActionPerformed and jButton1MouseClicked BUT this sets my image as the main Icon for the program.

Basically what I need is: Click a button and set the image/icon one time only.

1

There are 1 answers

1
MadProgrammer On BEST ANSWER

Start by creating your own button, one which you can control the selected state...

public class StickyModel extends JToggleButton.ToggleButtonModel {

    public void reset() {
        super.setSelected(false);
    }

    @Override
    public void setSelected(boolean b) {
        if (!isSelected()) {
            super.setSelected(b);
        }
    }

}

This will prevent the button from becoming "unselected" once it has been set selected (it also includes a reset method which will make it "unselected" for you)

Create your buttons with a "blank" or empty "default" icon and a set the selectedIcon property to what you want shown when the button is selected...

JToggleButton btn = new JToggleButton();
btn.setModel(new StickyModel());
btn.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Blank.png"))));
btn.setSelectedIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Bomb.png"))));

So, when the button is clicked, it will use the selectedIcon

Sticky Buttons

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            try {
                add(createButton());
                add(createButton());
                add(createButton());
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }

        protected JToggleButton createButton() throws IOException {

            JToggleButton btn = new JToggleButton();
            btn.setModel(new StickyModel());
            btn.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Blank.png"))));
            btn.setSelectedIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Bomb.png"))));
            return btn;

        }

    }

    public class StickyModel extends JToggleButton.ToggleButtonModel {

        public void reset() {
            super.setSelected(false);
        }

        @Override
        public void setSelected(boolean b) {
            if (!isSelected()) {
                super.setSelected(b);
            }
        }

    }

}