Solved

moving intrnal frames

Posted on 2004-10-11
13
152 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 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