Link to home
Start Free TrialLog in
Avatar of rockymagee
rockymagee

asked on

2 ActionListeners on one button

I have 2 Listeners that looks like this:

//AHEAD
}else if ("Ahead".equals(e.getActionCommand()) ){
   if(!deviceView.getText().equals("")){
   this.mdate.addMinute();
   clockView.setText(mdate.time(mdate));
}
         
//BACK       
}else if ("Back".equals(e.getActionCommand()) ) {
    if(!deviceView.getText().equals("")){
    this.mdate.subMinute();
    clockView.setText(mdate.time(mdate));
}

The methods addMinute() for "AHEAD" and subMinute() for "BACK" work properly.

What I want to do is call these methods if the button is clicked, but I also have two
other methods that add and subtract 10 minutes to my clockView.  I would like for these
methods to be called if the user clicks and holds on the buttons "Ahead or Back".

Please let me know if I can clarify, I guess my overall question is can I add two different
Listeners to one button that react differenty if the user either just clicks or clicks and holds.

TIA

Avatar of sciuriware
sciuriware

A component maintains a collection of actionlisteners; the number of listeners can be
unlimited therefore. When you click the component they all (!) fire.
What happens must be coded in the 'actionPerformed()'

E.g.: was it a left click or a right click?

;JOOP!
In your case 1 actionlistener can do all the work.

;JOOP!
About click, hold, release a.s.o.: that can only be distinguished by a mouseListener, not by an actionListener.

;JOOP!
Avatar of rockymagee

ASKER

Just a regular right click is what I am looking for ... let me look into mouseListeners ... any helpful code snippets?
have your code in mousePressed( MouseEvent me ) method.

probably you may have to launch a thread where you need to call these methods and stop the thread in mouseReleased( MouseEvent me ) method.
here is some code I put together to illustrate my need ... should compile and run ...

/BEGIN ******************************************************
   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
       
    public class TestMouse {
      private static Window win;
       public static void main(String[] args) {
      //Window win = new Window();
         win = new Window();
     
      }
   }

    class Window extends JFrame implements ActionListener {
      JTextArea clockView  = new JTextArea ("1");
      JButton ahead  = new JButton("Ahead");
      JButton back   = new JButton("Back");
   
       public Window() {
         setSize(100, 100);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setVisible(true);
         Container ca = getContentPane();
         ca.setBackground(Color.lightGray);
         GridBagLayout gblm = new GridBagLayout();
         GridBagConstraints gbc = new GridBagConstraints();
         gbc.fill = GridBagConstraints.HORIZONTAL;
         ca.setLayout(gblm);
         gbc.gridx = 1;
         gbc.gridy = 0;
         ca.add(clockView,gbc);
         gbc.gridx = 1;
         gbc.gridy = 1;
         ca.add(ahead,gbc);
         gbc.gridx = 1;
         gbc.gridy = 2;
         ca.add(back,gbc);
         ahead.addActionListener(this);
         back.addActionListener(this);
         setContentPane(ca);
      }
            
       public void actionPerformed(ActionEvent e) {       
         //AHEAD
         if ("Ahead".equals(e.getActionCommand()) ){
            int addTime;
            addTime = Integer.parseInt(clockView.getText());
            /***********************
               WANT TO ADD MOUSE LISTENER HERE?
               TO SELECTED WHETHER PRESSED/RELEASED OR CLICKED?
               THEN CHOOSE ADD 1 OR ADD 10
               ***********************/  
            addTime = addTime + 1;
            String addT = Integer.toString(addTime);
            clockView.setText(addT);
           
         
         //BACK       
         }
         else if ("Back".equals(e.getActionCommand()) ) {
            int subTime;
            subTime = Integer.parseInt(clockView.getText());
               /***********************
               WANT TO ADD MOUSE LISTENER HERE?
               TO SELECTED WHETHER PRESSED/RELEASED OR CLICKED?
               THEN CHOOSE SUB 1 OR SUB 10
               ***********************/        
            subTime = subTime - 1;
            String subT = Integer.toString(subTime);
            clockView.setText(subT);
         }
      }
   
       public void MousePressed() {}
   
       public void MouseReleased() {}
   
   }
/ END ******************************************************

any help with adding mouseListener would be great.

TIA
>>I would like for these
methods to be called if the user clicks and holds on the buttons "Ahead or Back".
>>

How you decide if the user clicks or holds the mouse button? do you maintain any deferences like the time he holds...?
also do want the process of adding 10 contineously till the user holds the button? if so what is the frequency?
Not really sure, I was hoping just to figure out how to decipher between the Press/Release and the Click ...

On Press/Release It would Incement the time 10 seconds every second or so. nothing real specific, just functioning.  

If it is going to turn out to be overly complicated to do so, then I will just proceed with using seperate +1 & + 10 buttons.

TIA
ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is perfect ! Thanks !

This will definately help me understand how the mouseListener works.

A++