JButton event handler
1 posts in topic
Flat View  Flat View

Posted By:   danny_xavier
Posted On:   Monday, April 28, 2003 09:40 AM

how can i know wether the jbutton is pressed down and not released?I want to continously scroll a Jscrollpane as long as the jbutton is pressed down and would like scrolling to stop the moment the button is released


Re: JButton event handler

Posted By:   Bahman_Barzideh  
Posted On:   Monday, April 28, 2003 10:11 AM

I don't know of any method in Swing/awt that checks on whether a mouse button is currently pressed. I am going to assume that
you already have looked at the JScrollPane and JScrollBar classes and have decided that they are not going
to work for you.

One way to accomplish what you are after is to use a javax.swing.Timer class in conjunction with a MouseListener for your JButton. You will also need an ActionListener for your Timer. I would suggest combining this class and the MouseListener into one.

The basic idea is as follows:
Once the user presses a mouse button, the system will invoke your mouse listeners's mousePressed () method. In this method you start your
timer. The timer is going to go off (call the actionPerformed () method of your ActionListener) on a regular basic (every so many milliseconds as set by you). Every time the timer
goes off, you do what you need for scrolling. Once the button is
released the system calls your mouseReleased () method
and you stop the timer thereby ending the scrolling.

Here is the basic logic:

class ButtonListener implements MouseListener, ActionPerformed {

public void mousePressed (MouseEvent event) {
timer.start ();

public void mouseRelease (MouseEvent event) {
timer.stop ();

public void actionPerformed (ActionEvent event) {
put logic for scrolling here

private final Timer timer = new Timer (, this);

Note that the above psuedo-code is overly simplified. You will not only need to supply the
rest of the methods required by MouseListener you will
also have to worry about such things as which mouse button is
About | Sitemap | Contact