volican
volican

Reputation: 137

coordinates of JWindow after click and drag

I have a program where I have JWindows which can be repositioned by click and drag. Just for information, they are transparent and have a blue border. I would like to know the coordinates of the top left corner of the rectangle (border) after it has been repositioned via click and drag. When I click a button in my gui a call will be made to captureComponent() to get the current x and y coordinate of the top left corner of the box. I have been trying to do this with Point loc = this.getLocation(); When I place it outside of MousePressed I get the coordinate before it was clicked and dragged somewhere else. When I try to put it inside MousePressed so that it will give me to updated values it gives me the error cannot find symbol: methods getLocation(). What can I do to fix this issue so that it will give me the updated value?

import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Point;
import javax.swing.border.LineBorder;

//test
public class Box extends JWindow {

    JPanel p=new JPanel();    
    public Box()            
    {

        this.setAlwaysOnTop(true);  
        this.setBackground(new Color(0, 0, 0, 0));
        setContentPane(p);   
        setSize(50,25);
        //this.setLocation(50, 50);

        p.setBorder(new LineBorder(Color.blue));
        p.setLayout(new FlowLayout());
        p.setBackground(new Color(0, 0, 0, 0));
        p.addMouseListener(adapter);
        p.addMouseMotionListener(adapter);

    }

    MouseAdapter adapter= new MouseAdapter()
    {

      int x,y; 
      public void mousePressed(MouseEvent e)
      {   
          if(e.getButton()==MouseEvent.BUTTON1)
        {            
            x = e.getX();
            y = e.getY();            
        }          
      }

      public void mouseDragged(MouseEvent e)
      {
          if( (e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) != 0)
          {
            setLocation(e.getXOnScreen()-x,e.getYOnScreen()-y);
            Point loc = this.getLocation();

          }
      }             

    };


    public void captureComponent()  {


        System.out.println(loc);


    }

}

The captureComponent method is called from another class when a button is pressed:

    btnSnap.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent event) {
    JButton clickedButton = (JButton) event.getSource();
    if (clickedButton == btnSnap) {

                    //new captureComponent();
                    //System.out.println("test");
                    Box capture = new Box();
                    capture.captureComponent(); 
    }                
}
    });

Upvotes: 0

Views: 65

Answers (1)

camickr
camickr

Reputation: 324128

it gives me the error cannot find symbol: methods getLocation().

You MouseListener is not a Component so you can't use the method unless you have a reference to a component.

One way would be to get the window for the component that generated the event:

Component component = e.getComponent();
Window window = SwingUtilities.windowForComponent( component );
Point location = window.getLocation();

Edit:

When I click a button in my gui a call will be made to captureComponent() to get the current x and y coordinate of the top left corner of the box.

Missed the above statement the first time. You are making your code too complicated. If you just want to know the location of the window when you click the button, then you just invoke the getLocation() method in your captureComponent() method. There is no need to save the location every time you drag the window.

Upvotes: 1

Related Questions