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

前端 未结 7 1896
轮回少年
轮回少年 2021-01-12 10:37

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 <

相关标签:
7条回答
  • 2021-01-12 10:54
    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
    
    0 讨论(0)
  • 2021-01-12 10:57

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

    Here is a tutorial: How to use Layered Panes

    0 讨论(0)
  • 2021-01-12 11:01

    Disabled Glass Pane might help you out.

    0 讨论(0)
  • 2021-01-12 11:04

    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);
    
    0 讨论(0)
  • 2021-01-12 11:10

    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

    0 讨论(0)
  • 2021-01-12 11:14

    Use JXLayer or JIDE Overlayable.

    0 讨论(0)
提交回复
热议问题