• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

setting boundary for draggable jcomponent

 
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic