Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Move button

Posted on 2005-05-16
Medium Priority
Last Modified: 2011-10-03
Here is my applet

import java.awt.event.*;

import javax.swing.*;

public class Diagram extends JApplet {
      private JButton b;
      private Box box;

      public void init()
            box = Box.createVerticalBox();
            b = new JButton("Hello");
            MyMoveListener moveListener = new MyMoveListener();
            b.addMouseMotionListener( moveListener );

      private class MyMoveListener extends MouseMotionAdapter {
                  public void mouseDragged(MouseEvent e)

When they press and move mouse , I want that button is moved too . How can I do ?
Question by:hahahahahaha
LVL 86

Accepted Solution

CEHJ earned 1000 total points
ID: 14011161
You need to implement a null layout and set the bounds of the button in the dragged event
LVL 92

Assisted Solution

objects earned 1000 total points
ID: 14014663
Add your mouse listener to the box and not the button, and set the buttons location using the following:

               public void mouseDragged(MouseEvent e)
                     b.setLocation(e.getX(), e.getY());

No point using a Box though, you may as well replace it with a JPanel

private JPanel box = new JPanel(null);

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question