java - How to write mouse moved event for JButton -
this question has answer here:
- mouse on events jbutton 2 answers
i tried following program test mouse moved method of java mouse adapter class, didn't work. want increase progress bar's value 2 when move mouse on mouse on button. how can fix this?
import javax.swing.*; import java.awt.*; import java.awt.event.*; class progressdemo extends jframe{ private jprogressbar progress; private jbutton mousebutton; static int x = 2; progressdemo(){ progress = new jprogressbar(jprogressbar.horizontal,0,100); progress.setbounds(50,100,500,15); progress.setstringpainted(true); mousebutton = new jbutton("mouse over"); mousebutton.addmouselistener(new mouseadapter(){ public void mousemoved(mouseevent evt){ progress.setvalue(x+=2); } }); jpanel mousepanel = new jpanel(new flowlayout(flowlayout.center)); mousepanel.setbounds(0,10,600,50); mousepanel.add(mousebutton); setsize(600,200); setlayout(null); add(mousepanel); add(progress); setdefaultcloseoperation(exit_on_close); setlocationrelativeto(null); } } class jprogress{ public static void main(string args[]){ progressdemo p1 = new progressdemo(); p1.setvisible(true); } }
modify code below. hope helps progress.
mousebutton.addmousemotionlistener(new mousemotionlistener() { @override public void mousedragged(mouseevent e) { } @override public void mousemoved(mouseevent e) { progress.setvalue(x += 2); } });
Comments
Post a Comment