Text displayed when mouse a coordinate

B

Ben

Hi,

I think this is a hard one. I've been looking around the net on how to
do this, can't find any info.

Firstly is this hard to implement?

The problem:
I have a JPanel. What I want to happen is some predetermined text to
appear over the mouse or in a JTextField on another panel, when the
mouse moves over or within 5 pixels of different coordinates. Text
above the mouse would be my prefferable implementation.

What do I need to look at to findout how this can be implemented?

Thanks again.

Ben.
 
I

Ian Mills

Ben said:
Hi,

I think this is a hard one. I've been looking around the net on how to
do this, can't find any info.

Firstly is this hard to implement?

The problem:
I have a JPanel. What I want to happen is some predetermined text to
appear over the mouse or in a JTextField on another panel, when the
mouse moves over or within 5 pixels of different coordinates. Text
above the mouse would be my prefferable implementation.

What do I need to look at to findout how this can be implemented?

Thanks again.

Ben.
Try looking at MouseMotionListener as a starting point or maybe
MouseMotionAdapter.
 
B

Ben

Hi,

I took your advice and came up with the following. I have a class that
initialises the MouseMotion class (code below). In the initialising
class I have also used
ToolTipManager.sharedInstance().setEnabled(true); to enable the
tooltips for the whole application. I also added the listener to a
panel using panel1.addMouseMotionListener(mouseListener);. (adding
tooltips to buttons etc works fine)
The code below prints a line to the screen when the mouse is moved over
a coordinate on the panel. How do I change this to a tooltip?

Thanks for any help.

Ben.


import java.awt.*;
import java.awt.event.*;

public class MouseMotion implements MouseMotionListener
{
// The X-coordinate and Y-coordinate ofthe last Mouse Position.
int xpos;
int ypos;

public void mouseMoved(MouseEvent me)
{
xpos = me.getX();
ypos = me.getY();

if(xpos = 10 && y pos == 10)
{
System.out.println("Area 1");
}


if(xpos = 20 && y pos == 20)
{
System.out.println("Area 2");
}


if(xpos = 30 && y pos == 30)
{
System.out.println("Area 3");
}
}

public void mouseDragged(MouseEvent me)
{
}
}
 
I

Ian Mills

Ben said:
Hi,

I took your advice and came up with the following. I have a class that
initialises the MouseMotion class (code below). In the initialising
class I have also used
ToolTipManager.sharedInstance().setEnabled(true); to enable the
tooltips for the whole application. I also added the listener to a
panel using panel1.addMouseMotionListener(mouseListener);. (adding
tooltips to buttons etc works fine)
The code below prints a line to the screen when the mouse is moved over
a coordinate on the panel. How do I change this to a tooltip?

Thanks for any help.

Ben.


import java.awt.*;
import java.awt.event.*;

public class MouseMotion implements MouseMotionListener
{
// The X-coordinate and Y-coordinate ofthe last Mouse Position.
int xpos;
int ypos;

public void mouseMoved(MouseEvent me)
{
xpos = me.getX();
ypos = me.getY();

if(xpos = 10 && y pos == 10)
{
System.out.println("Area 1");
}


if(xpos = 20 && y pos == 20)
{
System.out.println("Area 2");
}


if(xpos = 30 && y pos == 30)
{
System.out.println("Area 3");
}
}

public void mouseDragged(MouseEvent me)
{
}
}
I haven't done this myself but you could try creating a JToolTip (using
the createToolTip() method) then doing a setTipText and setVisible(true)
where you are doing your System.out.println
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top