javauser-interfacemouseeventmouse-listeners

How to identify which button the mouse has entered using mouse listener in Dr Java


how do I set the Jlabel info3 to different text messages depending on which button the mouse hovers over? Right now I do not know how to distinguish which button the mouse is currently hovering over. Here is my code

import javax.swing.*;//You need these two libraries to do the GUI stuff
import java.awt.*;
import java.awt.event.*;

public class GUI_integrator extends JFrame implements ActionListener, MouseListener//You are telling Java that this is a GUI application
{ 

  // *** CREATE THE GUI COMPONENTS: Here we are only creating the following: Buttons, Labels, and Text field

  //buttons
  JButton EI = new JButton ("Emotional intelligence");//emotional intelligence test button
  JButton MI = new JButton ("Multiple intelligences");//multiple intelligences test
  JButton MB = new JButton ("Myers-Briggs assessment");//myers-briggs assessment
  JButton LS = new JButton ("Learning styles");//learning styles test
  JButton CP = new JButton ("Career preferences");//career preferences test

  //panels
  JPanel p1 = new JPanel();// panel for the information
  JPanel p2 = new JPanel();//panel for the selection of tests
  JPanel p3 = new JPanel();// panel to display information on the tests

  //labels
  JLabel info1 = new JLabel("Welcome! Please take a variety of tests so information can be gathered in order to determine what ",JLabel.RIGHT);
  JLabel info2 = new JLabel ("learning strategies will be most beneficial to you ", JLabel.RIGHT);
JLabel info3 = new JLabel ("Select the desired test");

// **********                      

  // *** CONSTRUCTOR - Has the same name as the class and it runs once (when the program begins)
  // This is were the GUI should be configured.

  public GUI_integrator() //This section adds the components on to the frame
  { 
    setTitle("Psychology Simulator 2015");  //Set the window title or  frame
    setSize(640, 480);         //Set the dimensions of

    FlowLayout fl1 = new FlowLayout(); //used to organize window

    GridLayout g1 = new GridLayout();
    g1.setColumns(1);
    g1.setRows(5);
    setLayout(g1);

//set the layout of the panels      
    p1.setLayout(fl1);
    p2.setLayout(fl1);
p3.setLayout(fl1); 

//add welcome greeting to first panel
    p1.add(info1);
    p1.add(info2);
p3.add(info3);    

//add buttons to second panel
    p2.add(EI);
    p2.add(MI);
    p2.add(MB);
    p2.add(LS);
    p2.add(CP);

    //add the panels
    add(p1);
    add(p2);
    add(p3);

//add action listeners to buttons
    EI.addActionListener(this);
    MI.addActionListener(this);
    MB.addActionListener(this);
    LS.addActionListener(this);
    CP.addActionListener(this);

    //add mouse listeners
    EI.addMouseListener(this);
    MI.addMouseListener(this);
    MB.addMouseListener(this);
    LS.addMouseListener(this);
    CP.addMouseListener(this);


    setVisible(true);       // display the gui on the screen
    setResizable(false); //the user cannot resize the window
  }

  public void actionPerformed(ActionEvent event)
  {
    String command =event.getActionCommand();

//check to see if buttons are pressed
    if (command.equals ("Emotional intelligence"))
    {}
    if (command.equals("Multiple intelligences"))
    {}
    if (command.equals("Myers-Briggs assessment"))
    {}
    if (command.equals("Learning styles"))
    {}
    if (command.equals("Career preferences"))
    {}


  }

public void mouseExited(MouseEvent event)
{}

public void mousePressed(MouseEvent event)
{}

public void mouseEntered(MouseEvent event)
{info3.setText("hi");}

public void mouseReleased (MouseEvent event)
{}

public void mouseClicked(MouseEvent event)
{}

// **** This is the main method - It just starts the GUI
  public static void main(String[] args) {
    GUI_integrator frame1 = new GUI_integrator();  //start the GUI!
  }

}

Solution

  • The information about the Component that generated the event is containted in the MouseEvent itself:

    public void mouseEntered(MouseEvent event)
    {
        Component c = event.getComponent();
        System.out.println(c);
    }
    

    For other events you would use the getSource() method.