So, you have at least two solutions. Or go with what @Geoff and @sthupahsmaht offer. BTW is also possible to use JOptionPane, which automatically creates a dialogue for you.
Another option is to use GlassPane from the frame.
Or another option is to use JLayeredPane, as @jzd suggests.
EDIT: An example showing how to use GlassPane to select a user. Try the following steps:
1.Click on the glass panel visible at startup. See Conclusion.
2. Right-click. This hides the glass panel.
3. Left-click in the content pane. See Conclusion.
4. Right-click. Go to step 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 a dialogue effect, you can achieve this by including the appropriate code in 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);
Boro source share