Bring JPanel to front of other objects in java (SWING)

So here you have at least two solutions. Either go with what @Geoff and @sthupahsmaht are suggesting. BTW also possible is to use JOptionPane which automatically creates a dialog for you.

The other option would be to use a GlassPane from a frame.

Or yet another option is to use JLayeredPane as @jzd suggests.

EDIT: Example showing how to use GlassPane to capture user selections. Try following steps:

1.Left clicking on the glass pane visible at start. See the output.

2.Right click it. This hides the glass pane.

3.Left clicking on the content pane. See the output.

4.Right click it. Go to point 1. Enjoy.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class OverPanel extends JPanel
{   
    private static void createAndShowGUI()
    {
        final JFrame f = new JFrame();
        f.setPreferredSize(new Dimension(400, 300));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        JPanel glassPanel = new JPanel();
        glassPanel.setBackground(Color.RED);        
        glassPanel.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                super.mousePressed(e);
                System.out.println("f.getGlassPane() mousePressed");
                if(e.getButton() == MouseEvent.BUTTON3)
                    f.getGlassPane().setVisible(false);
            }
        });
        f.setGlassPane(glassPanel);     

        f.getContentPane().setBackground(Color.GREEN);
        f.getContentPane().addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                super.mousePressed(e);
                System.out.println("f.getContentPane() mousePressed");
                if(e.getButton() == MouseEvent.BUTTON3)
                    f.getGlassPane().setVisible(true);
            }
        });
        f.getGlassPane().setVisible(true);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

EDIT2: If you want to have an effect of a dialog, you can achieve it by incorporating appropriately this code into my example.

        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        panel.setBackground(Color.YELLOW);
        panel.add(new JLabel("I am message Label"));
        panel.add(new JButton("CLOSE"));
        JPanel glassPanel = new JPanel(new GridBagLayout());
        glassPanel.setOpaque(false);
        glassPanel.add(panel);

You need a to use a JLayeredPane for moving components in front of each other.

Here is a tutorial: How to use Layered Panes


Disabled Glass Pane might help you out.

Tags:

Java

Swing

Jpanel