Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 23-07-2010
Member
 
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.
Reply With Quote
  #2  
Old 23-07-2010
Member
 
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))
      (....)
)
Reply With Quote
  #3  
Old 23-07-2010
Member
 
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.
Reply With Quote
  #4  
Old 23-07-2010
Member
 
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.
Reply With Quote
  #5  
Old 23-07-2010
Member
 
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.
Reply With Quote
  #6  
Old 23-07-2010
Member
 
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"); 
)
Reply With Quote
  #7  
Old 03-08-2010
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Add actionlistener to JButton"
Thread Thread Starter Forum Replies Last Post
Pass JFrame to actionlistener Freeza 2 Software Development 5 23-08-2010 06:30 PM
Where to use actionlistener ? Anthony12 Software Development 7 09-08-2010 10:20 AM
What is the use of JButton class? Owen Fernandes Software Development 5 19-02-2010 08:19 AM
Access ActionListener in java Windowed Software Development 3 18-11-2009 02:57 PM
Query regarding use of ActionListener in java beelow Software Development 3 16-11-2009 10:56 AM


All times are GMT +5.5. The time now is 07:40 AM.