I'm trying to create a small drag and drop program in Java. It is supposed to be a "fist contact to programing", like taking some components of code with the mouse and move them into a loop (while, do while, for). This should be shown grafically. I have a JFrame, with a JPanel in it (absolut layout for my panel) and I add two (later there will be some panels more but at the moment unimportant) more JPanels to my panel (colored them to see which one is in front). So I found out that the first JPanel which is added to my mainpanel is in the foreground. I wrote a working method to put a JPanel into the foreground but I want to know if there is another, maybe "easyier" way to move a JPanel to the foreground.
Here is the code of my new class containing my written method:
import javax.swing.JPanel;
public class ForegroundJPanel
extends JPanel {
public void setToForeground(JPanel foregroundPanel, JPanel backgroundPanel, JPanel container)
{
container.remove(backgroundPanel);
container.remove(foregroundPanel);
container.validate();
container.add(foregroundPanel);
container.add(backgroundPanel);
container.validate();
}
}
Thanks in advance.
Edit: To make it more clearly what I mean: See the picture here