@Override public JToolTip createToolTip() { //Customizing tool tip for better visibility JToolTip tip = super.createToolTip(); tip.setForeground(Color.BLACK); tip.setBorder(BorderFactory.createLineBorder(Color.BLACK)); return tip; } };
public class Main { public static void main(String args[]) { JFrame frame = new JFrame("JToolTip Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton b1 = new JButton("Button 1") { public JToolTip createToolTip() { JToolTip tip = super.createToolTip(); tip.setForeground(Color.YELLOW); tip.setBackground(Color.RED); tip.setFont(new Font("Arial", Font.BOLD,36)); return tip; } }; b1.setToolTipText("HELLO"); frame.add(b1, BorderLayout.NORTH); frame.setSize(300, 150); frame.setVisible(true); } }
public class CButton extends JButton { public CButton(String text, Icon icon) { super(text, icon); } @Override public JToolTip createToolTip() { JToolTip toolTip = super.createToolTip(); toolTip.setForeground(Color.BLACK); toolTip.setBackground(Color.WHITE); toolTip.setFont(new Font("Arial", Font.PLAIN, 12)); return toolTip; } };
/** * Creates a new tooltip. If overlapping is true then the tooltip will take on the foreground/background color and * font of the specified component (if the component isspecifiedd) * * @param c the component the tooltip describes * @param overlapping whether the tooltip is for a normal or overlapping tooltip * @return the new tooltip object */ public JToolTip createToolTip(JComponent c, boolean overlapping) { JToolTip tt = new JToolTip(); if (c != null) { tt.setComponent(c); if (overlapping) { if (c.getBackground() != null) { Color bg = c.getBackground(); if (bg.getAlpha() != 255) { bg = new Color(bg.getRed(), bg.getGreen(), bg.getBlue()); } tt.setBackground(bg); } if (c.getForeground() != null) { tt.setForeground(c.getForeground()); } if (c.getFont() != null) { tt.setFont(c.getFont()); } } } return tt; }