Search code examples
javaswingtooltip

Tooltip behind modal dialog


How to check these kinds of problem. When i checked the menus i have for a desktop application, some shows proper display of tooltip for close button which should always be at the front. But some are displayed at the back of the modal dialog.

Screenshot of the bug: Tooltip_issue

I kind of have the same problems as the one who posted this: https://coderanch.com/t/460688/java/Glasspanes-tooltips

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


class GlassPaneContent extends JPanel {

    GlassPaneContent() {
        setSize(200, 50);
        ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);
        JButton button = new JButton("A button");
        button.setToolTipText("A tooltip");
        add(button);

    }
}

class GlassPane extends JPanel {

    private static final Color BG_COLOR = new Color(0, 0, 0, 96);
    private GlassPaneContent content = new GlassPaneContent();

    public GlassPane() {
        setLayout(null);
        setOpaque(false);
        add(content);
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(BG_COLOR);
        g.fillRect(0, 0, getWidth(), getHeight());
        int x = (getWidth() - content.getWidth()) / 2;
        int y = (getHeight() - content.getHeight()) / 2;
        content.setLocation(x, y);
        super.paintComponent(g);
    }
}

public class MainWindow extends JFrame {

    public MainWindow() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 500);
        GlassPane gp = new GlassPane();
        getRootPane().setGlassPane(gp);
        gp.setVisible(true);
    }

    public static void main(String[] args) {
        new MainWindow().setVisible(true);
    }
}

We're using JAVA Swing. Kindly comment below if ever i need to post the codes. Thank you!


Solution

  • Try out this one:

    public class MainWindow extends JFrame {
    
        public MainWindow() {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(500, 500);
            GlassPane gp = new GlassPane();
            setContentPane(gp);
            this.setVisible(true);
        }
    
        public static void main(String[] args) {
            new MainWindow();
        }
    }