Components in layer 1 appearing above those of layer 0 when hovering the mouse above them

50 Views Asked by At

In this JFrame, I added a JLayeredPane of two panels.menu,a JPanel,is added at layer 0. mainPanel is the panel on the right side ,that is added at layer 1. Why when hovering the mouse over the button of the menu ,the radiobuttons on the mainPanel (layer1) appear above the menu(layer 0). Any solutions?

Note that Menu.moreDetails is a jlabel with an imageIcon,the menu Panel opens/closes when clicking on this jlabel.

enter image description here

enter image description here

public class HomePage {

  Dimension DimMax;
  JFrame homeFrame;
  CardLayout card ;
  JPanel mainPanel ;
 
static JPanel homePanel;

public void createFrame() {

    DimMax = Toolkit.getDefaultToolkit().getScreenSize();
    card = new CardLayout();
    homeFrame = new JFrame();
    homePanel = new JPanel();
    mainPanel= new JPanel(card);
    Menu menu = new Menu(mainPanel, card);
    JLayeredPane layer = new JLayeredPane();

    mainPanel.setBackground(Color.white);
    menu.setBackground(new Color(200, 200, 200));

    menu.setMinimumSize(new Dimension(24, DimMax.height));
    //menu.setOpaque(true);
    mainPanel.setOpaque(false);

    mainPanel.setBounds(24, 0, DimMax.width - 24, DimMax.height);
    menu.setBounds(0, 0, 24, DimMax.height);

    layer.add(mainPanel, 1);
    layer.add(menu, 0, 0);
    
    homeFrame.add(layer);
    layer.addMouseListener(new MouseAdapter() {
        boolean open = false;

        @Override
        public void mousePressed(MouseEvent me) {
            if (me.getSource() == Menu.moreDetails) {
                if (open) {
                    menu.setMaximumSize(new Dimension(24, DimMax.height));
                    menu.setMinimumSize(new Dimension(24, DimMax.height));
                    menu.setPreferredSize(new Dimension(24, DimMax.height));
                    menu.setBounds(0, 0, 24, DimMax.height);

                    menu.revalidate();
                    open = false;
                } else {

                    menu.setMaximumSize(new Dimension(120, DimMax.height));
                    menu.setMinimumSize(new Dimension(120, DimMax.height));
                    menu.setPreferredSize(new Dimension(120, DimMax.height));
                    menu.setBounds(0, 0, 120, DimMax.height);

                    menu.revalidate();
                    HomePage.homeFrame.revalidate();
                    HomePage.homePanel.revalidate();
                    open = true;
                }

            }
        }

    });

   
    homeFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    homeFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    homeFrame.setUndecorated(true);
    homeFrame.setVisible(true);

}

}

0

There are 0 best solutions below