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.
Start by creating your own button, one which you can control the selected state...
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...So, when the button is clicked, it will use the
selectedIcon