Question

Program ScreenShot

enter image description here

I'm making a animation. When I press on a button it triggers a animation which makes a JPanel float in from the right side. The JPanel gets DeAnimated(JPanel exit animation) when the mouse leaves the JPanel, but the problem is that I have a JButton on the animated JPanel. So the panel does not only disappear when i move the mouse out of the animated JPanel but also when I move the mouse on the button (which is a component of the panel), which means that when I want to click on the button it disappears, because the MouseExit is fired when the mouse leaves the JPanel.

Look at the picture above where I marked certain areas.

  1. Is the JPanel which is getting animated and disappears when i move over the button.
  2. Is the button which causes the JPanel to be DeAnimated.
  3. Is the area which should trigger the DeAnimation of the 1st JPanel.

I have been trying to fix this for a long time now, but I can't get it working. I would really appreciate it if you guys could help me. Thanks!

Was it helpful?

Solution

"but the problem is that I have a JButton on the animated JPanel. "

Don't put the button on the same panel then. Learn to use layout managers. With your image, I would use nested JPanels with BorderLayouts

The right side

JButton button = new JButton();
JPanel animatedPanel = new JPanel();

Add them to another JPanel

JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.add(button, BorderLayout.NORTH);
rightPanel.add(animatedPanel, BorderLayout.CENTER);

 /**      result       **/

       +-----+
       |     |
       |     |
       +-----+
       |     |
       |     |
       |     |
       |     |
       |     |
       +-----+

Then create another JPanel to hold the BigPanel and the RightPanel

JPanel mainPanel = new JPanel(new BorderLayout());
JPanel leftBigPanel = new JPanel();
mainPanel.add(leftBigPanel, BorderLayout.CENTER);
mainPanel.add(rightPanel);

          /**       result       **/

         *--------------------+-----+
         |                    |     |
         |                    |     |
         |                    +-----+
         |                    |     |
         |                    |     |
         |                    |     |
         |                    |     |
         |                    |     |
         +--------------------+-----+

The button no longer overlaps the right animated panel

  • Note : @override the getPreferredSize() in the JPanels for your desired dimension.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top