I want to hover over a number of JButtons on my GUI (map) and display the name of that location e.g. Manchester and London. I have the code working for one button, but it does not work for more than one button and prints the last out
message (as i have 10 buttons) for all button locations.
If button1
is true it then draws the text on the GUI in the specified area via my paintComponent()
method.
How can i resolve this?
button1.addMouseMotionListener(this);
button2.addMouseMotionListener(this);
public void mouseMoved(MouseEvent arg0)
{
if(button1.contains(arg0.getPoint()))
{
button1 = true;
out = "test 1";
repaint();
}
if(!button1.contains(arg0.getPoint()))
{
b1 = false;
out = " ";
repaint();
}//same for all 10 buttons but change variables
}
Why not use the tool tip API that already exists?
You even use HTML text to produce formatted results.
If the images are embedded, you can even supply an image...
don't to use
MouseListener
orMosueMotionListener
fromJButton
, this method are correctly implemented inJButtons API
,there no reason, I can't found reason to use
repaint()
for this jobanother way is add
ChangeListener
toJButton
and take rellated event(s) from derivedButtonModel
for better help sooner post an SSCCE, short, runnable, compilable, just about
JFrame
with oneJButton
Well this answer is coolio for JDK 8 users, so try it out:
for regular text
for html use