2

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    First, don't use `MouseListener`s with `JButton`s. You could create a custom button model which doesn't allow it to be turned off, so, once selected, it's state remains selected (until you reset it), that way you can use the selected/unselected icon properties. – MadProgrammer Nov 26 '14 at 03:58
  • I tried Statechanged and ActionPerformed but the image shows when I hover the mouse over the button (cause those events include all the possible actions right? like mouse hover, mouse released etc). Anyway I'll look into creating the custom button, if you have any link that would be appreciated :) – Fernando Castillo Nov 26 '14 at 05:24

1 Answers1

7

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);
            }
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Thank you SO much! Now I need to make the opponent board with the bombs and go 1 turn each and validate when the person wins. It's my final project due in two weeks so I'll probably come back with more questions :p thanks again. – Fernando Castillo Nov 27 '14 at 04:56