Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2008

Tip: Looking for answers? Try searching our database.

Restricting dragging a JButton inside a JPanel

Thread view: 
Chanchal - 28 Jan 2008 11:28 GMT
Hello,

The following code creates a JPanel on which JButtons can be added by
clicking JButton. Also. these created JButtons  can be dragged around
in the JPanel.

<code>

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());
       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();
           c.setLocation(c.getX() + e.getX()-c.getWidth()/2, c.getY()
+ e.getY()-c.getHeight()/2);
       }
   }
}

</code>

The problem here is that the JButtons added to the JPanel can be
dragged out side the JPanel. Kindly advice how to restrict dragging
of  those JButton to be only inside the JPanel

Thanks in advance

Chanchal
Andrew Thompson - 01 Feb 2008 01:31 GMT
..
> ...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
Chanchal - 04 Feb 2008 09:12 GMT
Dear Mr.Thompson,

Thank you very much for the help. it has been very useful.

Regarding the doubt you expressed about he draggable buttons, the
project i'm doing is to develop a graph creation tool. To represent
the nodes in the graph i'm using button instances. These are joined
with lines to create the graph. The nodes needs to be dragged around
in the graph creation area so that the user can change the graph.

Hope this gives you an idea of what this gives to the user.

Thanks again

Chanchal
Daniel Pitts - 04 Feb 2008 16:01 GMT
> Dear Mr.Thompson,
>
[quoted text clipped - 11 lines]
>
> Chanchal
Is there a reason you're not using labels instead of buttons? Buttons
have a concept of triggered, which could interfere with your concept of
dragging.  You can attach a mouse listener to a label and simulate the
dragging yourself (I've done this for graph visualizations myself).  If
there is a *reason* you need a button instead of a label, you might find
yourself recreating the button functionality in a way that is compatible
with your dragging.  I shouldn't want to be the one that maintains
/that/ code.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Andrew Thompson - 04 Feb 2008 22:51 GMT
On Feb 5, 2:01 am, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
..
> > ...the
> > project i'm doing is to develop a graph creation tool. To represent
[quoted text clipped - 3 lines]
>
> > Hope this gives you an idea of what this gives to the user.

That is a good description.

> Is there a reason you're not using labels instead of buttons?
<snip>

Yep.  That para. about sums up my own thoughts
on the matter, given the use.

Why not use labels for the draggable nodes?

--
Andrew T.
PhySci.org
Chanchal - 10 Feb 2008 06:31 GMT
Hi,

My knowledge of swing was pretty low when this project was started.
Didn't get much time to learn also. So followed what came to mind. Now
too complicated to make the change.

Chanchal
Chanchal - 10 Feb 2008 06:38 GMT
Also,

i have made the buttons round by using images. Is the same thing
possible if i use lables

Chanchal
Daniel Pitts - 10 Feb 2008 20:13 GMT
> Also,
>
> i have made the buttons round by using images. Is the same thing
> possible if i use lables
>
> Chanchal

JLabels can contain Icon objects, which can be images.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.