Change text and background color Tooltip

Asked

Viewed 1,778 times

4

How do I change the background and text color of a Tooltip? I already did a search and found here a chunk of code but I can’t change it. Colors always stay the default Netbeans. Any suggestions?

I put the following code in the constructor of my class:

     UIManager.put("ToolTip.foreground", Color.BLACK);
     UIManager.put("ToolTip.background", Color.GREEN);
     ToolTipManager.sharedInstance().setInitialDelay(0);
     ToolTipManager.sharedInstance().setDismissDelay(1500);
     jFormattedNIPC.setToolTipText("teste");

Updating:

Use HTML in the field properties in the Tooltiptext section. The color has changed well, but the background looks like this:

inserir a descrição da imagem aqui

Background color does not fill the 'balloon' completely. Code used: <html><p style='background-color:blue; color:white;'>teste</p></html>

  • 2

    Some elements of swing accept HTML in the text. For example in a label you can put something like this in the text: "<html>Palavra em <strong>negrito</strong>.</html>". Tried to put that in your tooltip?

  • I didn’t use any type of HTML in my code. I’ll try this one. But at the color level what would be the code? I’m just not really into HTML. Thanks in advance! IT WORKS BOLD !! : ) I need the colors for background and text :)

  • Change the color of the text already managed, I miss the tooltip background.

1 answer

6


Solution 1

Some swing elements accept HTML in the text. For example in a label you can put something like this in the text: "<html>Palavra em <strong>negrito</strong>.</html>".

In your case you can do so:

"<html><body style=\"background-color:#d8d8ff;\"><center><br>TESTETETESTE</center></body></ht‌​ml>";

However, as the author of the question has pointed out, this solution does not solve the problem perfectly. It does not leave the background completely filled with the color.

Solution 2

You can create a custom Tooltip for the component:

class MyCustomToolTip extends JToolTip {
    public MyCustomToolTip(JComponent component) {
       super();
       setComponent(component);
       setBackground(Color.black);
       setForeground(Color.red);
    }
}

So at the time of initializing a component, in this example a jTextField, do so:

jTextField1 = new JTextField("teste"){
    @Override
    public JToolTip createToolTip() {
        return (new MyCustomToolTip(this));
    }
};

Note: if you use Netbeans you can change the tooltip of a component as follows:

  1. Put the class MyCustomToolTip after the method initComponents();

  2. Right-click on the component;

  3. Go on Properties;

  4. Click on the Code tab;

  5. In the Custom Creation Code property click on ... and enter the creation code (in case a JTextField):

    new JTextField("teste"){ // Pode ser new JLabel, depende do tipo do componente
        @Override
        public JToolTip createToolTip() {
            return (new MyCustomToolTip(this));
        }
    };
    
  6. OK.

  • That way it doesn’t work. HTML code has to be placed in the field properties in the tooltip part.

  • <html><font color="red">Your text will turn red. </font>. </html> This code sets the color of the red text. Your code must have some error or it doesn’t work :S

  • Exactly. After the style you have to put a =. I had put a :.

  • try like this "<html><body style=\"background-color:#d8d8ff;\"><center><br>TESTETETESTE</center></body></html>"; (just to help, I don’t know if it’s the same @Earendul)

  • I don’t know anything about html. @jsantos1991 your code didn’t work :S Earendul this works but has a small problem. I’ll edit the question and show you what happens.

  • @Hugomachado I don’t fish much either, but that code works for me ;) but it’s good that the Earendul code works

  • Okay, I’ll edit the answer with one more solution...

  • I edited the answer. I tested it here and solution 2 worked better than the first.

  • I always have a problem with that that should be basic but I can’t solve :S I tried that code yesterday but I couldn’t. Instead of creating a new Jtextfield I want to use in a field I already have in my design. How do I?

  • You use Netbeans?

  • Yes I’m with netbeans. If the @jsantos1991 solution worked for both of you maybe I’m not putting the code right. You can put the code line in the Solutions as well ?

  • @Hugomachado changed the answer with jsantos1991 code. I also put the steps to change in Netbeans.

  • It’s not working for me. Can you put the @jsantos1991 line of code in these solutions? and all in one line just to see if I put in HTML ? : ) EDIT: Already there ! ?

  • @Hugomachado - I put there the code of jsantos1991. In the case of solution 2 remembered to put the class MyCustomToolTip next to the code? You can put after the method initComponents().

  • Yes I put the class later in initComponents and put the code you told me in Custom Code in the properties and it doesn’t work for me :(

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.