How can I set tooltip inserts in Java?

I created a tooltip with HTML formatted text, this works fine, but I have no space between the border and the text. How to set an insert or an empty template?

+2
source share
3 answers

Found this article on how to change Java ToolTip properties (background, border, etc.) . It focuses on colors and border style, but maybe you can also use this approach for margins (inserts).

+3
source

This works for me:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JToolTip;
import javax.swing.border.EmptyBorder;

public class tooltipinsets {
  public static void main(String[] args) {
    JFrame window = new JFrame();
    JLabel lbl = new JLabel("Test") {
      @Override
      public JToolTip createToolTip() {
        return createCustomToolTip();
      }
    };
    window.add(lbl);
    lbl.setToolTipText("<html><b><i>This is the tooltip</i></b></html>");
    window.pack();
    window.setLocationRelativeTo(null);
    window.setVisible(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public static JToolTip createCustomToolTip() {
    JToolTip tip = new JToolTip();
    tip.setBorder(new EmptyBorder(10, 10, 10, 10));
    return tip;
  }
}
+1
source

All Articles