Moving button in front of other panels
<p>I'm coding a simple card-based game where the player has a hand of cards along the bottom of the screen. When they click on a card, the card moves across the window onto a 'discard pile' location.</p>

<p>I've got the movement animation working to my satisfaction, but the problem is the card button disappears when it moves across other panels. The hand of cards is in the PAGE_END section of a borderlayout for the content pane, and the discard pile is at the top of the screen - so the card has to move across the CENTER section to get to its destination. But it goes <em>behind</em> the CENTER panel, whereas I want it to go in front.</p>

<p>Is this possible? I've tried looking into LayeredPanes but that seems to require all of my components being directly inside one container, which they aren't (because I have another panel inside the bottom section of my content panel). </p>

<p>I've written up the simplest version of the problem that I can, using two classes.</p>

<p>The main class:</p>

<pre><code>package moveButton;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class MoveButton extends JFrame implements ActionListener {

    public JPanel view;
    public MovingButton mover;
    public static MoveButton instance;

    public static void main(String[] args) {
        instance = new MoveButton();
    }

    public MoveButton() {

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500,500);
        this.setVisible(true);

        view = new JPanel();
        view.setLayout(new BorderLayout());

        JPanel buttonPanel = new JPanel();
        view.add(buttonPanel, BorderLayout.PAGE_END);

        mover = new MovingButton("Move");
        mover.addActionListener(this);
        buttonPanel.add(mover);

        this.setContentPane(view);

        this.repaint();
        this.revalidate();



    }

    @Override
    public void actionPerformed(ActionEvent e) {
        MoveButton.instance.mover.timer.start();
    }

}
</code></pre>

<p>The button class:</p>

<pre><code>package moveButton;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.Timer;

public class MovingButton extends JButton implements ActionListener {

    public Timer timer;
    public static final int X_INC = 5;
    public static final int Y_INC = -5;
    public int xPos;
    public int yPos;



public MovingButton(String text) {
        super(text);
        timer = new Timer(30, this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        this.move();
        MoveButton.instance.repaint();

        if(xPos == 100 || yPos == 100) {
            timer.stop();
        }

    }

    public void move() {
        xPos = xPos + X_INC;
        yPos = yPos + Y_INC;
    }

    public void paint(Graphics g) {
        g.translate(xPos, yPos);
        super.paint(g);
    }

}
</code></pre>