Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Event/Mouse Listeners

Status
Not open for further replies.

Rhoon

Programmer
Mar 23, 2001
55
US
Greetings,

For several weeks I've been trying to fire a mouse event so when I click and drag a JLabel around the screen. I've gotten a work around, but its not exactly solid. After lots of digging through the API I figured out that a mouse event is fired relative to the component which fired it (duh! :). When I was doing label.setLocation( e.getX(), e.getY() ) it was pulling the coordinates off of the JLabel which I was pulling around. Although it worked, kind of, it was very choppy and did not look good at all. I switched the mousemotionlistener to the JFrame which was holding the jlabel and it works, but obviously its only a temp work around, what I would like to do actually click on any JLabel (might have upto 10 in the JFrame at any time) and just move that one JLabel around seemlessly. If anyone has any further suggestions I would greatly appreciate it.

Thanks in advance

Rhoon
 
Hi,
I don't have a perfect solution for you, but maybe this will give you more ideas on how to fix your problem. The problem with my solution is that it requires the user to first click on a button, (I used buttons instead of labels, but same idea) then move off the button and drag the mouse to the new position on the frame.
Hopefully someone else will have THE solution, or this will give you some more ideas.


Barry

Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MoveLabel
{
    static JButton   button_1, button_2;
    static JButton   activeButton;       

    public static void main(String [] args)
    {
        button_1 = new JButton("Move Me");
        button_2 = new JButton("Move Me Too");
        button_1.setBounds(10,10,120,30);
        button_2.setBounds(100,100,120,30);

        JFrame frame = new JFrame();
        frame.setSize(300,300);
        frame.setLocation(200,200);
        Container c = frame.getContentPane();
        c.setLayout(null);

        c.add(button_1);
        c.add(button_2);

        frame.show();

        button_1.addActionListener(new ActionListener()
            {                
                public void actionPerformed(ActionEvent $ae)
                {
                    System.out.println("ActionEvent on button_1");
                    activeButton = button_1;
                }

            });

        button_2.addActionListener(new ActionListener()
            {                
                public void actionPerformed(ActionEvent $ae)
                {
                    System.out.println("ActionEvent on button_2");
                    activeButton = button_2;
                }

            });

        
        c.addMouseMotionListener(new MouseMotionAdapter()
        {        
            public void mouseDragged(MouseEvent $me) 
            {
                 System.out.println("Mouse dragged on container");
                 if (activeButton != null)
                 {
                     activeButton.setLocation($me.getX(),$me.getY());    
                 }
            }
        });        
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top