3

I've been looking everywhere online but I have found no answers to this little problem. In Windows 7 (and in Vista, I think,) you have a nice rounded silver looking tooltip that looks way better than the old yellow boxed crappy looking one. The "How do I make a Windows 7 Tooltip in Java - Stack Overflow" tip below is how it appears.

enter image description here

So in Java, I want to do exactly that, make the nice new Windows tooltip. The problem is I don't know how to.

Outside of the following simple code is a main method that simply creates an object of this class 'Tooltip'.

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Tooltip extends JFrame {
public Tooltip() {

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }

    setLayout(new FlowLayout());

    JButton button = new JButton("Tooltip Button");
    button.setToolTipText("Tooltip");
    add(button);

    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(250, 160);
    }
}

As you can see in the try/multi catch, I set the Look and Feel to the operating system that the program is currently running on. If I were to run this program on Windows, I would get the crummy yellow tooltip.

So what changes do I have to make to the code so that I will get a nice silver tooltip if the program is running on a Windows operating system?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Just a suggestion, if you are so afraid of typing, then why you wrote this line `catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e)`, why not you simply write `catch(Exception e)`, that will do the same thing, since it will catch no matter what exception arises and will reduce your typing work too :-) – nIcE cOw Jun 23 '12 at 04:56
  • DYM like in the screenshot I edited into the question? – Andrew Thompson Jun 23 '12 at 05:57
  • Thanks @nIcEcOw I was not aware you could do that. I feel old now :D I will probably update the code example above now. – Shrederator Jun 23 '12 at 06:19
  • @AndrewThompson Thank you very much for taking the time to do that. This question has been on my mind for a couple of months now and I hope it will be answered. I've looked everywhere, but found nothing, so I can use all the help I can get :) – Shrederator Jun 23 '12 at 06:22

1 Answers1

3

You might need to use a component based on a JWindow for this, as in my answer to Preview window (like Windows 7 taskbar shows for opened applications).

For the rounded corners, see How to Create Translucent and Shaped Windows.

Shaped window

For the slight gradient (squints), paint the BG using a GradientPaint.

A tool-tip provides a functionality not included in that simple example linked. But it is a starting point. Make sure you don't accidentally drop the Windows 7 style tool-tip to pre Windows 7 users or users of other OS'. In the same way you feel the 'square/yellow BG' tool-tip clashes, they will not find the clashing style to be 'pretty'. Which leads to..


The best way to achieve the effect is through a custom PLAF based on The Synth Look and Feel.

Creating a custom look and feel, or modifying an existing one, can be a daunting task. The javax.swing.plaf.synth package can be used to create a custom look and feel with much less effort. You can create a Synth look and feel either programatically or through the use of an external XML file.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • This preview thingy is COOL :-) – nIcE cOw Jun 23 '12 at 16:44
  • @nIcEcOw You mean in the animated GIF? Thanks. :) – Andrew Thompson Jun 23 '12 at 17:23
  • This small image that comes up, as a tooltip. I remember once I asked you about how to animate an image, you told me about that software you made, on that site of yours, I try my hands on that, learned how to put three images in one. For the rest, you are MOST WELCOME and KEEP SMILING :-) – nIcE cOw Jun 23 '12 at 17:29
  • Thanks @AndrewThompson but I am sure there is another way to properly achieve this, whether it is in the API or some other well known or used external library. I know that it is possible to get these tooltip styles because they are in eclipse everywhere! (eclipse was written in Java of course :D) – Shrederator Jun 23 '12 at 23:33
  • 1
    @Shrederator eclipse is java, but not Swing – kleopatra Jul 08 '12 at 09:05