I tried following program for test mouse moved method of java mouse adapter class, but it didn't work. I want to increase the progress bar's value from 2 when I move mouse on the Mouse Over button. How can I 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);
}
}