0

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

HSV_DO
  • 99
  • 1
  • 1
  • 9
  • I think question is more appropriate for code review. – Alex Weitz Mar 09 '16 at 09:36
  • @HSV_DO You can consider using custom painting which could be easier than directly dealing with multiple panels: [Foreground & background image](http://stackoverflow.com/questions/35711891/how-to-create-a-background-and-foreground-image-which-overlaps/35711894#35711894) – user3437460 Mar 09 '16 at 09:49
  • These guys made some [draggable components](http://www.codeproject.com/Articles/116088/Draggable-Components-in-Java-Swing). – matt Mar 09 '16 at 10:39
  • 1
    A lot will depend on the layout manager, but you could use [`Container#setComponentZOrder`](https://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#setComponentZOrder(java.awt.Component,%20int)) or [`OverlayLayout`](https://docs.oracle.com/javase/8/docs/api/javax/swing/OverlayLayout.html) or [`JLayeredPane`](https://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html) – MadProgrammer Mar 09 '16 at 10:52
  • @user3437460 I guess You can't move a directly drawn part anymore or am I wrong at this point? – HSV_DO Mar 09 '16 at 10:54
  • @matt Thanks that looks good, I'll check how they have done their program – HSV_DO Mar 09 '16 at 10:55
  • @HSV_DO Do You mean can we drag and drop using custom painting? – user3437460 Mar 09 '16 at 10:56
  • @user3437460 I reckon that it would not be possible to drag and drop a custom painting but I'm not sure about that at all.. – HSV_DO Mar 09 '16 at 11:03

0 Answers0