1

I am new in J2SE, I am using Java Swing to create J2SE application. The JFrame can pick and drag at any where on screen but I want to restrict frame to do not reach out from desktop screen.

How to enforce the drag restriction?

Note: The code must work for all type of O.S.(Window, Linux, Mac)

I found help from net but that code is not supportable for all type of O.S.

Dheeraj Upadhyay
  • 336
  • 2
  • 12
  • What have you got so far? Don't just "copy code from the net", write your own. BTW, what book are you using? If it says "J2SE", update. J2SE became out of date early 2006 – Stultuske Apr 07 '16 at 08:40
  • 2
    Why do you want to restrict the frame from being moved out of the desktop? To me that seems like an OS feature which might not be supported on every platform (depending on what you mean by "**all type of O.S.**") – Thomas Apr 07 '16 at 08:43
  • 3
    And, what if the user doesn't want that? What if they have multiple monitors or virtual desktops? – MadProgrammer Apr 07 '16 at 09:42
  • Sir, Help me to solve out this. I wait here for solution not for suggestion. In my case their is no virtual desk . – Dheeraj Upadhyay Apr 07 '16 at 09:55
  • 3
    *"I wait here for solution not for suggestion"* lol – ortis Apr 07 '16 at 10:32
  • 3
    *"but I want to restrict my frame"* If it's *your* app. being run on *my* machine and taking up ***my*** screen space, what makes you think it is **your** frame (any longer)? BTW - following that scenario, what does all this restrictions of movement of *my* frame on **my** screen actually do for ***me,*** the end user? Why would **I** want that ..feature? – Andrew Thompson Apr 07 '16 at 10:41

1 Answers1

1

You will need to know size of the screen , and size of JFrame.

Toolkit.getDefaultToolkit().getScreenSize();

Use thread to scan for location of JFrame on screen. CONSTANTLY / Daemon

Find the location/Position of JFrame in the window

If user gets to corner of the screen, do some math.Set your JFrame ot latest valid location or to center of the screen.

How to set JFrame to appear centered, regardless of monitor resolution?

Bud for the love of sanity , dont do this. Just let user move it where he wants it.If any software woud do this to me i woud consider it borderline Malware sincei use multimonitor setup, and this is not acceptable for me.

Community
  • 1
  • 1
Tomas Bisciak
  • 2,801
  • 5
  • 33
  • 57