How to change jtooltip timers for 1 component

I have a component where I want to display custom jtooltip. It's easy, just change the getTooltip method. Similarly for location and text.

However, I also want to change the timers. A tooltip should always be displayed if the mouse is over the cellular agent of the component. If he leaves all this, he must be invisible. I know that I can use TooltipManager to manage time around the world. But the best solution is probably just a short circuit and displaying the prompt itself with the mouse. However, when I tried to do this (unregister the component in the TooltipManager and set the visibility of the tooltip with the text and in the correct position in the mouse listener), the tooltip never showed at all. What am I doing wrong?

Edit: Now the question has changed! In 2 questions.

My solution at the moment is this, but it will lose the shadow that jtooltip always displays sometimes frustratingly, and it is hidden if the mouse enters a popup window. How to filter mouseexit events over a popup if the popup is not even a component? I could do hacking based on lastPosition, but that seems silly since I don't know its width.

    private Popup lastPopup;
    private final JToolTip tooltip = ...;
    private Point lastPoint;
   @Override public void mouseMoved(MouseEvent e) {
        Point p = privateToolTipLocation(e);
        if (p == null || p.equals(lastPoint)) {
            return;
        }
        lastPoint = p;
        tooltip.setTipText(privateToolTipText(e));
        //copy
        p = new Point(p);
        SwingUtilities.convertPointToScreen(p, this);
        Popup newPopup = PopupFactory.getSharedInstance().getPopup(this, tooltip, p.x, p.y);
        if (lastPopup != null) {
            lastPopup.hide();
        }
        lastPopup = newPopup;
        newPopup.show();
    }

    @Override public void mouseExited(MouseEvent e) {
        if (lastPopup != null && someUnknownCondiction) {
            lastPopup.hide();
            lastPopup = null;
        }
    }
+3
source share
3 answers

Instead of trying to override the display of tooltips, you can add a mouse listener to your component, which changes the global tooltip timer when the mouse enters and leaves the area above the component.

Here is a sample code:

instantTooltipComponent.addMouseListener(new MouseAdapter() 
{    
    final int defaultTimeout = ToolTipManager.sharedInstance().getInitialDelay();

    @Override
    public void mouseEntered(MouseEvent e) {
        ToolTipManager.sharedInstance().setInitialDelay(0);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        ToolTipManager.sharedInstance().setInitialDelay(defaultTimeout);
    }
});

, , .

+6

, , tooltip mouselistener

:

Action toolTipAction = component.getActionMap().get("postTip");

if (toolTipAction != null)
{
    ActionEvent postTip = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, "");
    toolTipAction.actionPerformed( postTip );
}

Edit:

. Ctrl + F1 - , . , Ctrl + F1 KeyStroke . :

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

public class PostTipSSCCE2 extends JPanel
{
    public PostTipSSCCE2()
    {
        FocusAdapter fa = new FocusAdapter()
        {
            public void focusGained(FocusEvent e)
            {
                JComponent component = (JComponent)e.getSource();

                KeyEvent ke = new KeyEvent(
                    component,
                    KeyEvent.KEY_PRESSED,
                    System.currentTimeMillis(),
                    KeyEvent.CTRL_MASK,
                    KeyEvent.VK_F1,
                    KeyEvent.CHAR_UNDEFINED);

                component.dispatchEvent( ke );
            }
        };

        MouseAdapter ma = new MouseAdapter()
        {
            @Override
            public void mouseEntered(MouseEvent e)
            {
                JComponent component = (JComponent)e.getSource();

                KeyEvent ke = new KeyEvent(
                    component,
                    KeyEvent.KEY_PRESSED,
                    System.currentTimeMillis(),
                    KeyEvent.CTRL_MASK,
                    KeyEvent.VK_F1,
                    KeyEvent.CHAR_UNDEFINED);

                component.dispatchEvent( ke );
            }
        };

        JButton button = new JButton("Button");
        button.setToolTipText("button tool tip");
        button.addFocusListener( fa );
        button.addMouseListener( ma );
        add( button );

        JTextField textField = new JTextField(10);
        textField.setToolTipText("text field tool tip");
        textField.addFocusListener( fa );
        textField.addMouseListener( ma );
        add( textField );

        JCheckBox checkBox =  new JCheckBox("CheckBox");
        checkBox.setToolTipText("checkbox tool tip");
        checkBox.addFocusListener( fa );
        checkBox.addMouseListener( ma );
        add( checkBox );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("PostTipSSCCE2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new JScrollPane(new PostTipSSCCE2()) );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
+3

, , getTooltipText null . null, npe . .

0

Source: https://habr.com/ru/post/1736740/


All Articles