?
Solved

moving intrnal frames

Posted on 2004-10-11
13
Medium Priority
?
162 Views
Last Modified: 2010-03-31
hi experts,

I tried the following code which keeps internal frames being resized or moved from appearing outside the dektop..
the resizing worked but not the moving.. can anyone see something wrong?

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.beans.*;
 
// A DesktopManager that keeps its frames inside the desktop.
public class SampleDesktopMgr extends DefaultDesktopManager {
 
  // We'll tag internal frames that are being resized using a client
  // property with the name RESIZING.  Used in setBoundsForFrame().
  protected static final String RESIZING = "RESIZING";
 
  public void beginResizingFrame(JComponent f, int dir) {
    f.putClientProperty(RESIZING, Boolean.TRUE);
  }
 
  public void endResizingFrame(JComponent f) {
    f.putClientProperty(RESIZING, Boolean.FALSE);
  }
 
  // This is called any time a frame is moved or resized.  This
  // implementation keeps the frame from leaving the desktop.
  public void setBoundsForFrame(JComponent f, int x, int y, int w, int h) {
    if (f instanceof JInternalFrame == false) {
      super.setBoundsForFrame(f, x, y, w, h); // only deal w/internal frames
    }
    else {
      JInternalFrame frame = (JInternalFrame)f;
 
      // Figure out if we are being resized (otherwise it's just a move)
      boolean resizing = false;
      Object r = frame.getClientProperty(RESIZING);
      if (r != null && r instanceof Boolean) {
        resizing = ((Boolean)r).booleanValue();
      }
 
      JDesktopPane desk = frame.getDesktopPane();
      Dimension d = desk.getSize();
 
      // Nothing all that fancy below, just figuring out how to adjust
      // to keep the frame on the desktop.
      if (x < 0) {              // too far left?
        if (resizing)
          w += x;               // don't get wider!
        x=0;                    // flush against the left side
      }
      else {
        if (x+w>d.width) {      // too far right?
         if (resizing)
           w = d.width-x;       // don't get wider!
         else
           x = d.width-w;       // flush against the right side
        }
      }
      if (y < 0) {              // too high?
        if (resizing)
          h += y;               // don't get taller!
        y=0;                    // flush against the top
      }
      else {
        if (y+h > d.height) {   // too low?
          if (resizing)
            h = d.height - y;   // don't get taller!
          else
            y = d.height-h;     // flush against the bottom
        }
      }
 
      // Set 'em the way we like 'em
      super.setBoundsForFrame(f, x, y, w, h);
    }
  }
}
0
Comment
Question by:Samooramad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12275141
Could you give a small example on how you use this?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12275266
When do you call beginReszing() and when endResizing() ?
0
 

Author Comment

by:Samooramad
ID: 12275292
I just set the desktop manager by doing
desktop.setDesktopManager(new SampleDesktopMgr());

and then begingResizing and endResizing are called automatically, kinda like mouseClicked, MouseEntered
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 37

Expert Comment

by:zzynx
ID: 12275454
When the internal frame is moved the function setBoundsForFrame() is never called.
I think you'll have to overwrite the resizeFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) too.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12275469
Forget the last line of my previous comment.
Nevertheless the first line is true: When the internal frame is moved the function setBoundsForFrame() is never called.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12275479
This is the solution: you have to overwrite

      public void dragFrame(JComponent f, int x, int y)
0
 
LVL 37

Accepted Solution

by:
zzynx earned 1600 total points
ID: 12275523
So, you even don't need that beginResizingFrame() and endResizingFrame() funcitons.
This works:

public class SampleDesktopMgr extends DefaultDesktopManager {
 
  // This is called any time a frame is resized.
  // This implementation keeps the frame from leaving the desktop.
  public void setBoundsForFrame(JComponent f, int x, int y, int w, int h) {
    if (f instanceof JInternalFrame == false) {
      super.setBoundsForFrame(f, x, y, w, h); // only deal w/internal frames
    }
    else {
      JInternalFrame frame = (JInternalFrame)f;
      Dimension d = frame.getDesktopPane().getSize();
 
      // Nothing all that fancy below, just figuring out how to adjust
      // to keep the frame on the desktop.
      if (x < 0)
        w += x;
      else if (x+w>d.width)
        w = d.width-x;       // don't get wider!

      if (y < 0)
        h += y;               // don't get taller!
      else if (y+h > d.height)
        h = d.height - y;   // don't get taller!
 
      // Set 'em the way we like 'em
      super.setBoundsForFrame(f, x, y, w, h);
    }
  }

  // This is called any time a frame is moved.
  // This implementation keeps the frame from leaving the desktop.
  public void dragFrame(JComponent f, int x, int y) {
    if (f instanceof JInternalFrame == false) {
      super.dragFrame(f, x, y); // only deal w/internal frames
    }
    else {
      JInternalFrame frame = (JInternalFrame)f;
      Dimension d = frame.getDesktopPane().getSize();
 
      // Nothing all that fancy below, just figuring out how to adjust
      // to keep the frame on the desktop.
      if (x < 0)               // too far left?
        x=0;                   // flush against the left side
      else if (x+f.getWidth()>d.width)     // too far right?
        x = d.width-f.getWidth();       // flush against the right side
     
      if (y < 0)               // too high?
        y=0;                   // flush against the top
      else if (y+f.getHeight() > d.height)  // too low?
        y = d.height-f.getHeight();     // flush against the bottom
 
      // Set 'em the way we like 'em
      super.dragFrame(f, x, y);
    }

  }
0
 

Author Comment

by:Samooramad
ID: 12280534
Im sorry I don't get it.. why didn't it work? and why don't I need beginResize and endResize?
won't that top the resize check?
0
 

Author Comment

by:Samooramad
ID: 12280539
I meant stop :)
0
 

Author Comment

by:Samooramad
ID: 12280557
ok .. Im sure you are right :) I'll just try it and then bug you with my questions :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12284319
>>why didn't it work?
Because, like I said: when the internal frame is moved the function setBoundsForFrame() is never called.
While the function dragFrame() is.
0
 

Author Comment

by:Samooramad
ID: 12286673
yep that did it.. a great help as always :)

thank you
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12286712
Thanks :)
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question