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

I want to make a loading message while processing the application, so I used JPanel on top of JTree . But when the user clicks on JPanel , JTree will be selected and JPanel will jump back. After hiding JPanel , he never shows again. I don't know why, but it seems like it never goes before JTree .

I need a way to put JPanel in front of everything. How can i do this?

EDIT: Also I have to point out that I don't want JDialog . I want to use JPanel on top of any element to show a loading message before the process is complete.

+6
source share
7 answers

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); 
+8
source

You need to use JLayeredPane to move components in front of each other.

Here is a tutorial: How to use layered panels

+4
source
+2
source

It's not entirely clear how your code is organized. However, it looks like you might need a modal dialog. Here is a link to a similar discussion with several reference resources.

How to make JFrame Modal in Swing java

+1
source

Use JXLayer or JIDE Overlayable .

0
source
 Jpanel main = new JPanel(); Jpanel a = new JPanel(); JPanel b = new Jpanel(); main.add(a); main.add(b); 

at this moment the object:

 a -> 0 ( index) b -> 1 (index) main.getComponentCount() = 2 main.setComponentZorder(b,0); a -> 1 b -> 0; b OVER a DOWN 
0
source

For those who have no problems using JDialog , this is the right way to show it if you have any problems. Just make sure that you manage it correctly if the dialog box is modal, when disposing, adjusting focus, etc.

 JDialog dialog = new JDialog(); dialog.setAlwaysOnTop(true); 
0
source

Source: https://habr.com/ru/post/887857/


All Articles