[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

moving intrnal frames

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
Samooramad
Asked:
Samooramad
  • 8
  • 5
1 Solution
 
zzynxSoftware engineerCommented:
Could you give a small example on how you use this?
0
 
zzynxSoftware engineerCommented:
When do you call beginReszing() and when endResizing() ?
0
 
SamooramadAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
zzynxSoftware engineerCommented:
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
 
zzynxSoftware engineerCommented:
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
 
zzynxSoftware engineerCommented:
This is the solution: you have to overwrite

      public void dragFrame(JComponent f, int x, int y)
0
 
zzynxSoftware engineerCommented:
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
 
SamooramadAuthor Commented:
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
 
SamooramadAuthor Commented:
I meant stop :)
0
 
SamooramadAuthor Commented:
ok .. Im sure you are right :) I'll just try it and then bug you with my questions :)
0
 
zzynxSoftware engineerCommented:
>>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
 
SamooramadAuthor Commented:
yep that did it.. a great help as always :)

thank you
0
 
zzynxSoftware engineerCommented:
Thanks :)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now