2

I have 3 image so I want, if I drag any image that should appear above all the other images. Which technique can use? please any idea???? By the way I am using "ImageIcon".

Thanks in advance.

user600966
  • 55
  • 5
  • 9

2 Answers2

3

If what you want to do is click on an image, lift it above the others and then drag it, consider placing them in ImageIcons and these in JLabels, and displaying them in a JLayeredPane, or a JPanel that is held in a JLayeredPane. You can then lift the clicked label onto the JLayeredPane.DRAG_LAYER while dragging, and then drop it down into the DEFAULT_LAYER (or on a JPanel that's on the DEFAULT_LAYER) when done. For an example of this, please see my code in a related question: dragging a jlabel around the screen

If I'm totally off base, sorry, but please correct my incorrect assumptions.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    I have tried so many methods and find them all to be too much code, or don't implement properly. The problem I faced was that all of my visual elements are split over different classes so I was getting z order conflicts.. You suggestion fixed a major headache. I created a JLayeredPane inside the ContentPane and put the background as an imageIcon inside the contentPane but under the JLayeredPanel. THANK YOU +1 – gcoulby Nov 10 '13 at 19:29
1

By the way I am using "ImageIcon".

I assume this means you are adding in image to a JLabel and are then adding the JLabel to a JPanel.

so I want, if I drag any image that should appear above all the other images

You need to add a MouseListener to the JLabel. When you click on the label then you can reset its Z-Order to 0. This will cause the label to be painted last and so it will be on top of all other labels on the panel.

So the basic code in the MouseListener would be:

panel.setComponentZOrder(label, 0);
camickr
  • 321,443
  • 19
  • 166
  • 288