File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Dragging behavior of Array of JPanels & Jbutton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Dragging behavior of Array of JPanels & Jbutton" Watch "Dragging behavior of Array of JPanels & Jbutton" New topic
Author

Dragging behavior of Array of JPanels & Jbutton

Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 485
As per HowToAskQuestionsOnJavaRanch...i try my best to simplify my question...
Question introduction:- I am trying to create simple swing program which has drag-able array of jpanels & jbuttons...(i create just 3 each)...


Scenario:-
In simpleExample class
1 JFrame
1 Array of JPanels
1 Array of JLabels
1 Array of JButtons
1 Dragger class

Code as follows

Problem generation:-
There are 3 jpanels creates due to Array index 3 (0,1,2).
When you try to drag Jpanel which has low value of array index(0 or 1) over Jpanel which has high value of array index(1 or 2), then that low array indexed jpanel move from above to jpanel which has high value of array index.(in simple words, 0th panels can drag 'above' 1st or 2nd jpanel)
But when you try to opposite 2nd jpanel drag on 1st or 0th jpanel then it goes behind it same for if 1st jpanel try to drag on 0th it goes behind it.
First i thought this happened due to jpanel is container...so i create jbutton also..but same behavior by jbutton also.
Now Question:- Why that so...how to avoid it..i am trying to same behavior to all jpanels & jbuttons which created by array..no one should get backside to other when try to drag here & there.
I hope i try to simplest way to explain my question...
I know due to unknowing of few lines of code i can not do myself...but hope someone surely help ...
sorry of posting long code with long description...


Millions saw the apple fall, but Newton asked why.
If you understand, say "understand". If you don't understand, say "don't understand". But if you understand and say "don't understand". How do I understand that you understand? Understand!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

One way: setComponentZOrder(...)

Another way I've seen discussed in various threads: remove the component being dragged, add it to the glassPane for the duration of dragging, then return it to its normal place in the hierarchy.


luck, db
There are no new questions, but there may be new answers.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 485
Thanks Darryl Burke,
....glassPane...

&
One way: setComponentZOrder(...)

by this one word...i realized that lot to learn in java basics...
Is this topic comes in basics or advanced java? this question is not major...
I try both ways...it might be take few days to understand & implement in my program..(due to slow learning/understanding...)
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 485
As perjava doc

Where components overlap, the component with the lower z-order paints over the component with the higher z-order.

&
As per question-answer in coderanch

Craig Wood said as:-
The idea about using zOrder was to change the draggingComponents zOrder to zero in mousePressed as it is selected, before you start dragging it. This will keep it on top of any other children while you drag it.

&
As per stackoverflow
i got basic code( 2 lines which i am searching)

By all 3 links i tried this in my code...
first i declare 2 integer in class simpleExample
int mycomp_order;
private int myZorder=0;
Then in mousePressed event m....i write as follows

When i drag jpanel 1 over jpanel 0 it move from above...(previously not, it only move above on jpanel 2)..but now it works...but...

i got exception as follows:-

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding container's parent to itself

i am trying to find out why that so...? Any clue?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Post the full stack trace along with the line of code that threw the Exception and a few preceding lines.

If that doesn't serve to identify the problem, you may need to create and post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 485
Post the full stack trace along with the line of code that threw the Exception and a few preceding lines.

here is that...
Actually i am facing 2 different problems...
1 for overlapping & 1 for default positioning of jpanels or jcomponents...(but this default positioning is not major for me..so i neglect it)


If that doesn't serve to identify the problem, you may need to create and post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

The program posted in first post in this question is itself smallest code written by me...actually this question is regarding my questions
1
2
3
4

If you read all then it may be huge confusion regarding why,what,when,how etc...

So instead of more than 500 line code i posted only 139 line code which only java swing...
How can i do more SSCCE..i may be need example with reference or related to my code...
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Read the API for setComponentZOrder(...) more carefully. This isn't right:
The first argument to the method can't be the same object on which the method is invoked -- and that wouldn't make any sense either.

IMO all you need is
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 485
Read the API for setComponentZOrder(...) more carefully.

Thanks for suggestion & that line which i missed to read & understand...
Now i have only 1 problem remain with my code which related to swing is avoid overlapping...
But i will ask separately...after try...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dragging behavior of Array of JPanels & Jbutton
 
Similar Threads
3 qestions regarding jdbc with swing!
Array of jpanels within JScrollPane
Focus Help
3 questions regarding JPanel.
Problem in jdbc with swing (old wine in new bottle)