Bring JPanel in front of other objects (swing) in Java

I want to load when the app process is running, so I use JPanel on JTree But when the user clicks JPanel, JTree will select and JPanel will go to the back After hiding JPanel, it never shows up (I don't know why! But it never seems to appear on JTree)

I need a way to move JPanel forward How can I do this?

In addition, I must mention that I don't want jdialog I want to use the JPanel top of any element to display the load until the process is complete

Solution

So there are at least two solutions Or select the content suggested by @ Geoff and @ sthupahsmaht BTW can also use joptionpane, which will automatically create a dialog box for you

Another option is to use glasspane in the frame

Or another option is to use jlayeredpane as @jzd advice

Edit: the example shows how to capture user selections using glasspane Try the following steps:

1. Left click on the glass pane visible at the beginning View output

2. Click it This hides the glass pane

3. Left click the content pane View output

4. Click it Go to point 1 Please 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 get the effect of the dialog box, you can do so by properly incorporating 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);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>