1

I'm writing a custom component that displays some bioinformatics data, and I'd like to be able to show additional information about the location the mouse is at when the user holds down a certain key. This seems like an obvious job for a tooltip, but there are a few problems that seem to be preventing this from working. First, I want to have the tooltip follow the mouse and change its text dynamically. This works somewhat by overriding getToolTipText and getToolTipLocation for the component, but the tooltip flickers as the mouse position is updated and doesn't display over the sub-components (it's a JPanel with some JTextPanes inside it). I also don't think there's any way to make it display immediately without a call to the ToolTipManager, which I believe would change the delay for all other components.

It looks like there are workarounds for some of these problems, but they're rather complicated and inelegant so I'm thinking a good solution would be to just create my own component, fill it with the relevant information and show it myself. However, this needs to be some kind of top-level component because it needs to be able to extend slightly beyond the borders of the parent component or even the containing JFrame and be drawn over everything else. The only objects I know of that have this functionality outside of JToolTip are JFrame and JDialog, which have borders with titles and close buttons which I don't want. Is there some way to accomplish this?

JaredL
  • 1,372
  • 2
  • 11
  • 27
  • 2
    [`Zoom`](http://stackoverflow.com/a/3742841/230513) updates without flickering. – trashgod Nov 01 '12 at 19:02
  • 1
    [or JLabel](http://stackoverflow.com/a/5957405/714968) but to use that with `int offset = textArea.viewToModel(...);` for `JTextComponents` or for derived `Editor`s from `Compound JComponents` – mKorbel Nov 01 '12 at 19:21

1 Answers1

3

One option is to use a glass pane. In this case your tooltip won't be able to go outside of the frame, but you can easily position it relative to how near it is to a side of the frame. Some example code that draws a bubble (which you can fill with text in the paint method) that follows the mouse.

    public static void main(String[] args)
{
    JFrame frame = new JFrame();
    frame.setSize(new Dimension(500, 500));

    JPanel glassPane = new JPanel();
    glassPane.setOpaque(false);
    glassPane.setLayout(null);

    frame.setGlassPane(glassPane);
    frame.getGlassPane().setVisible(true);


    final MyInfoBubble mib = new MyInfoBubble();
    mib.setBounds(10, 30, 100, 50);
    ((JPanel)frame.getGlassPane()).add(mib);

    frame.getContentPane().addMouseMotionListener(new MouseMotionAdapter() {

        public void mouseMoved(MouseEvent me) {
            mib.setBounds(me.getPoint().x, me.getPoint().y, 100, 50);
        }
    });

    ((JPanel)frame.getGlassPane()).validate();
    ((JPanel)frame.getGlassPane()).repaint();

    frame.setVisible(true);
}



static class MyInfoBubble extends JPanel
{
    public MyInfoBubble()
    {
        setVisible(true);
    }


    public void paintComponent(Graphics g)
    {
        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor(Color.BLUE);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
    }

}
martinez314
  • 12,162
  • 5
  • 36
  • 63