Resizeable JComponents

I have a JPanel with DnD custom JComponents that can be added to this JPanel. I would like to be able to select the JComponents and resize them, perhaps with draggable handles. Does anyone know how I would go about this?
[227 byte] By [JOzzieTa] at [2007-11-26 20:06:39]
# 1

Just implement a MouseListener/MouseMotionAdapter which monitors the relevant events and modifies the relevant component, including using Cursor to adjust the cursor. ie detect the mouse being within a certain distance of the edges, set the cursor accordingly, resize the component when dragging, etc. The code itself is pretty straightforward.

itchyscratchya at 2007-7-9 23:08:14 > top of Java-index,Desktop,Core GUI APIs...
# 2

So the MouseListener would be added to the JPanel to detect edges. When the mouse is close to an edge of a component the cursor will change and when dragging the component that the cursor is over will increase or decrease its dimension?

There has been a resizeable class posted on this forum somewhere but it used a class called WindowUtilties and I have no idea where that is from. I will try the solution that you have suggested. Thanks.

JOzzieTa at 2007-7-9 23:08:15 > top of Java-index,Desktop,Core GUI APIs...
# 3

> There has been a resizeable class posted on this

> forum somewhere but it used a class called

> WindowUtilties and I have no idea where that is from.

> I will try the solution that you have suggested.

> Thanks.

Yes that was my resizeable class. You don't really need the WindowUtilities... you can fill in your own values. But if you'd like it, here's WindowUtilities

package tjacobs.ui;

import java.awt.*;

import java.awt.event.*;

import javax.swing.ImageIcon;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenuItem;

import javax.swing.JPopupMenu;

import javax.swing.Popup;

import javax.swing.SwingUtilities;

public abstract class WindowUtilities {

public static final int RESIZE_MARGIN_SIZE = 4;

private static Window sExitWindow = null;

public static interface CreatePopupWindow {

public JDialog createPopupWindow(Point p);

}

/////

/**

* Returns an point which has been adjusted to take into account of the

* desktop bounds, taskbar and multi-monitor configuration.

*

* This adustment may be cancelled by invoking the application with

* -Djavax.swing.adjustPopupLocationToFit=false

*/

public static Point adjustPopupLocationToFitScreen(Component popup, Component invoker, int xposition, int yposition) {

Point p = new Point(xposition, yposition);

if(//popupPostionFixDisabled == true ||

GraphicsEnvironment.isHeadless())

return p;

Toolkit toolkit = Toolkit.getDefaultToolkit();

Rectangle screenBounds;

Insets screenInsets;

GraphicsConfiguration gc = null;

// Try to find GraphicsConfiguration, that includes mouse

// pointer position

GraphicsEnvironment ge =

GraphicsEnvironment.getLocalGraphicsEnvironment();

GraphicsDevice[] gd = ge.getScreenDevices();

for(int i = 0; i < gd.length; i++) {

if(gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {

GraphicsConfiguration dgc =

gd[i].getDefaultConfiguration();

if(dgc.getBounds().contains(p)) {

gc = dgc;

break;

}

}

}

// If not found and we have invoker, ask invoker about his gc

if(gc == null && invoker != null) {

gc = invoker.getGraphicsConfiguration();

}

if(gc != null) {

// If we have GraphicsConfiguration use it to get

// screen bounds and insets

screenInsets = toolkit.getScreenInsets(gc);

screenBounds = gc.getBounds();

} else {

// If we don't have GraphicsConfiguration use primary screen

// and empty insets

screenInsets = new Insets(0, 0, 0, 0);

screenBounds = new Rectangle(toolkit.getScreenSize());

}

int scrWidth = screenBounds.width -

Math.abs(screenInsets.left+screenInsets.right);

int scrHeight = screenBounds.height -

Math.abs(screenInsets.top+screenInsets.bottom);

Dimension size;

size = popup.getPreferredSize();

if( (p.x + size.width) > screenBounds.x + scrWidth )

p.x = screenBounds.x + scrWidth - size.width;

if( (p.y + size.height) > screenBounds.y + scrHeight)

p.y = screenBounds.y + scrHeight - size.height;

/* Change is made to the desired (X,Y) values, when the

PopupMenu is too tall OR too wide for the screen

*/

if( p.x < screenBounds.x )

p.x = screenBounds.x ;

if( p.y < screenBounds.y )

p.y = screenBounds.y;

return p;

}

/////

public static MouseListener addAsPopup(final CreatePopupWindow cpw, Component owner) {

if (cpw == null || owner == null) {

return null;

}

MouseListener ml = new MouseAdapter() {

CreatePopupWindow create = cpw;

public void mousePressed(MouseEvent ev) {

testPopup(ev);

}

public void mouseReleased(MouseEvent ev) {

testPopup(ev);

}

private void testPopup(MouseEvent ev) {

if (ev.isPopupTrigger()) {

final JDialog pop = create.createPopupWindow(ev.getPoint());

//pop.setUndecorated(true);

if (pop == null) {

return;

}

Window w = SwingUtilities.getWindowAncestor(ev.getComponent());

pop.setLocation(ev.getX() + w.getX(), ev.getY() + w.getY());

pop.pack();

pop.addWindowListener(new WindowAdapter() {

public void windowDeactivated(WindowEvent we) {

pop.dispose();

}

});

pop.setFocusableWindowState(true);

pop.setVisible(true);

pop.requestFocus();

//pop.show();

}

}

};

owner.addMouseListener(ml);

return ml;

}

public static MouseListener addAsPopup(final JPopupMenu popup, Component owner) {

if (popup == null || owner == null) {

return null;

}

//popup.setUndecorated(true);

MouseListener ml = new MouseAdapter() {

JPopupMenu pop = popup;

public void mousePressed(MouseEvent ev) {

testPopup(ev);

}

public void mouseReleased(MouseEvent ev) {

testPopup(ev);

}

private void testPopup(MouseEvent ev) {

if (ev.isPopupTrigger()) {

//pop.setLocation(ev.getPoint());

//pop.pack();

//pop.setVisible(true);

//JPopupMenu pop = new JPopupMenu();

//pop.add(new JMenuItem("Hi"));

pop.show(ev.getComponent(), ev.getX(), ev.getY());

}

}

};

owner.addMouseListener(ml);

return ml;

}

public static MouseListener addAsPopup(final JDialog popup, Component owner) {

if (popup == null || owner == null) {

return null;

}

//popup.setUndecorated(true);

MouseListener ml = new MouseAdapter() {

Window pop = popup;

public void mousePressed(MouseEvent ev) {

testPopup(ev);

}

public void mouseReleased(MouseEvent ev) {

testPopup(ev);

}

private void testPopup(MouseEvent ev) {

if (ev.isPopupTrigger()) {

pop.setLocation(ev.getPoint());

pop.pack();

pop.setVisible(true);

}

}

};

owner.addMouseListener(ml);

return ml;

}

public static Point getBottomRightOfScreen(Component c) {

Dimension scr_size = Toolkit.getDefaultToolkit().getScreenSize();

Dimension c_size = c.getSize();

return new Point(scr_size.width - c_size.width, scr_size.height - c_size.height);

}

public static Window visualize(Component c) {

return visualize(c, sExitWindow == null);

}

public static Window visualize(Image im) {

return visualize(new JLabel(new ImageIcon(im)));

}

public static Window visualize (Component c, int width, int height) {

return visualize(c, sExitWindow == null, width, height);

}

public static Window visualize(Component c, boolean exit, int width, int height) {

JFrame f;

Window w;

if (c instanceof Window) {

w = (Window) c;

}

else {

f = new JFrame();

f.getContentPane().add(c);

w = f;

}

// to be thread safe, this should be synchonized.

// but it doesn't really matter in everyday situations

if (sExitWindow == null) {

sExitWindow = w;

}

w.setSize(width, height);

w.setLocation(100, 100);

if (exit) {

w.addWindowListener(new WindowClosingActions.Exit());

}

w.setVisible(true);

return w;

}

public void center(Window w) {

Dimension dim = w.getSize();

Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();

w.setLocation((scr.width - dim.width)/2, (scr.height - dim.height)/2);

}

public static Window visualize(Component c, boolean exit) {

JFrame f;

Window w;

if (c instanceof Window) {

w = (Window) c;

}

else {

f = new JFrame();

f.getContentPane().add(c);

w = f;

}

w.pack();

w.setLocation(100, 100);

if (exit) {

w.addWindowListener(new WindowClosingActions.Exit());

}

w.setVisible(true);

return w;

}

}

tjacobs01a at 2007-7-9 23:08:15 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thats the one. It works exactly as I wanted. Im going to have to create my own implementation for this project but I will still add ur name to the documentation. Is this ok?
JOzzieTa at 2007-7-9 23:08:15 > top of Java-index,Desktop,Core GUI APIs...
# 5
> Thats the one. It works exactly as I wanted. Im going> to have to create my own implementation for this> project but I will still add ur name to the> documentation. Is this ok?That's fine
tjacobs01a at 2007-7-9 23:08:15 > top of Java-index,Desktop,Core GUI APIs...
# 6

The problem I am having now is that the custom JComponent that I have created is of static size. The JComponent is basically a Rectangle2D with set bounds. Therefore when the JComponent is resized only the bounds are changed and not the contents of the JComponent. How do I make it so that when the JComponent is added to the JPanel its contents (ie the Rectangle2D) are resizeable? (ie relative not absolute)

JOzzieTa at 2007-7-9 23:08:15 > top of Java-index,Desktop,Core GUI APIs...
# 7
OK forget that last post. The answer is: myCustomComponent.setRect(10, 0, this.getWidth(), this.getHeight()-60);
JOzzieTa at 2007-7-9 23:08:15 > top of Java-index,Desktop,Core GUI APIs...