Java Swing - mouseMoved event fires slowly

I am currently experiencing problems with the mouseMoved event in Java - Swing. In short, I have a JPanel, and I connected a MouseMotionListener to it to hide or show JscrollPane on the fly:

myPanel.addMouseMotionListener(new MousePresenter()); 

I have my own class that implements the MouseMotionListener interface:

 public class MousePresenter implements MouseMotionListener { public void mouseMoved(MouseEvent e) { int x = e.getX(); int y = e.getY(); if (x>20 && x<200) { hideScrollBar(); } else { showScrollBar(); } } } 

The problem is that the mouseMoved event does not fire often enough. Is there any related solution to this problem when using MouseMotionListener?

Thank you for your time.

+4
source share
5 answers

Everything seems to work just fine for me. Note that handling the event is pretty fast:

  public static void main( String[] args ) { EventQueue.invokeLater( new Runnable() { @Override public void run() { JFrame frame = new JFrame( "TestFrame" ); JPanel content = new JPanel( new BorderLayout() ); final JLabel mousePosition = new JLabel( "Unknown" ); content.add( mousePosition, BorderLayout.NORTH ); content.addMouseMotionListener( new MouseMotionAdapter() { @Override public void mouseMoved( MouseEvent e ) { mousePosition.setText( "X: " + e.getX() + " Y: " + e.getY() ); } } ); frame.setContentPane( content ); frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE ); frame.pack(); frame.setVisible( true ); } } ); } 

This may not be the case for your hideScrollBar method hideScrollBar

+2
source

The mouse movement event is inherently slow, since it fires with every change in the pixel.

The only thing you can do to optimize the whole problem is to optimize what you are doing inside the callback handler. In your case you have

 if (something) doA(); else doB(); 

This means that in any case, you are trying to show or hide the scroll bar, even if it is already shown or hidden. What you can do is:

 if (scrollBarIsVisible && x>20 && x<200) { hideScrollBar(); scrollBarIsVisible = false; } else if (!scrollBarIsVisible) { showScrollBar(); scrollBarIsVisible = true; } 

So that you only change the visibility of an element (which can be a difficult operation, as events may need to be transmitted) when switching from borders to external and vice versa. This should significantly reduce computing operations.

+1
source

If you all execute your code on a Dispatch thread, this can cause problems. Take a look at trail and try putting any code that does a great job in SwingWorker .

+1
source

Your code is not very optimized. Be that as it may, it will always invoke either a show or hide scrolling methods. You should probably change it, for example, it hides it only if it is displayed, and displays it only if it is hidden.

+1
source

The problem is resolved. There was a specific performance issue in my application that caused such delays. Thank you for your efforts and the information and advice that you have provided.

0
source

Source: https://habr.com/ru/post/1436566/


All Articles