2

I have a text area, whenever the scroll bar is scrolled, I want to call a method. On what event I should call the method? I tried:

private void jScrollPane1MouseReleased(java.awt.event.MouseEvent evt) {
      //execute();  
     System.out.println("Scroller Moved");       
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
FirmView
  • 3,130
  • 8
  • 34
  • 50
  • 2
    What ***feature*** are you trying to implement? Explain it to me the way you might explain it to an end user. Wanting to access the methods related to scrolling, in your own code, has an extremely bad code smell to it. – Andrew Thompson Aug 22 '12 at 14:34
  • 1
    And what for interaction with the keyboard, e.g. using the arrow keys ? – Robin Aug 22 '12 at 15:40
  • @AndrewThompson Nick Rippe solution is perfect. I wanted an event when the row numbers gets changed in jtextarea. Robin, Nick Rippe solution is taking care of my purpose as i enter key or move the cursor through arrow key – FirmView Aug 23 '12 at 00:17

3 Answers3

3

You want to use the AdjustmentListener on the JScrollBar (choose the one you want to listen for movement on). Here's an example of listening on the vertical bar.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TempProject extends JPanel{

    public static void main(String args[])    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                JPanel panel = new JPanel();
                panel.setPreferredSize(new Dimension(300, 2000));
                JScrollPane pane = new JScrollPane(panel);
                pane.setPreferredSize(new Dimension(400, 300));
                pane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){

                    @Override
                    public void adjustmentValueChanged(AdjustmentEvent e) {
                        System.out.println("Hi Mom!");
                    }});

                frame.setContentPane(pane); 
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}
Nick Rippe
  • 6,465
  • 14
  • 30
  • 1
    +1 for main content - but I might be tempted to downvote next time I see a setXXSize ;-) – kleopatra Aug 22 '12 at 15:48
  • 1
    @kleopatra I'm not quite sure what you're referring to - I assume one of the `setPreferredSize` calls on either the `panel` or `pane` (or both). If there's something wrong with doing this, I'd appreciate the information - I didn't find anything in a quick search. The reason for it in this code is because I wanted to force a scrollbar. – Nick Rippe Aug 22 '12 at 17:02
  • 1
    sorry for not having been entirely clear: it's perfectly fine for demonstating (preferably with a hint that it's done only for demonstrating). [Some reasons for not using](http://stackoverflow.com/a/7229519/203657) it in production code – kleopatra Aug 22 '12 at 20:38
  • @Nick Rippe This is perfect!!!, i would have voted more than once, if stackoverflow had allowed – FirmView Aug 23 '12 at 00:20
2

You should use a mouselistener event instead.

If you'd like to know more about it check it out here - http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
  • 1
    the OP already is using a mouseListener :-) The snippet shown is created by a visual builder, meant to be fleshed out by client code. – kleopatra Aug 22 '12 at 15:47
1

use the mouseclicked method then get the coordinates using getX() and getY(), then with an if test, test to see whether the coordinates are within the dimensions of your JScrollPane. Just remember to implement mouselister and add all inherited abstract methods :)

imulsion
  • 8,820
  • 20
  • 54
  • 84
  • 2
    ehh .. not exactly: the snippet (produced by a visual builder, possibly NetBeans) is a callback messaged by an auto-installed MouseListener on the JScrollPane. This implies that the event is _always_ within the dimensions of the scrollPane. As such it will rarely be called, because most of its area is filled by other components. – kleopatra Aug 22 '12 at 15:54