Bring JPanel to front of other objects in java (SW

2020-03-19 02:53发布

问题:

I want to make a loading message when an app processes, so I used a JPanel over a JTree. But when the user clicks on the JPanel, the JTree will be selected and the JPanel will go to the back. After hiding the JPanel, it never shows again. I don't know why, but it seems it never go in front of the JTree.

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

EDIT: Also I must mention that I don't want a JDialog. I want to use the JPanel on top of any element to show a loading message until a process finishes.

回答1:

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


回答2:

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

Here is a tutorial: How to use Layered Panes



回答3:

Disabled Glass Pane might help you out.



回答4:

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



回答5:

Use JXLayer or JIDE Overlayable.



回答6:

Jpanel main = new JPanel();
Jpanel a = new JPanel();
JPanel b = new Jpanel();

main.add(a);
main.add(b);

at this point the object:

a -> 0 ( index)
b -> 1 (index)

main.getComponentCount() = 2

main.setComponentZorder(b,0);


a -> 1
b -> 0;

b OVER
a DOWN


回答7:

For those who have no problem using a JDialog, this is a sure way to get it to show up if you're having issues. Just make sure to control it properly if the dialog is modal, when disposing, setting focus etc.

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