Customize the tooltip font, color , background and foreground color in Java


To customize the tooltip font, color and background, use UIManager.

UIManager.put("ToolTip.background", Color.ORANGE);
UIManager.put("ToolTip.foreground", Color.BLACK);
UIManager.put("ToolTip.font", new Font("Arial", Font.BOLD, 14));

Above, we have set the font with −

Tooltip.font

We have set the foreground and background color with the following above −

ToolTip.foreground
ToolTip.background

The following is an example to customize tooltip −

Example

package my;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
public class SwingDemo {
   public static void main(String[] args) throws Exception {
      JFrame.setDefaultLookAndFeelDecorated(true);
      JFrame frame = new JFrame("Register!");
      JLabel label1, label2, label3;
      frame.setLayout(new GridLayout(2, 2));
      label1 = new JLabel("Id", SwingConstants.CENTER);
      label1.setToolTipText("<html>" + "Add the id given to you" + "<br>" + "in the
      beginning!" + "</html>");
      UIManager.put("ToolTip.background", Color.ORANGE);
      UIManager.put("ToolTip.foreground", Color.BLACK);
      UIManager.put("ToolTip.font", new Font("Arial", Font.BOLD, 14));
      label2 = new JLabel("Rank", SwingConstants.CENTER);
      label2.setToolTipText("Enter rank");
      label3 = new JLabel("Password", SwingConstants.CENTER);
      label3.setToolTipText("Enter Password");
      JTextField emailId = new JTextField(20);
      JTextField rank = new JTextField(20);
      JPasswordField passwd = new JPasswordField();
      passwd.setEchoChar('*');
      frame.add(label1);
      frame.add(label2);
      frame.add(label3);
      frame.add(emailId);
      frame.add(rank);
      frame.add(passwd);
      Point center =
      GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
      int width = 500;
      int height = 200;
      frame.setBounds(center.x - width / 2, center.y - height / 2, width, height);
      frame.setVisible(true);
}
}

Output

raja
Published on 07-Jun-2019 07:33:44
Advertisements