Results 1 to 6 of 6
  1. #1
    SirVyvin is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Question 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?

    Java 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.
    Last edited by SirVyvin; 12-22-2011 at 06:30 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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:

    Java 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
        // ....
    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: ActionPerformed - JButton - How to setActionCommand?

    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: JButton : Background and Text where the paintIcon() method can do anything it likes: set the background, draw an image, etc.

  5. #5
    SirVyvin is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Wink Re: ActionPerformed - JButton - How to setActionCommand?

    Quote Originally Posted by Fubarable View Post
    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:

    Java 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.

  6. #6
    SirVyvin is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: ActionPerformed - JButton - How to setActionCommand?

    Quote Originally Posted by pbrockway2 View Post
    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: JButton : Background and Text 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.

Similar Threads

  1. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  2. Replies: 18
    Last Post: 01-24-2010, 04:59 PM
  3. Help with actionPerformed
    By mayhewj7 in forum New To Java
    Replies: 8
    Last Post: 02-10-2009, 06:45 PM
  4. JLabel .setActionCommand
    By stevemcc in forum AWT / Swing
    Replies: 1
    Last Post: 03-28-2008, 04:16 AM
  5. Problems with jButton ActionPerformed
    By susan in forum AWT / Swing
    Replies: 3
    Last Post: 08-07-2007, 04:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •