GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes MouseDragged event with JScrollpane's setViewPosition & scrollRectToVisible! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "MouseDragged event with JScrollpane Watch "MouseDragged event with JScrollpane New topic
Author

MouseDragged event with JScrollpane's setViewPosition & scrollRectToVisible!

Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 499

I write program which as follows
Lots(90,900,9000) of small Draggable JPanels withing JPanel. For drag any small Jpanel i keep lot of area like (10000,70000) so i use JScrollpane with scroll bars.
But when i drag particular panel beyond visible area, i have to release it & move scrollbar where panel set.
So for solve this problem i think when small jpanel i pick visible rect also move along with it till boundries & scrollbars also move automatically, so when ever i drag panel, i can see easily where i am dragging it.

So in mouse Dragged event i write few lines as below:-

x1 & y1 are panel co-ordinates comes after calculation.
So by this i can see panel where i drag but it always on top left corner of screen but when i release the mouse, at that point that particular panel set. So partial problem solved.
Now how to keep panel with mouse cursor with scrollbar automatically moves where i drag it?

I not post whole code because its huge.


Millions saw the apple fall, but Newton asked why.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 499

I try differently also like below:-

But no use by this when i drag pannel only 1centimeter then also it goes to at near to end of right bottom corner.
Any help?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if you're still using the Dragger class in earlier posts, it only needs one extra line of code (indicated)

run the code then drag a label to the right hand edge of the viewport/scrollpane,
it should move the panel to the appropriate position.
is this what you're after?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
In addition to Michael's suggestion, you will also want to add:



for smoother scrolling.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 499

Thanks Michael Dunn & Rob Camick,
By your help i can complete my basic(basic is not right word but this program for show how concept works ) swing program.
Actually i use all techniques which i learn from coderanch in as bellow:-
small panels created by smallpanels.java



It called in show.java


so i can write mouse drag event at 2 places 1. smallpanels.java 2.show.java

At any location i use



When i drag smallpanel towards vertically -down or down rightside then it goes very fast at the end & i can not do opposite to it ie if i want to drag panel (which already below) towards up then
scrollbar not works as they work in basic swing program.!

So finally 2 results
1. Basic swing program finish with success.
2. Advanced & difficult swing program not finish as basic one.
Now i confuse what should i ask for get success? I am trying many R & D's by important 2 lines.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 499

In following line


the ((JComponent)comp.getParent()) should be what? scrollpane or viewport?

Becuase in my advance program if i use it in show.java & smallpanel.java,
i found comp's 1st parent is JPanel, 2 parent is viewport & 3rd parent is JScrollpane, 4th parent is JPanel



Is this matter in above line, i try it for where i found JScrollpane as parent but problem remains....very fast it goes to down & not come up when i try to drag up.
which parent should i use?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> the ((JComponent)comp.getParent()) should be what? scrollpane or viewport?

whatever the labels are added to
i.e. whatever.add(labels[0 to 9000])

[edit]
in your program, whatever the draggable panels are added to
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 499

Thanks once again Michael Dunn,
As per your instruction & help i write in my program as follows
in smallpanel.java in mouse Dragged event, i write

& it works... So as my first query here work done...but in my mind some thoughts come as follows...
now question is when i drag small panel beyond(800,600)(if we consider resolution as 800,600) & drop anywhere then i start dragging it on visible screen then it should be roam freely, but it stick with scrollbar's so always when i dragg it at perticular, scrollbar moves at perticular direction, i mean as it freely roam in first screen(800,600), it should be consider now its second(800,600) or 3rd (800,600)....some thing same manner i saw in pc based games...
Or this thinking is wrong & i should stop here? I need thinking guidance...
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
You only need to set the autoscrolls property on the component added to the scrollpane.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 499

Thanks Rob Camick,
but i set
panel.setAutoscrolls(true);

What else should i do?

I see AOE game's, we can drag back panel here & there then we can drag small component visible area very easily without backpanel moving...when we need we move backpanel by dragging small component till limits, when not then it stop...
I not know how microsoft do that game or other games...
i am trying something like that with small panels...though i am not writing any game or its not necessary or compulsory to me...
 
GeeCON Prague 2014
 
subject: MouseDragged event with JScrollpane's setViewPosition & scrollRectToVisible!