Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to use ToolTip' functions on a Graphics area?

647 views
Skip to first unread message

Vinicius Pereira

unread,
Jun 8, 2009, 12:14:05 PM6/8/09
to
Hello,

I'm having some difficulties in applying the ToolTips functions in a
graphic program in java.

I was trying to make my canvas inherit the functions of ToolTip, to
when the mouse move over the draw area, appear a "tip" on the side of
the mouse, showing a text.
I already try a lot of things and nothing sens to work, I'm thinking
in do it manually. But I prefer to use the ToolTip package.
The sites that I'm using are:

http://java.sun.com/docs/books/tutorial/uiswing/components/tooltip.html

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#celltooltip

http://help.eclipse.org/stable/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/window/DefaultToolTip.html

This last one is perfect to what I want, because it controls when and
how the tip is show. But I'm can't implement it at all.

Can somebody send me a example where its implemented?
Or a link to a tutorial?
Or modify this code to show me how it's done:

import java.applet.Applet;
import java.awt.Graphics;

public class Teste01 extends Applet{

public void init(){
repaint();
}

public void paint(Graphics g){
g.drawRect(10, 10, 50, 50);
}
}


Thanks in advance.

John B. Matthews

unread,
Jun 8, 2009, 1:16:11 PM6/8/09
to
In article
<bcd1c115-f525-4792...@t21g2000yqi.googlegroups.com>,
Vinicius Pereira <vinicius...@gmail.com> wrote:

> I'm having some difficulties in applying the ToolTips functions in a
> graphic program in java.
>
> I was trying to make my canvas inherit the functions of ToolTip, to
> when the mouse move over the draw area, appear a "tip" on the side of
> the mouse, showing a text.

I like TextLayout for this [1], but you can do it manually [2], too.
Just have paint() or paintComponent() draw it relative to the
coordinates given by your mouse-motion listener [3].

[1]<http://java.sun.com/javase/6/docs/api/java/awt/font/TextLayout.html>
[2]<http://sites.google.com/site/drjohnbmatthews/composite>
[3]<http://java.sun.com/docs/books/tutorial/uiswing/events/mousemotionlistener.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Vinicius Pereira

unread,
Jun 8, 2009, 3:20:19 PM6/8/09
to
On 8 jun, 14:16, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <bcd1c115-f525-4792-a8e5-260a3d362...@t21g2000yqi.googlegroups.com>,

>  Vinicius Pereira <viniciusdenov...@gmail.com> wrote:
>
> > I'm having some difficulties in applying the ToolTips functions in a
> > graphic program in java.
>
> > I was trying to make my canvas inherit the functions of ToolTip, to
> > when the mouse move over the draw area, appear a "tip" on the side of
> > the mouse, showing a text.
>
> I like TextLayout for this [1], but you can do it manually [2], too.
> Just have paint() or paintComponent() draw it relative to the
> coordinates given by your mouse-motion listener [3].
>
> [1]<http://java.sun.com/javase/6/docs/api/java/awt/font/TextLayout.html>
> [2]<http://sites.google.com/site/drjohnbmatthews/composite>
> [3]<http://java.sun.com/docs/books/tutorial/uiswing/events/mousemotionlis...>

>
> --
> John B. Matthews
> trashgod at gmail dot com
> <http://sites.google.com/site/drjohnbmatthews>

Hello,

Thanks John B. Matthews

Yes, that was what I think, but I was looking for a way to use these
ToolTip's functions of these packages.
I think that if use these functions, my program will be more
resourceful.
But I can make it manually.

My principal goal, is that my ToolTip look like this on of this site:
http://faculty.salisbury.edu/~despickler/pascgalois/Zn.html

In the applet on this site, press the button of "refresh" in the right-
superior corner.
The applet will show a colour figure of Pascal's Triangle.
Then put the mouse's cursor on the triangle and then appear a "tip" on
the side of the cursor. In the tip, appear the coefficient number of
the cell of the triangle which the cursor is.

Thanks again!

Knute Johnson

unread,
Jun 8, 2009, 3:36:25 PM6/8/09
to

Tooltips are built in to Swing components. Why don't you use a JApplet
and a JPanel instead of the Applet and Canvas?

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

public class test extends JApplet {
public void init() {
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(100,75));
p.setBackground(Color.BLUE);
p.setToolTipText("My Blue JPanel");
add(p);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test t = new test();
t.init();
f.add(t,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
}

--

Knute Johnson
email s/nospam/knute2009/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Vinicius Pereira

unread,
Jun 8, 2009, 3:57:32 PM6/8/09
to
On 8 jun, 16:36, Knute Johnson <nos...@rabbitbrush.frazmtn.com> wrote:
> Vinicius Pereira wrote:
> > Hello,
>
> > I'm having some difficulties in applying the ToolTips functions in a
> > graphic program in java.
>
> > I was trying to make my canvas inherit the functions of ToolTip, to
> > when the mouse move over the draw area, appear a "tip" on the side of
> > the mouse, showing a text.
> > I already try a lot of things and nothing sens to work, I'm thinking
> > in do it manually. But I prefer to use the ToolTip package.
> > The sites that I'm using are:
>
> >http://java.sun.com/docs/books/tutorial/uiswing/components/tooltip.html
>
> >http://java.sun.com/docs/books/tutorial/uiswing/components/table.html...
>
> >http://help.eclipse.org/stable/index.jsp?topic=/org.eclipse.platform....

Hi Knute Johnson,
Yes, I already try this,

But how can I add a Graphics to the Panel?
This is my realy problem.

John B. Matthews

unread,
Jun 8, 2009, 4:07:35 PM6/8/09
to
In article
<21058638-43a9-4759...@k2g2000yql.googlegroups.com>,
Vinicius Pereira <vinicius...@gmail.com> wrote:

> On 8 jun, 14:16, "John B. Matthews" <nos...@nospam.invalid> wrote:
> > In article
> > <bcd1c115-f525-4792-a8e5-260a3d362...@t21g2000yqi.googlegroups.com>,
> >  Vinicius Pereira <viniciusdenov...@gmail.com> wrote:

[...]


> Thanks John B. Matthews
>
> Yes, that was what I think, but I was looking for a way to use these
> ToolTip's functions of these packages. I think that if use these
> functions, my program will be more resourceful. But I can make it
> manually.
>
> My principal goal, is that my ToolTip look like this on of this site:
> http://faculty.salisbury.edu/~despickler/pascgalois/Zn.html
>
> In the applet on this site, press the button of "refresh" in the
> right- superior corner. The applet will show a colour figure of
> Pascal's Triangle. Then put the mouse's cursor on the triangle and
> then appear a "tip" on the side of the cursor. In the tip, appear the
> coefficient number of the cell of the triangle which the cursor is.

As Knute Johnson's example in this thread shows, JComponents have that
functionality already. Your example appears to be a JButton, which is
also a JComponent. Here's an example of a custom "tip" that follows the
mouse:

<code>
package gui;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TipTest extends JPanel {

private static Color hiliteColor = new Color(0xFFFFC0);
private static Font font = new Font("SansSerif", Font.PLAIN, 12);
private FontRenderContext frc =
new FontRenderContext(null, false, false);
private Point pt = new Point(Short.MAX_VALUE, Short.MAX_VALUE);

public TipTest() {
setPreferredSize(new Dimension(320, 240));
this.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
pt.setLocation(e.getX(), e.getY());
TipTest.this.repaint();
}
});
}

@Override
public void paintComponent(Graphics g) {
Graphics2D g2D = (Graphics2D) g;

g.setColor(Color.lightGray);
g.fillRect(0, 0, getWidth(), getHeight());
String s = pt.x + "," + pt.y;
Rectangle2D.Float r = (Rectangle2D.Float)
font.getStringBounds(s, frc);
r.setRect(r.x + pt.x - 3d, r.y + pt.y - 2d,
r.width + 6d, r.height + 4d);
g2D.setPaint(hiliteColor);
g2D.fill(r);
TextLayout layout = new TextLayout(s, font, frc);
g2D.setPaint(Color.black);
layout.draw(g2D, (float) pt.x, (float) pt.y);
g2D.setPaint(Color.blue);
g2D.draw(r);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.add(new TipTest(), BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
}
</code>

Vinicius Pereira

unread,
Jun 8, 2009, 5:01:43 PM6/8/09
to
On 8 jun, 17:07, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <21058638-43a9-4759-9c50-32c3e46ba...@k2g2000yql.googlegroups.com>,
> John B. Matthews, Knute Johnson.

> trashgod at gmail dot com
> <http://sites.google.com/site/drjohnbmatthews>

Thanks
I guess it's better do it manually, like the code above.

Thanks John B. Matthews.
Thanks Knute Johnson.
Thanks for the help.

Knute Johnson

unread,
Jun 8, 2009, 5:34:12 PM6/8/09
to

To which panel, the tooltip or to the JPanel?

Knute Johnson

unread,
Jun 8, 2009, 5:54:23 PM6/8/09
to
Vinicius Pereira wrote:
> Hi Knute Johnson,
> Yes, I already try this,
>
> But how can I add a Graphics to the Panel?
> This is my realy problem.

The example you posted to John shows a different tooltip based on the
location on the panel. That is very easily done by overriding the
JComponent.getToolTipText(MouseEvent) method. See the modified example
below.

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

public class test extends JApplet {
public void init() {

JPanel p = new JPanel() {
public String getToolTipText(MouseEvent me) {
int x = me.getX();
int y = me.getY();
int halfH = getHeight() / 2;
int halfW = getWidth() / 2;

if (x < halfW && y < halfH)
return "Northwest";
else if (x > halfW && y < halfH)
return "Northeast";
else if (x < halfW && y > halfH)
return "Southwest";
else
return "Southeast";
}
};
p.setPreferredSize(new Dimension(100,75));
p.setBackground(Color.BLUE);

John B. Matthews

unread,
Jun 8, 2009, 9:44:43 PM6/8/09
to
In article
<b9ed5d55-6012-413c...@z9g2000yqi.googlegroups.com>,
Vinicius Pereira <vinicius...@gmail.com> wrote:

> I guess it's better do it manually, like the code above.

I don't know. I'm still a little vague on your goal. Knute's second
example shows how to override getToolTipText() to customize the text
based on location, something I'd overlooked earlier. My example shows a
completely custom approach. The former is easier and more consistent;
the latter is more flexible but more effort.

You may have enough now to put together an example of what you're trying
to accomplish. Keep us posted on your progress.

--
John B. Matthews

0 new messages