Focus when Mouse Hover in Java

May 29, 2020
Tutorials
focussample

In this tutorial will tackle how to create a program that when a mouse is hovering it will focus on the button using java.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of focusSample.java.

2. Import the following package library:

import java.awt.*; //used to access a Component and GridLayout class
import java.awt.event.*; //used to access MouseAdapter,MouseEvent, and MouseListener class
import javax.swing.*; //used to access JButton and JFrame class

3. Create a class name MouseHover that will extend a MouseAdapter to access a component so that it can request focus. Use the JComponent class so that it will call the component to be focused.

class MouseHover extends MouseAdapter {
  public void mouseEntered(MouseEvent mouseEvent) {
    Component component = mouseEvent.getComponent();
    if (!component.hasFocus()) {
      component.requestFocusInWindow();
    }
  }
}

4. We will initialize variables in our Main, variable frame as JFrame, and mouseListener as the MouseHover class that we have declared above.

JFrame frame = new JFrame("Focus Sample");
MouseListener mouseListener = new MouseHover();

Now, we will create 6 buttons and will apply the Focus that we have declared the MouseHover class. We will use the setFocusable method of the buttons so that it can be focused.

for (int i = 1; i <= 6; i++) {
  JButton button = new JButton(Integer.toString(i));
  button.addMouseListener(mouseListener);
  button.setFocusable(true);
  frame.getContentPane().add(button);
}

5. Finally, set the size and visibility to true, and close its operation.Copy this code below:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(2, 3));
frame.setSize(300, 200);
frame.setVisible(true);


Output:

output


Here’s the full code of this tutorial:

import java.awt.*; //used to access a Component and GridLayout class
import java.awt.event.*; //used to access MouseAdapter,MouseEvent, and MouseListener class
import javax.swing.*; //used to access JButton and JFrame class
 
 
class MouseHover extends MouseAdapter {
  public void mouseEntered(MouseEvent mouseEvent) {
    Component component = mouseEvent.getComponent();
    if (!component.hasFocus()) {
      component.requestFocusInWindow();
    }
  }
}
public class focusSample{
  public static void main(String args[]) {
    JFrame frame = new JFrame("Focus Sample");
    MouseListener mouseListener = new MouseHover();
 
    for (int i = 1; i <= 6; i++) {
      JButton button = new JButton(Integer.toString(i));
      button.addMouseListener(mouseListener);
      button.setFocusable(true);
      frame.getContentPane().add(button);
    }
 
 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new GridLayout(2, 3));
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}
Download Code
Facebook Comments

Leave a Reply

Your email address will not be published.