Link to home
Start Free TrialLog in
Avatar of srulison
srulison

asked on

Displaying discriptive text to a Java component shuch as JMenuItem or JButton, etc.

Thank you for taking to time to read my post.

Can somebody tell me a good way to display help text for a JMenuitem or JButton?  In other words the text would be displayed in the Frame whenever a particular component is moused over or highlighted.

 I'm thinking something like itemListener or mouse listener would work but I am just not sure what the best approach is.  For example, in the program below, if I wanted to display a message "Selecting this option will close the application " when the user hovers the mouse over the JMenuItem menuItmExit, what would be the best approach to providing this type of functionality?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class MyMenu extends JFrame
            implements ActionListener
{
      JMenuItem menuItmExit;
 
      public static void main(String[] args)
      {
            MyMenu mm = new MyMenu();
      }

      MyMenu()
      {
            setBounds(100,100, 300, 300);
            setTitle ("My Menu");

            JMenuBar mbar = new JMenuBar();

            JMenu menuFile = new JMenu("File");
            mbar.add(menuFile);
            
            JMenu menuView = new JMenu("View");
            mbar.add(menuView);

            JMenu menuLeaveApp = new JMenu("Leave");
            menuItmExit = new JMenuItem("Exit");
            menuItmExit.addActionListener(this);
            menuLeaveApp.add(menuItmExit);

            mbar.add(menuLeaveApp);

            setJMenuBar(mbar);

            setVisible(true);
      }

      public void actionPerformed(ActionEvent evt)
      {
            if(evt.getSource() == menuItmExit)
                  System.exit(0);
      }

}//End of class MyMenu.
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of srulison
srulison

ASKER

Thank you for your help. That was a whole lot easier than I thought.
Great.