Java Swing UIManager showToolTip(Component contents, int x, int y, final int dismissDelay, int initialDelay)

Here you can find the source of showToolTip(Component contents, int x, int y, final int dismissDelay, int initialDelay)

Description

show Tool Tip

License

LGPL

Declaration

public static Popup showToolTip(Component contents, int x, int y, final int dismissDelay, int initialDelay) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.awt.Color;
import java.awt.Component;

import java.awt.Font;

import java.awt.Paint;
import java.awt.Point;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.font.TextAttribute;

import java.util.HashMap;

import java.util.Map;

import javax.swing.JLabel;

import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.Timer;
import javax.swing.UIManager;

public class Main {

    public static Popup showToolTip(String text, Component target, int milliseconds) {
        JLabel label = new JLabel(text);
        label.setOpaque(true);/*from   ww w  .  ja v  a  2 s. com*/
        label.setFont(UIManager.getFont("ToolTip.font"));
        label.setForeground(UIManager.getColor("ToolTip.foreground"));
        label.setBackground(UIManager.getColor("ToolTip.background"));
        label.setBorder(UIManager.getBorder("ToolTip.border"));
        Point p = target.getLocationOnScreen();
        return showToolTip(label, p.x, p.y + target.getHeight(), milliseconds, 20);
    }

    public static Popup showToolTip(Component contents, int x, int y, final int dismissDelay, int initialDelay) {
        final Popup popup = PopupFactory.getSharedInstance().getPopup(null, contents, x, y);
        final Timer timer = new Timer(initialDelay, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Timer hideTimer = new Timer(dismissDelay, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        popup.hide();
                    }
                });
                hideTimer.setRepeats(false);
                popup.show();
                hideTimer.start();
            }
        });
        timer.setRepeats(false);
        timer.start();
        return popup;
    }

    @SuppressWarnings("unchecked")
    public static Font getFont(String name, int style, int size, Color color) {
        Font font = new Font(name, style, size);
        Map<TextAttribute, Object> map = (Map<TextAttribute, Object>) font.getAttributes();
        map.put(TextAttribute.FOREGROUND, color);
        return font.deriveFont(map);
    }

    public static Font getFont(Paint bg, Paint fg, String family, int size, Float weight, boolean obliquePosture,
            boolean underline, boolean strikethrough, Boolean extendWidth, Boolean superscript) {
        Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>();
        if (bg != null)
            attributes.put(TextAttribute.BACKGROUND, bg);
        if (fg != null)
            attributes.put(TextAttribute.FOREGROUND, fg);
        if (family != null)
            attributes.put(TextAttribute.FAMILY, family);
        if (size > 0)
            attributes.put(TextAttribute.SIZE, size);
        if (obliquePosture)
            attributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
        if (strikethrough)
            attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
        if (underline)
            attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
        if (weight != null)
            attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
        if (extendWidth != null) {
            if (extendWidth) {
                attributes.put(TextAttribute.WIDTH, TextAttribute.WIDTH_EXTENDED);
            } else {
                attributes.put(TextAttribute.WIDTH, TextAttribute.WIDTH_CONDENSED);
            }
        }
        if (superscript != null) {
            if (superscript) {
                attributes.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER);
            } else {
                attributes.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB);
            }
        }
        return new Font(attributes);
    }
}

Related

  1. setTheme()
  2. setupComponent(JComponent comp)
  3. setupUI()
  4. shouldUpdateStyle(PropertyChangeEvent event)
  5. showDirSelect()
  6. systemLF()
  7. traceSubstanceApiUsage(Component comp, String message)
  8. useLNF()
  9. useLnf(String lnfClassName)