Re: Restricting dragging
To: comp.lang.java.gui
On Jan 28, 10:28 pm, Chanchal <chanchal.ja...@gmail.com> wrote:
..
...Kindly advice how to restrict dragging
of those JButton to be only inside the JPanel
Sure*. But since I think this entire 'dragging
buttons' GUI is very counter-inuitive (the user
would expect that clicking a button causes it to
avtivate), perhap you can kindly advise what the
point of all this is. What does it provide to the
end user?
* Note that 'accounting for the border' is left
as an exercise for the reader. One easy solution
is to put the drag panel inside another panel,
with the 'outer' panel having the border.
<sscce>
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class DragTest extends JFrame {
private JPanel dragArea;
private JButton jButton1;
public DragTest() {
GridBagConstraints gridBagConstraints;
dragArea = new JPanel();
jButton1 = new JButton();
dragArea.setBorder(new LineBorder(Color.BLACK, 2));
getContentPane().setLayout(new GridBagLayout());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
GroupLayout dragAreaLayout =
new GroupLayout(dragArea);
dragArea.setLayout(dragAreaLayout);
dragAreaLayout.setHorizontalGroup(
dragAreaLayout.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addGap(0,376, Short.MAX_VALUE)
);
dragAreaLayout.setVerticalGroup(
dragAreaLayout.createParallelGroup(
GroupLayout.Alignment.LEADING).
addGap(0, 217, Short.MAX_VALUE)
);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTH;
getContentPane().add(dragArea, gridBagConstraints);
jButton1.setText("Click Me");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = GridBagConstraints.SOUTH;
getContentPane().add(jButton1, gridBagConstraints);
pack();
}
private void jButton1ActionPerformed(ActionEvent evt) {
JButton jButton = new JButton("Drag Me");
jButton.setBounds(50,50,100,50);
jButton.addMouseMotionListener(new DragHandler());
getContentPane().add(jButton);
dragArea.add(jButton);
dragArea.repaint();
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new DragTest().setVisible(true);
}
});
}
/* Inner class to handle dragging of
items on the workspace */
class DragHandler extends MouseMotionAdapter{
public void mouseDragged(MouseEvent e) {
Component c = e.getComponent();
setLocationSafe(c,e);
}
public void setLocationSafe
(Component c, MouseEvent e) {
c.setLocation(
c.getX() + e.getX()-c.getWidth()/2,
c.getY() + e.getY()-c.getHeight()/2);
if ( c.getX()<0 ) {
c.setLocation(
0,
c.getY());
}
if ( c.getY()<0 ) {
c.setLocation(
c.getX(),
0);
}
if ( c.getX()+c.getWidth()>
c.getParent().getWidth() ) {
c.setLocation(
c.getParent().getWidth()-c.getWidth(),
c.getY());
}
if ( c.getY()+c.getHeight()>
c.getParent().getHeight() ) {
c.setLocation(
c.getX(),
c.getParent().getHeight()-c.getHeight());
}
}
}
}
</sscce>
--
Andrew T.
PhySci.org
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24