Solved

moving intrnal frames

Posted on 2004-10-11
13
150 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
  • 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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 400 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

831 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