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 2 of 2

Thread: Adding an actionlistener to a JButton with no text

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding an actionlistener to a JButton with no text

    I'm having trouble figuring this out. I would normally just do something like

    JButton blah = new JButton("blah");
     
    String str = e.getActionCommand();
    if(str.equals("blah") {
    //do something
    }

    but now I'm using an icon for the JButton instead of text.
    ImageIcon createIcon = new ImageIcon("images/new.png");
    JButton create = new JButton(createIcon);
    create.addActionListener(new EventHandler());

    what would I do in the eventhandler class?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Adding an actionlistener to a JButton with no text

    Presuming 'e' is an ActionEvent from an ActionListener registered with the JButton - use the setActionCommand method of JButton, or compare the source of the event to the Component, or separate out into a single ActionListener to Listen to only that JButton, ...many ways

    Edit: and please read the forum rules. Double posting is not allowed. Your other post has been deleted and I have moved this to a more appropriate location

Similar Threads

  1. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 05:14 PM
  2. Error when adding ActionListener to JButton
    By grimrader22 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2011, 05:53 AM
  3. JButton.... actionListener.... help!!!!....
    By eiramae in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 04:54 AM
  4. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 15
    Last Post: February 10th, 2011, 01:21 AM
  5. Can't set label text from a Jbutton
    By VBGuy in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 8th, 2010, 10:55 AM