Question about user moving and resizing components

I need to put up a form which contains a number of subpanels that the user can move and resize, as well as moving and resizing the components within (which are dummies, they don't actually do anything). I then need to save the position and size information for storage, as that data will be used to display the form in a different application.

I searched through the forums and found some code that allows putting up panels and moving/resizing them (although it is difficult to grab the edges correctly) but when I tried to extend it to move/resize the components as well, everything stopped working.

I've seen some references to using glass panes for this sort of thing but I can't seem to figure out how that helps.

Any pointers would be greatly appreciated.

Thanks!

[803 byte] By [betseyba] at [2007-11-26 18:12:27]
# 1
Maybe I don't understand what you are doing:Are you using layout managers?Can't you just call getSize()?Define: "everything stopped working"
zadoka at 2007-7-9 5:45:17 > top of Java-index,Java Essentials,New To Java...
# 2

You may want to check out JToolbar and JInternalFrame, but for general dragging and resizing, you can use my Draggable and Resizeable classes

You are welcome to use and modify these classes, but please don't change the package or take credit for it as your own work

Draggable.java

==============

package tjacobs.ui;

import java.awt.Component;

import java.awt.Cursor;

import java.awt.Dimension;

import java.awt.Point;

import java.awt.Window;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionListener;

/**

* tjacobs.ui.Draggable

* Makes a component draggable. Does not work if there's a layout manager

* messing with things

* <code>

* usage:

* Component c = ...

* new Draggable(c);

* parent.add(c);

* </code>

*/

public class Draggable extends MouseAdapter implements MouseMotionListener {

Point mLastPoint;

Component mDraggable;

public Draggable(Component w) {

w.addMouseMotionListener(this);

w.addMouseListener(this);

mDraggable = w;

}

public Draggable(Window w, Component drag) {

drag.addMouseMotionListener(this);

drag.addMouseListener(this);

mDraggable = w;

}

public void mousePressed(MouseEvent me) {

if (mDraggable.getCursor().equals(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR))) {

mLastPoint = me.getPoint();

}

else {

mLastPoint = null;

}

}

private void setCursorType(Point p) {

Point loc = mDraggable.getLocation();

Dimension size = mDraggable.getSize();

if ((p.y + WindowUtilities.RESIZE_MARGIN_SIZE < loc.y + size.height) && (p.x + WindowUtilities.RESIZE_MARGIN_SIZE < p.x + size.width)) {

mDraggable.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

}

}

public void mouseReleased(MouseEvent me) {

mLastPoint = null;

}

public void mouseMoved(MouseEvent me) {

setCursorType(me.getPoint());

}

public void mouseDragged(MouseEvent me) {

int x, y;

if (mLastPoint != null) {

x = mDraggable.getX() + (me.getX() - (int)mLastPoint.getX());

y = mDraggable.getY() + (me.getY() - (int)mLastPoint.getY());

mDraggable.setLocation(x, y);

}

}

}

Resizeable.java

=============

package tjacobs.ui;

import java.awt.Component;

import java.awt.Cursor;

import java.awt.Dimension;

import java.awt.Point;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionListener;

//For testing

///*

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

//*/

/**

* tjacobs.ui.Resizeable

* Makes a component resizeable. Does not work if there's a layout manager

* messing with things

* <code>

* usage:

* Component c = ...

* new Resizeable(c);

* parent.add(c);

* </code>

*/

public class Resizeable extends MouseAdapter implements MouseMotionListener {

int fix_pt_x = -1;

int fix_pt_y = -1;

Component mResizeable;

private Double mAspectRatio;

private Cursor mDefaultCursor;

private Dimension mMinSize, mMaxSize;

public Resizeable(Component c) {

mResizeable = c;

c.addMouseListener(this);

c.addMouseMotionListener(this);

}

public boolean isRespectingMinSize() {

return mMinSize == null;

}

public boolean isRespectingMaxSize() {

return mMinSize == null;

}

public void setRespectingMaxSize(boolean b) {

mMaxSize = b ? mResizeable.getMaximumSize() : null;

}

public void setRespectingMinSize(boolean b) {

mMinSize = b ? mResizeable.getMinimumSize() : null;

}

public void setMaintainAspect(double x, double y) {

setMaintainAspect(new Double(Math.atan2(y, x)));

}

public void setMaintainAspect(Double angle) {

mAspectRatio = angle;

}

public void mouseEntered(MouseEvent me) {

setCursorType(me.getPoint());

}

private void setCursorType(Point p) {

//boolean n = p.y <= WindowUtilities.RESIZE_MARGIN_SIZE;

//boolean w = p.x <= WindowUtilities.RESIZE_MARGIN_SIZE;

boolean s = p.y + WindowUtilities.RESIZE_MARGIN_SIZE >= mResizeable.getHeight();

boolean e = p.x + WindowUtilities.RESIZE_MARGIN_SIZE >= mResizeable.getWidth();

if (e) {

if (s) {

mResizeable.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));

return;

}

mResizeable.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));

return;

}

if(s) {

mResizeable.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));

return;

}

else if (mDefaultCursor != null) {

mResizeable.setCursor(mDefaultCursor);

}

}

public void setDefaultMouseCursor(Cursor c) {

mDefaultCursor = c;

}

public void mouseExited(MouseEvent me) {

//if (mOldcursor != null)

//((Component)me.getSource()).setCursor(mOldcursor);

//mOldcursor = null;

}

public void mousePressed(MouseEvent me) {

Cursor c = mResizeable.getCursor();

Point loc = mResizeable.getLocation();

if (c.equals(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR))) {

fix_pt_x = loc.x;

fix_pt_y = loc.y;

return;

}

if (c.equals(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR))) {

fix_pt_x = loc.x;

fix_pt_y = -1;

return;

}

if (c.equals(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR))) {

fix_pt_x = -1;

fix_pt_y = loc.y;

return;

}

}

public void mouseReleased(MouseEvent me) {

fix_pt_x = -1;

fix_pt_y = -1;

}

public void mouseMoved(MouseEvent me) {

setCursorType(me.getPoint());

}

public void mouseDragged(MouseEvent me) {

Point p = me.getPoint();

if (fix_pt_x == -1 && fix_pt_y == -1) return;

int width = fix_pt_x == -1 ? mResizeable.getWidth() : p.x;

int height = fix_pt_y == -1 ? mResizeable.getHeight() : p.y;

if (mMinSize != null) {

width = width < mMinSize.width ? mMinSize.width : width;

height = height < mMinSize.height ? mMinSize.height : height;

}

if (mMaxSize != null) {

width = width > mMaxSize.width ? mMaxSize.width : width;

height = height > mMaxSize.height ? mMaxSize.height : height;

}

if (mAspectRatio == null) {

mResizeable.setSize(new Dimension(width > 1 ? width : 1, height > 1 ? height : 1));

}

else {

//do something

double distance = Math.sqrt(width * width + height * height);

width = (int) (distance * Math.cos(mAspectRatio));

height = (int) (distance * Math.sin(mAspectRatio));

if (mMinSize != null) {

width = width < mMinSize.width ? mMinSize.width : width;

height = height < mMinSize.height ? mMinSize.height : height;

}

if (mMaxSize != null) {

width = width > mMaxSize.width ? mMaxSize.width : width;

height = height > mMaxSize.height ? mMaxSize.height : height;

}

mResizeable.setSize(new Dimension(width > 1 ? width : 1, height > 1 ? height : 1));

}

}

//For Testing

/*

public static void main(String args[]) {

JPanel jp = new JPanel();

JButton jb = new JButton("hello");

JButton jb2 = new JButton("hello2");

jp.setPreferredSize(new Dimension(300,300));

jp.setSize(300,300);

jp.setLayout(null);

jp.add(jb);

jp.add(jb2);

jb.setSize(20,20);

jb.setLocation(2,2);

jb2.setSize(30,20);

jb2.setLocation(30,10);

Resizeable _r = new Resizeable(jb);

_r.setDefaultMouseCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

Resizeable r = new Resizeable(jb2);

r.setMaintainAspect(30, 20);

r.setDefaultMouseCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

jb2.setMinimumSize(new Dimension(15,10));

jb2.setMaximumSize(new Dimension(210,140));

r.setRespectingMaxSize(true);

r.setRespectingMinSize(true);

JFrame jf = new JFrame();

jf.add(jp);

jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);

jf.setLocation(100,100);

jf.pack();

jf.setVisible(true);

}

//*/

}

WindowUtilities.java

=================

package tjacobs.ui;

public class WindowUtilities {

public static final int RESIZE_MARGIN_SIZE = 4;

private WindowUtilities();

}

tjacobs01a at 2007-7-9 5:45:17 > top of Java-index,Java Essentials,New To Java...
# 3

> Maybe I don't understand what you are doing:

>

> Are you using layout managers?

>

> Can't you just call getSize()?

>

> Define: "everything stopped working"

I don't see how getSize would help move and resize things. I'm using the null layout manager to facilitate moving stuff around arbitrarily.

What I'm doing is that the user specifies that they want certain things on the form. I put up the form with an arbitrary layout, which they can then tweak to suit their preferences. I then save it for use in another application.

"everything stopped working" = I could no longer resize or move the panels, and the components didn't respond as I expected either. I'm sure it was a case of not really understanding the code I snagged, because I thought that my extensions made sense.

betseyba at 2007-7-9 5:45:17 > top of Java-index,Java Essentials,New To Java...
# 4
Thank you, "tjacobs"...I will definitely give that a try!Your code looks much more understandable than the other stuff I had; hopefully I will be able to figure it out so I can learn from it.
betseyba at 2007-7-9 5:45:17 > top of Java-index,Java Essentials,New To Java...
# 5

Hi

Im trying to use the above Resizeable class but I can't understand where WindowUtilties class comes from? Ive googled it but there must be like 50 different versions, none of which can I find that have the RESIZE_MARGIN_SIZE variable. Can someone tell me where its from?

Thanks in advance!

JOzzieTa at 2007-7-9 5:45:17 > top of Java-index,Java Essentials,New To Java...