Results 1 to 8 of 8
Like Tree4Likes
  • 1 Post By gimbal2
  • 3 Post By DarrylBurke

Thread: JButton ActionListeners

  1. #1
    DaveMc is offline Member
    Join Date
    Dec 2012
    Location
    Kerry, Ireland
    Posts
    8
    Rep Power
    0

    Default JButton ActionListeners

    Hey I am trying to add Action Listeners to various buttons in my program but I am stuck at the moment. The code below is a snippet from the program.

    Java Code:
    JPanel tableButtonPanel = new JPanel();
            tableButtonPanel.add(new JButton("Add thing"));
            tableButtonPanel.add(new JButton("Delete thing"));
            tableButtonPanel.add(new JButton("Modify thing"));
    Is there a way to create action listeners to the buttons when they are created this way?
    Any help appreciated, Thanks :)

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: JButton ActionListeners

    Hmm. I'm sure you could get to the buttons somehow through getting all the components of the JPanel, but it'd be far easier to just go "JButton b1=new JButton(); b1.addActionListener()".

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: JButton ActionListeners

    The way you're doing it now, you create a new JButton and pass it directly into the JPanel.add() method.

    You need to keep around a reference to each JButton, at least long enough to add an ActionListener to it. Something like:

    Java Code:
    JPanel tableButtonPanel = new JPanel();
    JButton addButton = new JButton("Add thing");
    addButton.addActionListener(listener);
    tableButtonPanel.add(addButton);
    //same for delete
    //same for modify
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default Re: JButton ActionListeners

    All Swing's JComponents are Containers; you can get all Components from a Container; a bit of casting on the Components should do the trick but I find it an ugly hack ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: JButton ActionListeners

    Okay someone is going to kick my butt for this. But in the interest of exploring boundaries and possibilities:

    Java Code:
    public class MyButton extends JButton {
    
      public MyButton(String label, ActionListener listener){
        super(label);
        addActionListener(listener);
      }
    }
    When the standard API doesn't provide a direct way, you can always bodge something together yourself.
    JosAH likes this.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: JButton ActionListeners

    Quote Originally Posted by gimbal2 View Post
    When the standard API doesn't provide a direct way ...
    Java Code:
    tableButtonPanel.add(new JButton("Add thing") {
      {
        addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            // respond to button click
          }
        });
      }
    });
    KevinWorkman, JosAH and gimbal2 like this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DaveMc is offline Member
    Join Date
    Dec 2012
    Location
    Kerry, Ireland
    Posts
    8
    Rep Power
    0

    Default Re: JButton ActionListeners

    Hey thanks everyone for your suggestions they are a great help :) finally i can move on from this task :D

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: JButton ActionListeners

    A note: the last two responses are in the way of a joke. While valid code, this is not the way to write a maintainable application.

    Kevin's response at #3 is the way to go.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Problem with actionlisteners en changelisteners
    By SVENeRGy1 in forum New To Java
    Replies: 5
    Last Post: 03-18-2013, 05:37 PM
  2. Organization of actionListeners
    By John5788 in forum AWT / Swing
    Replies: 4
    Last Post: 12-28-2011, 06:35 AM
  3. JFrames and JPanels with ActionListeners
    By atomant in forum New To Java
    Replies: 3
    Last Post: 11-30-2010, 07:26 PM
  4. Question about ActionListeners and JAR files
    By Psyclone in forum AWT / Swing
    Replies: 6
    Last Post: 03-20-2010, 12:08 PM
  5. Replies: 1
    Last Post: 12-07-2008, 05:50 PM

Posting Permissions

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