0

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);
    }
}
Beniton Fernando
  • 1,533
  • 1
  • 14
  • 21
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jun 11 '16 at 05:35
  • `public void mouseMoved(MouseEvent evt){` should be `@Override public void mouseMoved(MouseEvent evt){`. Incidentally, that will result in a compilation error that must be fixed.. – Andrew Thompson Jun 11 '16 at 05:39
  • @Andrew yet that doesn't solves the problem. The OP wants to have a mouse over behaviour and `mouseMoved` isn't the right method to override for that behaviour. Like it is written in the second answer of the possible duplicate, it would be `mouseEntered` and `mouseExited` to express the mouse going over the button (enter) and getting out of the boundaries (exit). – OliPro007 Jun 11 '16 at 05:43
  • 1
    Another point to note, if `mouseMoved` is indeed what the OP wants, then it is not a `MouseListener`, but a `MouseMotionListener`, so the OP would need to do `addMouseMotionListener` instead of `addMouseListener` ([source](http://stackoverflow.com/questions/10819967/java-mousemoved-event-handling-in-swing)). – OliPro007 Jun 11 '16 at 05:50
  • @OliPro007 *"yet that doesn't solves the problem"* Yet that wasn't (and wasn't intended as) an answer. – Andrew Thompson Jun 11 '16 at 12:41

1 Answers1

2

Modify your code like below. Hope it helps you to progress.

mouseButton.addMouseMotionListener(new MouseMotionListener() {

     @Override
     public void mouseDragged(MouseEvent e) {
     }

     @Override
     public void mouseMoved(MouseEvent e) {
         progress.setValue(x += 2);
     }
});
Beniton Fernando
  • 1,533
  • 1
  • 14
  • 21