ActionPerformed - JButton - How to setActionCommand?
Hello, I'm making a Minesweeper game in Java for my final project for school and I have one issue that is keeping me from finishing it which is commented below in the code as well:
What I want to do is set the background of the button that was clicked to Gray and display the number that was inside (it is an image). However I am unable to use the normal 'set...' methods. So my question is how do I change those properties without doing it from a different approach?
Code:
public void actionPerformed(ActionEvent event) {
String action = event.getActionCommand();
if (action.equals("-1")) {
revealBoard();
} else if (action.equals("1") || action.equals("2") ...... || action.equals("8")) {
/*
* I want to do:
* event = new JButton(new ImageIcon(getClass().getResource("/minesweeper/resources/" + action + ".png"));
* event.setBackground(Color.GRAY);
*/
} else if (action.equals("0")) {
//not worried about this right now
} else if (action.equals("face")) {
frame.dispose();
play();
}
}
PS I forgot to change the thread title before I posted.
Re: ActionPerformed - JButton - How to setActionCommand?
One way I solved this problem is not to stick with just JButtons, but rather use a JPanel that uses a CardLayout as its layout manager for each mine cell. That way the panel swap what it shows. First it shows a JButton, and later after the button has been pressed, it swaps it for a JLabel that displays the number (or nothing if the number is 0). An example of my code is here (I'm HoverCraft on that site): minesweeper-action-events
Re: ActionPerformed - JButton - How to setActionCommand?
Oh, to answer your immediate question, to get a reference to the button that caused the action, use the getSource() method of the ActionEvent object that is passed into your actionPerformed method:
Code:
public void actionPerformed(ActionEvent event) {
String action = event.getActionCommand();
JButton sourceBtn = (JButton) event.getSource();
// ... now you can call whatever methods you'd like on the sourceBtn JButton
// ....
}
Re: ActionPerformed - JButton - How to setActionCommand?
Quote:
What I want to do is set the background of the button that was clicked to Gray and display the number that was inside (it is an image). However I am unable to use the normal 'set...' methods.
Why are you unable to use them?
(In the code you posted you create a new button. But surely you want to alter the appearance of the one that's already there.)
Have a look at the Icon implementation here: http://www.java-forums.org/awt-swing...ound-text.html where the paintIcon() method can do anything it likes: set the background, draw an image, etc.
Re: ActionPerformed - JButton - How to setActionCommand?
Quote:
Originally Posted by
Fubarable
Oh, to answer your immediate question, to get a reference to the button that caused the action, use the getSource() method of the ActionEvent object that is passed into your actionPerformed method:
Code:
public void actionPerformed(ActionEvent event) {
String action = event.getActionCommand();
JButton sourceBtn = (JButton) event.getSource();
// ... now you can call whatever methods you'd like on the sourceBtn JButton
// ....
}
Great that fixed my entire problem. hopefully my teacher wont think I stole the entire code because we haven't learned that in the course, and we didn't learn mouse listeners either but he seen me doing them lol. On the other hand I also learned keyboard listeners for another game I made earlier this year and he didn't care. but I can pretty much guarantee if i google'd 'minesweeper java tutorial' it will give me 100s of results which is why I'm so worried about him thinking I stole the code. :(devil):
Re: ActionPerformed - JButton - How to setActionCommand?
Quote:
Originally Posted by
pbrockway2
Why are you unable to use them?
(In the code you posted you create a new button. But surely you want to alter the appearance of the one that's already there.)
Have a look at the Icon implementation here:
http://www.java-forums.org/awt-swing...ound-text.html where the paintIcon() method can do anything it likes: set the background, draw an image, etc.
Yah it was late here and the 'new JButton' was a mistake and should of been '.setIcon()' and I will change that after I post this. Also the 'actionPerformed(ActionEvent event)' only uses 'event' to get some things from the object which the action was performed on which is why I am unable to use it that way and can use it with Fubarable's way.