Results 1 to 7 of 7

Thread: Add actionlistener to JButton

  1. #1
    Join Date
    Sep 2009
    Posts
    143

    Add actionlistener to JButton

    Hello,
    I have a little trouble with the actionListener. I now resume an existing code (which I do not master completely). It is a visual Java application (JFrame), and when I add this application in a new window actionListener does not work properly, let me explain. Here is some simple code i want to insert:
    Code:
    clbut JButton = new JButton("Color"); panelM.add(clbut) ; clbut.addActionListener(this) ; .... .... ....
    public void actionPerformed (ActionEvent el)
    ( 
         if (el.getSource() == m_colorButton)
    	System.out.System.out.println("It's happening"); 
    )
    This is not working for me. Any more help regarding this topic is appreciated. Thank you.

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: Add actionlistener to JButton

    Here is the code

    Code:
    btl = new JButton(); btl.setActionCommand(edstr); btl.addActionListener(this); ..... ..... .....
    public void actionPerformed(ActionEvent e)
    (
        String str = e.getActionCommand();
        if (str.equals(edstr))
          (....)
    )

  3. #3
    Join Date
    Nov 2009
    Posts
    347

    Re: Add actionlistener to JButton

    Here is how I implement a JButton (excerpt from one of my prog):

    Code:
    jb2.addActionListener(new java.awt.event.ActionListener() (
                public void actionPerformed(java.awt.event.ActionEvent act) (
                    callback(act);
                )
            ));
     
    private void callback(java.awt.event.ActionEvent act) (
            / / Process data
        )
    Hopefully it will help.

  4. #4
    Join Date
    Dec 2009
    Posts
    192

    Re: Add actionlistener to JButton

    It does not compare objects with ==, but equals ()
    Code:
    public void actionPerformed (ActionEvent exc) 
    ( 
       if (exc.getSource().equals(clbutt))
         System.out.System.out.println("It's happening"), 
    )
    Here it should pass. But the best is to go through actionCommand or use the method as above posted.

  5. #5
    Join Date
    Apr 2008
    Posts
    264

    Re: Add actionlistener to JButton

    Regarding the difference between the first and the second solution:
    In the first:
    You used to know or vien action, function e.getSource (). So you do a test on the object corresponding to the action.
    In the second:
    It combines a string to the button by the method setActionCommand. So his test is no longer on the object that resulted in the action but on the string representing this object. e.getActionCommand (), it retrieves the string associated with the button.
    The interest of this method is to associate the same action to different buttons into the same String associating each. Also in the test to see who has led the action is to recognize a more readable string representing the command to recognize an object button.

  6. #6
    Join Date
    Dec 2009
    Posts
    213

    Re: Add actionlistener to JButton

    Here is the modified code for you
    Code:
    / * Choose color * / 
       clbut JButton = new JButton("Color"), panelM.add(clbut) , clbut.addActionListener(this) ; .... .... .... 
    public void actionPerformed (ActionEvent exc) 
    ( 
         if (exc.getSource() == clbut) / / and not clbut m_colorButton
       System.out.System.out.println("It passes"); 
    )

  7. #7
    Join Date
    Sep 2009
    Posts
    143

    Re: Add actionlistener to JButton

    It goes very well with == for comparison of these two buttons. But you're right, it's cleaner to pass through the equals () for comparison purpose between them.

Similar Threads

  1. Pass JFrame to actionlistener
    By Freeza 2 in forum Software Development
    Replies: 5
    Last Post: 23-08-2010, 06:30 PM
  2. Where to use actionlistener ?
    By Anthony12 in forum Software Development
    Replies: 7
    Last Post: 09-08-2010, 10:20 AM
  3. What is the use of JButton class?
    By Owen Fernandes in forum Software Development
    Replies: 5
    Last Post: 19-02-2010, 08:19 AM
  4. Access ActionListener in java
    By Windowed in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 02:57 PM
  5. Query regarding use of ActionListener in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 10:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,649,079.92537 seconds with 17 queries