wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes setting boundary for draggable jcomponent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "setting boundary for draggable jcomponent" Watch "setting boundary for draggable jcomponent" New topic
Author

setting boundary for draggable jcomponent

Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 503

I write program in which 1 JFrame(in my program it named as myframe) containing small n number of JPanels(actually Jcomponents ) in JFrames ContentPane which can be drag any where in JFrame, it also avoid overlapping on each other.
I face problem that's when i put JFrame on extended state i.e. Its width & height same as system resolution then some times, it exceed the visible area on different monitor...
I solve it by use JFrame height & width less than 10. but when i drag small JPanels to the boundary, it go beyond visible region.
I tried if-else conditions at mouseDragged event & mouseReleased events. But i get confused & mess it. Basic thing is i try to avoid JComponent to go all possible directions [when (x,y) are as less than 1 or greater than system resolution)]!
In my code mouse events in following place

When i try to set boundaries i put if -else In mouseDragged, as follows

This not helping me properly for all possible conditions so i write few more if-else conditions in mouseReleased event as follows:-

I think, i failed to write easy calculations!
I need help. any clue or suggestion?


Millions saw the apple fall, but Newton asked why.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2332
    
  50
I face problem that's when i put JFrame on extended state i.e. Its width & height same as system resolution then some times, it exceed the visible area on different monitor...

You could check the size of the JFrame is not greater than the screen size, which you can get from
and if it is then set the frame size to the screen size.

I solve it by use JFrame height & width less than 10.

How does that work, or have I not understood the problem?

but when i drag small JPanels to the boundary, it go beyond visible region.

Are you saying you want to stop any part of a JPanel from being dragged outside of the JFrame?

BTW why aren't you using JDesktopPane which is designed to handle internal windows which can be dragged around etc.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 503

Thanks Tony Docherty,

I solve it by use JFrame height & width less than 10.

You got me there...I make mistake in writing....Right sentence is

I solve it by use JFrame height & width less than 10 with compare to system resolution.

I use following code:-

I try to keep simple & very basic way my code.
In bold font i write my main problem...any clue?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2332
    
  50
Using 'resolution' to mean 'screen size' won't stop anything from working but is damned confusing. The screen resolution is the number of discreet elements for a given length ie dots per inch and is not the overall size of the screen.

Basic thing is i try to avoid JComponent to go all possible directions [when (x,y) are as less than 1 or greater than system resolution)]!

Do you really mean this, what if your JFrame is not at full screen size and what about the frame's insets such as the title bar and border?
There are various ways of restricting the movement such as stopping the mouse from moving the panel outside the visible area to always snapping the panel back to the nearest visible area no matter where it is dropped.

Sorry but I can't even bring myself to try to decipher all those nested if-else statements to work out what they are for. Before writing any code you need to make sure you know exactly what it is you want to achieve and adding more and more complex if-else statements throughout the mouse handling code tends to suggest you haven't got a clear idea of the problem or solution. Can you write out exactly what it is you are trying to do and how ideally it would work from the user's perspective.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I think, i failed to write easy calculations!

all those if-else's ?

I told you in one of your other threads all you need to do is create a 'safe' rectangle,
then check if that rectangle contains the x,y of the dragged component.

if the frame/contentPane size is 800,600 and the panel size is 25,25 then the safe rectangle is
0,0,775,575
so, if the panel's x,y, is anywhere inside that rectangle, no part of the dragged panel is outside the frame.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 503

Thanks Michael Dunn,
Due to your help i can pretend as programmer.

if the frame/contentPane size is 800,600 and the panel size is 25,25 then the safe rectangle is
0,0,775,575
so, if the panel's x,y, is anywhere inside that rectangle, no part of the dragged panel is outside the frame.

This the only one solution?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> This the only one solution?

the best solution to any problem is one
that both the writer and any reader can understand.

the 2nd best solution is daylight.

the 3rd best solution is one that makes
sense to the writer but no-one else.

the 4th best solution is one that works (perhaps)
but makes no sense to anyone.

your posted code appears to fit into the 4th category.

re-read my earlier post.
write the example on paper.
try to understand it's simplicity.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 503

Thanks Michael Dunn,
re-read my earlier post.
write the example on paper.
try to understand it's simplicity.

Surely...I will do that. It take some time...i am slower in coding(right).
I appreciate by answer which explain very clearly where i am.
It help me to do improvement.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setting boundary for draggable jcomponent