Possible to detect Mouse Release from non-source?

Ok, I've been trying to get this to happen to no avail...

What I want to be able to do is get my jbuttons to respond to a mouse release... however, the mouse release does not originate from the mouse press of that button.

What I mean is, is it possible for me to click outside somewhere, hold the click, drag over to an object (say the jbutton mentioned before) and then release the mouse button on the object and have it do something to the object?

So far, frome what I've read, it seems that release only affects the object where the click originated from. But if this is the case, then if i want my button to highlight after I release my mouse on it no matter what, then this is just not possible.

So is this possible? and how?

[763 byte] By [yeoua] at [2007-9-27 21:21:05]
# 1

How about this?

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ReleaseOnButton extends JFrame

{

private JButton button;

private JPanel contentPane;

public ReleaseOnButton()

{

setSize(300, 300);

button = new JButton("Release here");

contentPane = (JPanel)getContentPane();

contentPane.setLayout(new FlowLayout());

contentPane.add(button);

contentPane.addMouseListener(new MouseAdapter()

{

public void mouseReleased(MouseEvent e)

{

int x = e.getX();

int y = e.getY();

int buttonX = button.getLocation().x;

int buttonY = button.getLocation().y;

if((x > buttonX && x < buttonX + button.getWidth()) && (y > buttonY && y < buttonY + button.getHeight()))

{

System.out.println("On it!");

}

}

});

setVisible(true);

}

public static void main(String[] args)

{

new ReleaseOnButton();

}

}

Ceranith at 2007-7-7 3:15:32 > top of Java-index,Archived Forums,Swing...
# 2

Thanks for the help... it does do what I asked for, however it is not possible to implement this code in my situation.

The problem is that since mouseRelease insists on not getting noticed by any component other than the one that initiated the mousePress, I must somehow make all the other buttons notice the mouseRelease. So that means, to use this code, i'd have to somehow set the if(stuff) to catch all coords of all buttons (unknown as to how many yet) for every button.

Though your code does give me a footing to work from.

yeoua at 2007-7-7 3:15:32 > top of Java-index,Archived Forums,Swing...
# 3

A little harder, but I think this should do for you:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ReleaseOnButton extends JFrame

{

private JButton button;

private JPanel contentPane;

private Component[] components;

public ReleaseOnButton()

{

setSize(300, 300);

button = new JButton("Release here");

contentPane = (JPanel)getContentPane();

contentPane.setLayout(new FlowLayout());

contentPane.add(button);

contentPane.add(new JButton("And here"));

contentPane.add(new JButton("This too"));

contentPane.add(new JLabel("Not a button"));

contentPane.addMouseListener(new MouseAdapter()

{

public void mouseReleased(MouseEvent e)

{

components = contentPane.getComponents();

for(int i = 0; i < components.length; i++)

{

if(components[i] instanceof JButton)

{

int x = e.getX();

int y = e.getY();

int cX = components[i].getLocation().x;

int cY = components[i].getLocation().y;

if((x > cX && x < cX + components[i].getWidth()) && (y > cY && y < cY + components[i].getHeight()))

{

System.out.println("On it!");

}

}

}

}

});

setVisible(true);

}

public static void main(String[] args)

{

new ReleaseOnButton();

}

}

Ceranith at 2007-7-7 3:15:32 > top of Java-index,Archived Forums,Swing...
# 4

Thanks again, and again your code works... but if you click and drag on a button and release on another button, you don't get the "Got it". So you'd need an array of components for each component... giving n arrays for n components, and n components in each array, ending up with n^2 things to check. There just has to be an easier way (though i'd do it this way if there is no other possible way).

It just seems that mouseRelease should be able to affect a button which was not the source of the mousePress. But that is just not the case.

Hrm...

yeoua at 2007-7-7 3:15:32 > top of Java-index,Archived Forums,Swing...