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.

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).

Not as I imagine (Like html + jquery pre loaders) but works. – ShirazITCo May 11 at 19:20.

You need a to use a JLayeredPane for moving components in front of each other. Here is a tutorial: How to use Layered Panes.

1 @jzd I forgot about it. But I guess it would not completely solve the problem of modality if the panel is smaller then the tree. Thus I think a dialog fits better here.

– Boro May 11 at 16:53 @Boro, I agree, a dialog seems to make the most sense, but the question specifically says "I don't want JDialog". – jzd May 11 at 17:17 Yea I agree. Now it does, but it didn't when I was answering :) – Boro May 11 at 17:21 @Boro, yes it did not say that when I was first answering either.

– jzd May 11 at 17:28.

It's not really clear how your code is organized. However, it sounds like what you might want is a modal dialog. Here's a link to a similar discussion with a number of referenced resources.

How to make a JFrame Modal in Swing java.

This really serves better as a comment. – Крыѝa May 11 at 16:45 Thanks for the tip. I'll do that next time.

– Geoff May 11 at 18:28.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions