7

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.

SovietFrontier
  • 2,047
  • 1
  • 15
  • 33
ShirazITCo
  • 1,041
  • 6
  • 23
  • 38
  • 2
    You should probably be using a `JDialog` instead of a `JPanel`, since you can set its _modality_ – mre May 11 '11 at 16:42
  • 1
    _Also i must mention that i dont want JDialog. i want use the jpanel top of any element to show a loading until a process finish._ What? `JDialog` will do this all for free. Can you please justify using a `JPanel` instead of a `JDialog`? – mre May 11 '11 at 16:50

7 Answers7

8

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);
Community
  • 1
  • 1
Boro
  • 7,913
  • 4
  • 43
  • 85
5

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

Here is a tutorial: How to use Layered Panes

Nate W.
  • 9,141
  • 6
  • 43
  • 65
jzd
  • 23,473
  • 9
  • 54
  • 76
  • +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 '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 '11 at 17:17
  • Yea I agree. Now it does, but it didn't when I was answering :) – Boro May 11 '11 at 17:21
  • @Boro, yes it did not say that when I was first answering either. – jzd May 11 '11 at 17:28
2

Disabled Glass Pane might help you out.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

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

Community
  • 1
  • 1
Geoff
  • 773
  • 1
  • 6
  • 13
0
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
senia
  • 37,745
  • 4
  • 88
  • 129
0

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);
SovietFrontier
  • 2,047
  • 1
  • 15
  • 33
0

Use JXLayer or JIDE Overlayable.

Nate W.
  • 9,141
  • 6
  • 43
  • 65
Jarek Przygódzki
  • 4,284
  • 2
  • 31
  • 41