2010/05/10

Custom Decorated TitleBar JFrame





Code

class ResizeWindowListener extends MouseAdapter {
  private Rectangle startSide = null;
  private final JFrame frame;
  public ResizeWindowListener(JFrame frame) {
    this.frame = frame;
  }
  public void mousePressed(MouseEvent e) {
    startSide = frame.getBounds();
  }
  public void mouseDragged(MouseEvent e) {
    if(startSide==null) return;
    Component c = e.getComponent();
    if(c==topleft) {
      startSide.y += e.getY();
      startSide.height -= e.getY();
      startSide.x += e.getX();
      startSide.width -= e.getX();
    }else if(c==top) {
      startSide.y += e.getY();
      startSide.height -= e.getY();
    }else if(c==topright) {
      startSide.y += e.getY();
      startSide.height -= e.getY();
      startSide.width += e.getX();
    }else if(c==left) {
      startSide.x += e.getX();
      startSide.width -= e.getX();
    }else if(c==right) {
      startSide.width += e.getX();
    }else if(c==bottomleft) {
      startSide.height += e.getY();
      startSide.x += e.getX();
      startSide.width -= e.getX();
    }else if(c==bottom) {
      startSide.height += e.getY();
    }else if(c==bottomright) {
      startSide.height += e.getY();
      startSide.width += e.getX();
    }
    frame.setBounds(startSide);
  }
}


References

5 comments:

bobndrew said...

Your examples are great, but the "Custom Decorated TitleBar JFrame" could not be moved to a second monitor on WinXP.

aterai said...

Hi, bobndrew. Thank you for your bug report.
However, I don't have a dual moniter environment...
How about this code instead?
@Override public void mouseDragged(MouseEvent me) {
if(window==null) window = SwingUtilities.windowForComponent(me.getComponent());
Point eventLocationOnScreen = me.getLocationOnScreen();
window.setLocation(eventLocationOnScreen.x - start.getX(),
eventLocationOnScreen.y - start.getY());
//loc = window.getLocation(loc);
//int x = loc.x - start.getX() + me.getX();
//int y = loc.y - start.getY() + me.getY();
//window.setLocation(x, y);
}

bobndrew said...

Hi aterai,
I started the MainPanel from within eclipse and from a WinXP Console and everything worked as expected.

I used the code from the downloadable 'src.zip'.

Is the webstart example updated to the new code? Because it shows the old behaviour: dragging outside the first monitor is not possible. Maybe the Webstart environment is causing this behaviour?!

aterai said...

bobndrew, thanks for the response.
> Maybe the Webstart environment is causing this behaviour?!
Maybe so. The Swing Tutorial has the same problem.
http://download.oracle.com/javase/tutorial/uiswing/examples/components/index.html#FrameDemo2
- FrameDemo2: "Look and feel decorated" not possible.
- FrameDemo2: "Window system decorated" possible.

Another example: http://terai.xrea.jp/swing/customdecoratedframe/example2.jnlp
- jnlp: all permissions in the setting()
- jars: signed

aterai said...

EDIT:
It seems to work OK for me, Java 1.7:
Java Web Start 10.0.0.147
JRE 1.7.0-b147 Java HotSpot(TM) Client VM