Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to get Which Button Clicked in through getActionCommand when Button have no Label!

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to get Which Button Clicked in through getActionCommand when Button have no Label!

    I have a JButton with a .PNG icon on it. I want to get that button click in actionPerformed Method but Jbutton have no Label... Please tell me how i will know that which button clicked?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to get Which Button Clicked in through getActionCommand when Button have no Label!

    Either compare it to the value returned from ActionEvent.getSource(), or only add the ActionListener to that particular JButton so you always know which JButton it was.

    If you can't get one of those approaches working, post an MCVE and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to get Which Button Clicked in through getActionCommand when Button have no Label!

    I have Added ActionListner to it and object is Catch by ActionEvent Parameter in actionPerformed but im a beginner and dont know how to know which button is in ActionEvent Parameter, I always access button by label i.e e.getActionCommand(); but this time i have no label on button i want to draw a rectanle by paint method when button clicked, thats why i want to catch button in actionPerformed

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to get Which Button Clicked in through getActionCommand when Button have no Label!

    Either compare it to the value returned from ActionEvent.getSource(), or only add the ActionListener to that particular JButton so you always know which JButton it was.

    If you can't get one of those approaches working, post an MCVE and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Getting a calculate button to work, and clear button to reset everything.
    By Ancharius in forum AWT / Java Swing
    Replies: 3
    Last Post: April 3rd, 2014, 05:20 PM
  2. Replies: 2
    Last Post: May 2nd, 2013, 03:35 AM
  3. ActionListener not working when button is clicked.
    By JamEngulfer221 in forum AWT / Java Swing
    Replies: 19
    Last Post: April 10th, 2012, 04:23 PM
  4. When a button is clicked: get my Data!!!
    By aStudentofJava in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 15th, 2012, 08:45 AM
  5. button update when clicked
    By prettynew in forum AWT / Java Swing
    Replies: 4
    Last Post: March 13th, 2011, 04:47 PM