dcsimg
Drag & Drop a JCheckbox
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   N_M
Posted On:   Wednesday, June 25, 2003 03:29 PM

Could anyone please let me know how to drag & drop a JCheckBox in an application?
Thanks in advance!

Re: Drag & Drop a JCheckbox

Posted By:   Tim_Schmelter  
Posted On:   Thursday, June 26, 2003 05:24 AM

Hello,
generally speaking you have to set the parent's layout-manager to null
and set the size and the location of your component to a special value,
otherwise it would be invisible.

Try using this checkbox (only beta-version ;)) :



import javax.swing.*;
import javax.swing.event.MouseInputAdapter;

import java.awt.*;
import java.awt.event.*;

/** MoveCheckBox is a moveable checkbox

* which parent container must not have a Layout-Manager,

* because it's location will be set absolutely.

* You also need to set its size and set its location

* otherwise it wouldnt be displayed.
*/
public class MoveCheckBox extends JCheckBox
{
/**
* static inner class MyInputAdapter which iplements all necessary methods

* from MouseListener- and MouseMotionListener interface.

* Full functionality is already provided for moving or resizing a MoveButton


*
* P.S. Do not call implemented methods directly ;-)
*/
static class MyInputAdapter extends MouseInputAdapter
{
/**default-cursor*/
private static Cursor defCursor = new Cursor(Cursor.DEFAULT_CURSOR);
/**move-cursor*/
private static Cursor moveCursor = new Cursor(Cursor.MOVE_CURSOR);

/**x-coordinate which is base(initialized when mousePressed) for moving*/
private int startX;
/**y-coordinate which is base(initialized when mousePressed) for moving*/
private int startY;

/**
* will initialize the base x- and y-coordinates
*/
public void mousePressed(MouseEvent e)
{

JCheckBox checkbox = (JCheckBox) e.getSource();
checkbox.setCursor(moveCursor);

this.startX = e.getX();
this.startY = e.getY();

System.out.println(
e.getComponent() + "
" + "pressed: " + e.getX() + " " + e.getY());
}

/**
* will perform moving the checkbox,

* depending on where the user pressed the box

*/
public void mouseDragged(MouseEvent e)
{
JCheckBox checkbox = (JCheckBox) e.getSource();

Point oldLocation = checkbox.getLocation();
Point newLocation =
new Point(
(int) oldLocation.getX() - startX + e.getX(),
(int) oldLocation.getY() - startY + e.getY());
checkbox.setLocation(newLocation);
/*System.out.println(
e.getComponent() + "
" + "dragged: " + e.getX() + " " + e.getY());*/

}

/**
* sets defaultCursor when user released the mouse
*/
public void mouseReleased(MouseEvent e)
{

JCheckBox checkbox = (JCheckBox) e.getSource();

checkbox.setCursor(defCursor);

System.out.println(
e.getComponent() + "
" + "released: " + e.getX() + " " + e.getY());
}
}

/**
* the MouseInputAdapter for mouseEvents and MouseMotionEvents
* registered on button affects button moving and resizing
*/
private static MyInputAdapter listen = null;

static {
listen = new MyInputAdapter();
}



/**
* Constructor for MoveCheckBox.
*
**/
public MoveCheckBox()
{
super();
initListener();
}

/**
* Constructor for MoveCheckBox.
* @param icon
**/
public MoveCheckBox(Icon icon)
{
super(icon);
initListener();
}

/**
* Constructor for MoveCheckBox.
* @param icon
* @param selected
**/
public MoveCheckBox(Icon icon, boolean selected)
{
super(icon, selected);
initListener();
}

/**
* Constructor for MoveCheckBox.
* @param text
**/
public MoveCheckBox(String text)
{
super(text);
initListener();
}

/**
* Constructor for MoveCheckBox.
* @param a
**/
public MoveCheckBox(Action a)
{
super(a);
initListener();
}

/**
* Constructor for MoveCheckBox.
* @param text
* @param selected
**/
public MoveCheckBox(String text, boolean selected)
{
super(text, selected);
initListener();
}

/**
* Constructor for MoveCheckBox.
* @param text
* @param icon
**/
public MoveCheckBox(String text, Icon icon)
{
super(text, icon);
initListener();
}

/**
* Constructor for MoveCheckBox.
* @param text
* @param icon
* @param selected
**/
public MoveCheckBox(String text, Icon icon, boolean selected)
{
super(text, icon, selected);
initListener();
}

/**
* Method initListener.
*/
private void initListener()
{
addMouseListener(listen);
addMouseMotionListener(listen);
}

/**
* Method main.
* @param args
*/
public static void main(String[] args)
{
JFrame fenster = new JFrame("'Move the button'-Fenster");
fenster.setSize(/*fenster.getToolkit().getScreenSize()*/
800, 600);
Container con = fenster.getContentPane();
con.setLayout(null);
JCheckBox moveCheckBox = new MoveCheckBox("move-me");
moveCheckBox.setContentAreaFilled(false);
con.add(moveCheckBox);
moveCheckBox.setSize(110, 30);
moveCheckBox.setLocation(400, 400);
fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenster.setVisible(true);
}

}




PS: sorry, I'm not familiar with this forum and dont know how to format my code,
at sun-forum,there is a [code]-tag.

I dont have time to format now, but maybe your IDE will do.


regards

Tim
About | Sitemap | Contact