Java Hover Effect
Most of the beginners want to know how the Java hover effect works. In Java programming language, we can easily do it by using those mouse events. Before we dig into that and if you find it helpful, make sure you follow this website to keep you updated on our future blog post ?. First, you need to have a component. Examples of components are the buttons, checkboxes, scrollbars, etc. Once you have a component, create a MouseEntered event. Let us assume I have JLabel with a variable name hover. See this sample code.
//Mouse Entered private void hoverMouseEntered(java.awt.event.MouseEvent evt){ hover.setText("Your cursor entered!"); }
Every time the cursor entered on the JLabel, the text will be automatically set the text whatever text you’ve set on it. Now let’s proceed on the MouseExited event.
//Mouse Exited private void hoverMouseExited(java.awt.event.MouseEvent evt){ hover.setText("Your cursor just exited!"); }
Hover effect in Java
How easy is that! ? Now try it on your computer. We’re still not yet done. Let us also try the Image hover effect in Java!
//Mouse Entered hovering an icon private void hoverMouseEntered(java.awt.event.MouseEvent evt){ ImageIcon ii = new ImageIcon(getClass().getResource("icon2.png"); hover.setIcon(ii); }
and for the MouseExited ?
//Mouse Exited private void hoverMouseExited(java.awt.event.MouseEvent evt){ ImageIcon ii = new ImageIcon(getClass().getResource("icon1.png"); hover.setIcon(ii); }
That’s it! very easy right?. I hope you have learned something new! Happy coding! Watch this youtube video to see the output