2

If i have something like this, where i can control the auto-scrolling using boolean flag "performAdjustment":

static boolean performAdjustment = true;

JTextArea textArea = new JTextArea();
JScrollPane jScrollPane1 = new JScrollPane(textArea);

   jScrollPane1.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {  
            public void adjustmentValueChanged(AdjustmentEvent e) { 
                if(performAdjustment){
                    e.getAdjustable().setValue(e.getAdjustable().getMaximum());  
                }
        }}); 

Now i this works fine, but the problem is i want to unset this boolean flag when a user clicks on the scroll bar and it should be set again when the user leave the click (like onMouseOut event in JavaScript).

Can you tell me how can i add this new EventListener where i can detect click event of the scroll bar??

Johnydep
  • 6,027
  • 20
  • 57
  • 74
  • i need something like jScrollPane1.getVerticalScrollBar().addClickListener() or something similar, is there anything like this exisits?? – Johnydep Dec 09 '11 at 12:29
  • How about adding a mouse listener? – Ben van Gompel Dec 09 '11 at 12:37
  • Thanks yes you are right, but i just realized i could autoscroll using: textArea.setCaretPosition(textArea.getDocument().getLength()); – Johnydep Dec 09 '11 at 12:42
  • @BenvanGompel, i can't select your answer as valid answer, can you please put it as an answer, thanks!! – Johnydep Dec 09 '11 at 12:42
  • what exactly do you want to achieve? – kleopatra Dec 09 '11 at 13:14
  • @kleopatra, i wanted the autoscroll to work but the user should be able to scroll up as well during processing. With above implementation because of scroll adjustment listener i was not able to do that... So i was thinking to disable the boolean check flag when there is mouse click event on the scroll bar... After little googling i realized setCaretPosition() is even better to use then adding two listeners on the scroll bar. – Johnydep Dec 09 '11 at 13:57

1 Answers1

2

I'm pretty sure a mouse listener should help you to achieve what you want;

jScrollPane1.getVerticalScrollBar().addMouseListener(...)

Ben van Gompel
  • 747
  • 4
  • 12