Solved

moving intrnal frames

Posted on 2004-10-11
13
147 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now