Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6340879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:55:06+00:00 2026-05-24T19:55:06+00:00

Java Swing question. I have a JPanel which displays a graph. When I move

  • 0

Java Swing question.

I have a JPanel which displays a graph. When I move the mouse over this graph, I want certain information to be displayed on a tooltip-like widget that moves with the mouse. How can I best implement this?

I guess my problem will be solved if I know how to position a custom JComponent absolutely within the JPanel that acts as my drawing canvas. I could then trap the mouse moved event and reposition/update the widget. Any other solution (including possibly using JToolTip directly) would also be very much welcome!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:55:07+00:00Added an answer on May 24, 2026 at 7:55 pm

    Override the getToolTipText(MouseEvent) method to dynamically set the tool tip based on the mouse location.

    Edit:

    If you want the tooltip to continually move with the mouse then you will also need to override the getToolTipLocation() method.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ToolTipPanel extends JPanel
    {
        public ToolTipPanel()
        {
            setPreferredSize( new Dimension(200, 200) );
            setToolTipText("");
        }
    
        public void paintComponent(Graphics g)
        {
            g.setColor( Color.red );
            g.fillRect(0, 0, 100, 200);
            g.setColor( Color.blue );
            g.fillRect(100, 0, 100, 200);
        }
    
        public String getToolTipText(MouseEvent e)
        {
            if (e.getX() < 100)
                return "red";
            else
                return "blue";
        }
    
        public Point getToolTipLocation(MouseEvent e)
        {
            Point p = e.getPoint();
            p.y += 15;
            return p;
    //      return super.getToolTipLocation(e);
        }
    
        public static void main(String[] args)
        {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.getContentPane().add( new ToolTipPanel() );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a continuation of the question Java rounded Swing JButton . I have
I have made a java swing GUI. Now I want to display a static
I'm new to java so forgive the noob question. I have created a swing
i'm new in java and have a phase of code like this: import javax.swing.JOptionPane;
I have a question about GUI design, specifically with Java Swing and creating clean
We have a Java Swing application which contains components like JTable, JCombobox, JTextArea and
I have a formatting question for my Java swing application. It should be fairly
I have a very simple Java JTable question. I am creating this class to
My Java Swing application generates an HTML file, and I want to open it
I have a Java swing application with a panel that contains three JComboBoxe s

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.