Search code examples
javaswingjscrollpane

Detecting mouseClick event on JScrollPane in Java Swing


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??


Solution

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

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