Tooltips usage

Discussion in 'Java' started by k0m0r, Aug 2, 2006.

  1. k0m0r

    k0m0r Guest

    Hi.
    I've been trying to solve it myself, but I simply don't get it :(

    I've got a JSlider and I need to print it's value in a tooltip that
    appears at the cursor position every time mouse enters the
    slider or changes its value (the tooltip should "chase" mouse
    position).
    I've been trying this:

    class JProxySlider extends JSlider
    implements ChangeListener {

    JToolTip tip = new JToolTip();

    JProxySlider() {
    .....
    tip.setComponent(this);
    this.addChangeListener(this);
    }

    public void stateChanged(ChangeEvent e) {
    tip.setToolTipText(this.getValue()+"");
    tip.setVisible(true);
    }
    }

    but I can't figure out how to fire the tooltip popup.
    I found also ToolTipManager, but I can't force it
    to work with this one.

    Please help.

    k0m0r
    k0m0r, Aug 2, 2006
    #1
    1. Advertising

  2. k0m0r wrote:
    > I've been trying to solve it myself, but I simply don't get it :(
    >
    > I've got a JSlider and I need to print it's value in a tooltip that
    > appears at the cursor position every time mouse enters the
    > slider or changes its value (the tooltip should "chase" mouse
    > position).
    > I've been trying this:
    >
    > class JProxySlider extends JSlider
    > implements ChangeListener {
    >
    > JToolTip tip = new JToolTip();
    >
    > JProxySlider() {
    > .....
    > tip.setComponent(this);
    > this.addChangeListener(this);
    > }
    >
    > public void stateChanged(ChangeEvent e) {
    > tip.setToolTipText(this.getValue()+"");
    > tip.setVisible(true);
    > }
    > }
    >
    > but I can't figure out how to fire the tooltip popup.
    > I found also ToolTipManager, but I can't force it
    > to work with this one.


    Try this:
    class JProxySlider extends JSlider
    implements ChangeListener {

    JProxySlider() {
    setToolTipText(this.getValue()+"");
    this.addChangeListener(this);
    }

    public void stateChanged(ChangeEvent e) {
    // update the tooltip to current slider value
    setToolTipText(this.getValue()+"");
    }

    // overridden, so that the tooltip follows the mouse position
    public Point getToolTipLocation(MouseEvent event) {
    return new Point(event.getX() + 15, event.getY());
    }
    }

    BTW, what makes you want to use tooltips to display the current slider
    value?
    Why not doing it more conventional?, i.e. by using the JSlider-API to
    draw ticks/labels permanently. For example:
    this.setPaintLabels(true);
    this.setMajorTickSpacing(20);
    this.setMinorTickSpacing(5);
    this.setPaintTicks(true);

    --
    Thomas
    Thomas Fritsch, Aug 2, 2006
    #2
    1. Advertising

  3. k0m0r

    Vova Reznik Guest

    k0m0r wrote:
    > Hi.
    > I've been trying to solve it myself, but I simply don't get it :(
    >
    > I've got a JSlider and I need to print it's value in a tooltip that
    > appears at the cursor position every time mouse enters the
    > slider or changes its value (the tooltip should "chase" mouse
    > position).
    > I've been trying this:
    >
    > class JProxySlider extends JSlider
    > implements ChangeListener {
    >
    > JToolTip tip = new JToolTip();
    >
    > JProxySlider() {
    > .....
    > tip.setComponent(this);
    > this.addChangeListener(this);
    > }
    >
    > public void stateChanged(ChangeEvent e) {
    > tip.setToolTipText(this.getValue()+"");
    > tip.setVisible(true);
    > }
    > }
    >
    > but I can't figure out how to fire the tooltip popup.
    > I found also ToolTipManager, but I can't force it
    > to work with this one.
    >
    > Please help.
    >
    > k0m0r
    >

    All you need is to overright
    public String getToolTipText(MouseEvent event) {
    return "" + getValue();
    }
    of JComponent and call setToolTipText in a constructor.


    import java.awt.BorderLayout;
    import java.awt.event.MouseEvent;

    import javax.swing.JFrame;
    import javax.swing.JSlider;

    class JProxySlider extends JSlider {
    JProxySlider() {
    super(5, 25);
    setToolTipText(this.getValue() + "");
    }

    public String getToolTipText(MouseEvent event) {
    return "" + getValue();
    }

    public static void main(String agrs[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JProxySlider(), BorderLayout.CENTER);
    f.getContentPane().add(new JProxySlider(), BorderLayout.NORTH);
    f.getContentPane().add(new JProxySlider(), BorderLayout.SOUTH);
    f.setLocationRelativeTo(null);
    f.pack();
    f.setVisible(true);
    }
    }
    Vova Reznik, Aug 2, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stevie_mac

    Tooltips on datagrid titles

    Stevie_mac, Jul 25, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    445
    Stevie_mac
    Jul 25, 2003
  2. Roberto López

    ToolTips delay and display times??

    Roberto López, Aug 22, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    4,386
    Roberto López
    Aug 23, 2003
  3. Steve Chatham

    tooltips in datagrid?

    Steve Chatham, Feb 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    371
    =?Utf-8?B?QmFycnkgRm9ycmVzdA==?=
    Feb 18, 2004
  4. Shaul Feldman

    Tooltips in intellisence

    Shaul Feldman, Apr 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    356
    Shaul Feldman
    Apr 3, 2004
  5. Frank
    Replies:
    2
    Views:
    497
    BootNic
    Dec 7, 2006
Loading...

Share This Page