Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

swing

jpanel

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.

like image 499
ShirazITCo Avatar asked May 11 '11 16:05

ShirazITCo


People also ask

Can you put a JPanel on top of another JPanel?

So for what you want to do, you can do this: JPanel panel = new JPanel(); panel. setLayout(new GridLayout(2, 1)); //the first number is the number of rows, the second is the number of columns JPanel topPanel = new JPanel(); JPanel bottomPanel = new JPanel(); panel.


1 Answers

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);
like image 101
Boro Avatar answered Sep 21 '22 22:09

Boro