Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Tooltips usage

Thread view: 
k0m0r - 02 Aug 2006 09:59 GMT
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
Thomas Fritsch - 02 Aug 2006 12:32 GMT
> I've been trying to solve it myself, but I simply don't get it :(
>
[quoted text clipped - 24 lines]
> 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);

Signature

Thomas

Vova Reznik - 02 Aug 2006 14:18 GMT
> Hi.
> I've been trying to solve it myself, but I simply don't get it :(
[quoted text clipped - 29 lines]
>
> 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);
    }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.