Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ToolTipManager in Java

I am implementing a MVC and I want to have a JToolTip on a JLabel in my view. I succeeded but now I want to set the dismiss and initial timer.

I read the Javadoc for JToolTip and TollTipManager. I found what I want in ToolTipManager but now I can't understand how to use it.

JToolTip doesn't have a setter for ToolTipManager and in the doc they said:

Manages all the ToolTips in the system.

I don't really understand how I should use ToolTipManager. Should my View extend ToolTipManager or am I mislead?

like image 548
Julien Breuil Avatar asked Jun 21 '12 07:06

Julien Breuil


3 Answers

ToolTipManager is a singleton. To use it, you need to call a static sharedInstance method that returns the instance of this class.

ToolTipManager manages all tooltips - it is a global configuration class, changes made to the sharedInstance will be applied to every tooltip in your application.

like image 88
npe Avatar answered Nov 04 '22 07:11

npe


Here is an example showing how to use the ToolTipManager:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;

public class TestTooltips {

    protected static void initUI() {
        ToolTipManager.sharedInstance().setInitialDelay(0);
        ToolTipManager.sharedInstance().setDismissDelay(500);
        JFrame frame = new JFrame("test");
        JLabel label = new JLabel("Label text");
        label.setToolTipText("My cool nice and fun tooltip");
        frame.add(label);
        frame.pack();
        frame.setVisible(true);
    }

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

            @Override
            public void run() {
                initUI();
            }
        });
    }
}
like image 23
Guillaume Polet Avatar answered Nov 04 '22 06:11

Guillaume Polet


No idea what you tried and what you want to do... do you have success with the following code?

import java.awt.*;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;

public class JScrollableToolTip extends JToolTip {

    private static final long serialVersionUID = 1L;
    private final MouseWheelListener mouseWheelListener;
    private final JTextArea tipArea;

    public JScrollableToolTip(final int rows, final int columns) {
        setLayout(new BorderLayout());
        mouseWheelListener = createMouseWheelListener();
        tipArea = new JTextArea(rows, columns);
        tipArea.setLineWrap(true);
        tipArea.setWrapStyleWord(true);
        tipArea.setEditable(false);
        LookAndFeel.installColorsAndFont(tipArea, "ToolTip.background",
                "ToolTip.foreground", "ToolTip.font");
        JScrollPane scrollpane = new JScrollPane(tipArea);
        scrollpane.setBorder(null);
        scrollpane.getViewport().setOpaque(false);
        add(scrollpane);
    }

    private MouseWheelListener createMouseWheelListener() {
        return new MouseWheelListener() {

            @Override
            public void mouseWheelMoved(final MouseWheelEvent e) {
                JComponent component = getComponent();
                if (component != null) {
                    tipArea.dispatchEvent(new MouseWheelEvent(tipArea, e.getID(),
                            e.getWhen(), e.getModifiers(), 0, 0, e.getClickCount(),
                            e.isPopupTrigger(), e.getScrollType(), e.getScrollAmount(),
                            e.getWheelRotation()));
                }
            }
        };
    }

    @Override
    public void addNotify() {
        super.addNotify();
        JComponent component = getComponent();
        if (component != null) {
            component.addMouseWheelListener(mouseWheelListener);
        }
    }

    @Override
    public void removeNotify() {
        JComponent component = getComponent();
        if (component != null) {
            component.removeMouseWheelListener(mouseWheelListener);
        }
        super.removeNotify();
    }

    @Override
    public void setComponent(JComponent c) {
        JComponent component = getComponent();
        if (component != null) {
            component.removeMouseWheelListener(mouseWheelListener);
        }
        super.setComponent(c);
    }

    @Override
    public void setTipText(final String tipText) {
        String oldValue = this.tipArea.getText();
        tipArea.setText(tipText);
        tipArea.setCaretPosition(0);
        firePropertyChange("tiptext", oldValue, tipText);
    }

    @Override
    public Dimension getPreferredSize() {
        return getComponent(0).getPreferredSize();
    }

    @Override
    public String getTipText() {
        return tipArea == null ? "" : tipArea.getText();
    }

    @Override
    protected String paramString() {
        String tipTextString = (tipArea.getText() != null ? tipArea.getText() : "");
        return super.paramString() + ",tipText=" + tipTextString;
    }

    //for testing only:
    public static void main(final String args[]) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                JFrame f = new JFrame("JScrollableToolTip");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(300, 200);
                f.setLocationRelativeTo(null);
                ToolTipManager.sharedInstance().setInitialDelay(500);
                ToolTipManager.sharedInstance().setDismissDelay(10000);
                ToolTipManager.sharedInstance().mousePressed(null);
                JTable table = new JTable(50, 4) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public JToolTip createToolTip() {
                        JScrollableToolTip tip = new JScrollableToolTip(3, 20);
                        tip.setComponent(this);
                        return tip;
                    }
                };
                table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public Component getTableCellRendererComponent(JTable table, Object value,
                            boolean isSelected, boolean hasFocus, int row, int column) {
                        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                        setToolTipText("Row " + row + " Column " + column
                                + "\nUsed to display a 'Tip' for a Component. "
                                + "Typically components provide api to automate the process of "
                                + "using ToolTips. For example, any Swing component can use the "
                                + "JComponent  setToolTipText method to specify the text for a standard tooltip.");

                        return this;
                    }
                });
                f.add(new JScrollPane(table));
                f.setVisible(true);
            }
        });
    }
}
like image 28
mKorbel Avatar answered Nov 04 '22 06:11

mKorbel