JToolTip เป็น subclass ของ JComponent สร้าง tooltip ให้ java component ใดๆ ได้ด้วยเมธอด setToolTipText() ใช้ตั้งค่า tooltip ให้ component เมธอดสำคัญของ JToolTip ได้แก่ getAccessibleContext(), getComponent(), paramString() และ updateUI() เราสามารถเปลี่ยนสี background และ foreground ของ JToolTip ได้โดยใช้เมธอด put() ของคลาส UIManager ส่ง argument ToolTip.background และ ToolTip.foreground
ตัวอย่าง
import java.awt.*;
import javax.swing.*;
public class JTooltipColorTest extends JFrame {
private JLabel label;
public JTooltipColorTest() {
setTitle("JTooltipColor Test");
setLayout(new FlowLayout());
label = new JLabel("Welcome to TutorialsPoint");
label.setToolTipText("Simply Easy Learning");
UIManager.put("ToolTip.background", Color.white); // เปลี่ยน background color ของ JToolTip
UIManager.put("ToolTip.foreground", Color.green); // เปลี่ยน foreground color ของ JToolTip
add(label);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new JTooltipColorTest();
}
}
Output
