user1841257
user1841257

Reputation: 51

Force HeavyWeight Tooltip with shaped JPanel

I'm working on an application with a custom shape, and having some issue with tooltips of my buttons. I isolated the problem in a simple example, which illustrate exactly my situation.

You can see, tooltip of the middle button is well display because is larger than the root panel, but the one on the left button doesn't work and is hidden by my custom shape.

Here is my exemple :

import java.awt.Point;
import java.awt.Polygon;
import java.awt.Shape;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ToolTipManager;

public class ButtonDemo extends JPanel {
protected JButton b1, b2, b3;

public ButtonDemo() {
    b1 = new JButton("Disable middle button");
    b1.setVerticalTextPosition(AbstractButton.CENTER);
    b1.setHorizontalTextPosition(AbstractButton.LEADING);

    b2 = new JButton("Middle button");
    b2.setVerticalTextPosition(AbstractButton.BOTTOM);
    b2.setHorizontalTextPosition(AbstractButton.CENTER);

    b3 = new JButton("Enable middle button");
    b3.setEnabled(false);

    b1.setToolTipText("Click this button to disable the middle button.");
    b2.setToolTipText("This middle button does nothing when you click it. This middle button does nothing when you click it. This middle button does nothing when you click it.");
    b3.setToolTipText("Click this button to enable the middle button.");

    add(b1);
    add(b2);
    add(b3);
}

private static void createAndShowGUI() {

    JFrame frame = new JFrame("ButtonDemo");

    frame.setUndecorated(true);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create and set up the content pane.
    ButtonDemo newContentPane = new ButtonDemo();
    newContentPane.setOpaque(true); // content panes must be opaque
    frame.setContentPane(newContentPane);

    // Display the window.
    frame.pack();
    frame.setSize(1024, 768);
    frame.setVisible(true);
    frame.setLocation(0, 0);

    // Shape
    final Point[] points = new Point[]{
            //
            new Point(0, 0),
            //
            new Point(0, frame.getHeight()),
            //
            new Point(frame.getWidth() - 400, frame.getHeight()),
            //
            new Point(frame.getWidth() - 400, 25),
            //
            new Point(frame.getWidth(), 25),
            //
            new Point(frame.getWidth(), 0),
            //
            new Point(0, 0)};

    int[] xpoints = new int[points.length];
    int[] ypoints = new int[points.length];

    for (int i = 0; i < points.length; i++) {
        xpoints[i] = (int) points[i].getX();
        ypoints[i] = (int) points[i].getY();
    }

    Shape formeFenetre = new Polygon(xpoints, ypoints, points.length);

    frame.setShape(formeFenetre);

}
public static void main(final String[] args) {

    ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            createAndShowGUI();
        }
    });
}
}

I thought "ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);" was exactly what I was looking for, but doesn't work quite well with my JRE. By the way, I'm using Java 1.7.0_09, but should work with any JRE in 1.7.

Hope someone know what to to ! Thanks for reading.

Upvotes: 5

Views: 728

Answers (1)

Related Questions