2

How to auto scroll JTextArea on middle mouse button click? To be clear, when we click the mouse wheel (middle button) in Firefox (for example) and push it down/up we will be able to scroll the page automatically when we release the middle button, we can alo see a round icon at the point of middle button click.

Here is a screenshot of it.

Auto scroll JTextArea Icon

I think there is no such functionality for JTextArea in Java. Is there any hack to implement it.

Any answer is appreciated.

JavaTechnical
  • 8,846
  • 8
  • 61
  • 97
  • Also consider `JScrollNavigator`, examined [here](http://stackoverflow.com/q/11739989/230513). – trashgod Jul 21 '13 at 12:10
  • *"How to auto scroll `JTextArea` on middle mouse button click?"* Not a very usable feature for those of us that have a 2 (or 1) button mouse. I suggest you just put the text area in a `JScrollPane` and let the user adjust the scroll position as needed using the scroll bars. – Andrew Thompson Jul 21 '13 at 12:45

1 Answers1

1

To determine which of the Mouse buttons is pressed, these three methods from SwingUtilities could help you:

isLeftMouseButton

isMiddleMouseButton

isRightMouseButton

Based on the mouse button clicked, you can take appropriate action to scroll the JTextArea programmatically. Probably, you can use something like this:

textArea.setCaretPosition(textArea.getDocument().getLength()); -> to move to the end of the JTextArea

You can look at this link to get a good idea of positioning the cursor position in JTextArea

Community
  • 1
  • 1
Mubin
  • 4,192
  • 3
  • 26
  • 45
  • `"Based on the mouse button clicked, you can take appropriate action to scroll the JTextArea programmatically."` -- and therein lies the main crux of the question. Note that moving to the end is not what he's trying to achieve. He's trying to scroll in any direction based on where the mouse currently is and where the initial press occurred. This is a much more complex issue. – Hovercraft Full Of Eels Jul 21 '13 at 12:05