Detecting mouseClick event on JScrollPane in Java Swing

风格不统一 提交于 2019-12-11 02:07:11

问题


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


回答1:


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

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



来源:https://stackoverflow.com/questions/8445413/detecting-mouseclick-event-on-jscrollpane-in-java-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!