Change properties of Java ToolTip (background, border, etc.)

Sun UIRB commitee requires that ToolTips should have a yellow background color (#fff7c8), with a black or dark grey (#4c4f53) border, and the ToolTip should be showed for 15 seconds. 

To change such properties, there is a very simple way by adding a few lines of code (see below) where the ToolTip is referenced:

-----------------------------

 UIManager.put("ToolTip.background", new ColorUIResource(255, 247, 200)); // The color is #fff7c8.
 Border border = BorderFactory.createLineBorder(new Color(76,79,83)); // The color is #4c4f53.
 UIManager.put("ToolTip.border", border);
 ToolTipManager.sharedInstance().setDismissDelay(15000);// 15 seconds    

 setToolTipText(message); // The message is a String variable containing text to display.

-----------------------------

If more properties of the ToolTip are required to be changed, or the changed ToolTip needs to be referenced multiple times in the program, it is better to create a Look&Feel class as follows:

----------------------------------

/\*\*
\* DemoFrame.java
\* This test frame is to demonstrate changing of tooltip background
\*/

import javax.swing.\*;
import javax.swing.text.\*;
import java.awt.\*;
import java.awt.event.\*;

import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.ColorUIResource;
import javax.swing.border.Border;

class ToolTipLookAndFeel extends MetalLookAndFeel
{
    protected void initSystemColorDefaults(UIDefaults table)
    {       
        super.initSystemColorDefaults(table);       
        table.put("info", new ColorUIResource(255, 247, 200));   
    }

    protected void initComponentDefaults(UIDefaults table) {
        super.initComponentDefaults(table);

    Border border = BorderFactory.createLineBorder(new Color(76,79,83));
    table.put("ToolTip.border", border);
    }
}


public class DemoFrame extends JFrame
{
    public DemoFrame()
    {
        super("Demo");

        try {                             
            UIManager.setLookAndFeel(new ToolTipLookAndFeel());
        } catch(Exception ex) {       
            System.err.println("ToolTipLookAndFeel exception!");
            System.err.println(ex.getMessage());
        }

        getContentPane().setLayout(new FlowLayout());

        JButton btn = new JButton("<html>Mouse Over me <br> for ToolTip </html>");               
       
        btn.setToolTipText("<html>I have changed <br> the color of <br> background and border. <br> It works! </html>");

        getContentPane().add(btn);

        JLabel label = new JLabel("What about me");
        label.setToolTipText("Me Too...");

        getContentPane().add(label);
    }

    public static void main(String[] arg)
    {
        DemoFrame m = new DemoFrame();

        m.setVisible(true);
        m.setSize(new Dimension(300, 150));
        m.validate();
    }

----------------------------------

Save the above code into a single java file, compile it, and run the DemoFrame class.

Note: If the following line of

    UIManager.setLookAndFeel(new ToolTipLookAndFeel());

is changed to

    UIManager.setLookAndFeel("ToolTipLookAndFeel");

the code of ToolTipLookAndFeel class must be moved to a separated file instead of being a internal class in the DemoFrame.java file. And the ToolTipLookAndFeel.java must be compiled before DemoFrame.java file.
Otherwise, the ToolTipLookAndFeel class cannot be really reference, the exception will be catched without any error or warning message while compiling. It is a little tricky.





 

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Ye Julia Li

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today