How to check if the mouse is pressed out of an JFrame - java

Right now I am using a MouseListener to see if the mouse is pressed but it doesn't register when you press outside of an JFrame I would really need it to so how do I check for mouse events outside of a JFrame?

Right now I am using a MouseListener to see if the mouse is pressed
but it doesn't register when you press outside of an JFrame I would
really need it to so how do I check for mouse events outside of a
JFrame?
then JFrame lost Focus, you can test by using WindowFocusListener
Focus is asynchronous, then everything inside windowGainedFocus and windowLostFocus should be wrapped into invokeLater

Add a window listener
addWindowListener(new WindowListener() {
#Override
public void windowOpened(WindowEvent arg0) {
}
#Override
public void windowIconified(WindowEvent arg0) {
}
#Override
public void windowDeiconified(WindowEvent arg0) {
}
#Override
public void windowDeactivated(WindowEvent arg0) {
}
#Override
public void windowClosing(WindowEvent arg0) {
}
#Override
public void windowClosed(WindowEvent arg0) {
}
#Override
public void windowActivated(WindowEvent arg0) {
}
});
Try out all the methods (window...) and see which one works out best for you!
:)
I'm not telling you exactly what to do because to learn you cant just copy paste!

To know the status of mouse outside the window you can use:
Point point = MouseInfo.getPointerInfo().getLocation();
Unfortunatly java.awt.event.MouseMotionListener give you information about mouse movement inside your window.

Related

Change image on JButton on mouse over

I am using an Icon with Java (Swing) JButton. Is it possible to change the icon when I take my mouse arrow over it?
I saw somewhere on Youtube that it is possible, but am unable to recall it.
You can take advantage of the JButton API which provides this kind of support.
Take a look at JButton#setRolloverIcon and JButton#setRolloverSelectedIcon
You will need to implement MouseListener like this:
public class YourClass extends JFrame implements MouseListener {
#Override
public void mouseEntered(MouseEvent e) { }
#Override
public void mouseExited(MouseEvent e) { }
#Override
public void mouseClicked(MouseEvent e) { }
#Override
public void mousePressed(MouseEvent e) { }
#Override
public void mouseReleased(MouseEvent e) { }
}
Add your function where needed.
You can override the mouseEntered() function by implementing a MouseListener and add the code to change the icon in that function.
If you're using an abstract button, you can just use setRolloverIcon() to set an image which will appear on rollOver.

Double click JTable

The cells in my JTable become editable only on the second click. When I debugged I noticed that on the second click the mouse released event is not fired. I saw a lot of answers for this problem with create a setSingleClick(1)... but it doesn't to work. I think that if i can get that second mouseReleased event to get fire i might be able to make it work. Does anybody has any sugestions?
table.addMouseListener(new TableMouseListener()) ;
class TableMouseListener extends MouseAdapter{
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
}
public void mouseClicked(MouseEvent e) {
System.out.println("mouseClicked");
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
}
}
Try something like this:
container_table.addMouseListener(new MouseAdapter() {
public void mouseClicked (MouseEvent me) {
if (me.getClickCount() == 2) {
//Double clicked
}
}
});
This way, you know that the 'container_table' has been clicked twice, and then you can get the selected row, and make things with it.
Hope it helps.

Capture all events in JFrame without deactivating the window

I'm trying to develop something like a remote desktop / VNC client. It's necessary for me to capture all events in the client window. The method I'm using is to override the processEvent method of the JFrame:
#Override
protected void processEvent(AWTEvent e) {
...
}
However on events like the Windows key or Alt+Tab the window is getting deactivated:
...
00000191 KEY_PRESSED,keyCode=524,keyText=Windows,keyChar=Undefined keyChar,keyLocation=KEY_LOCATION_LEFT,rawCode=91,primaryLevelUnicode=0,scancode=91,extendedKeyCode=0x20c
00000192 KEY_RELEASED,keyCode=524,keyText=Windows,keyChar=Undefined keyChar,keyLocation=KEY_LOCATION_LEFT,rawCode=91,primaryLevelUnicode=0,scancode=91,extendedKeyCode=0x20c
000000ce WINDOW_DEACTIVATED,opposite=null,oldState=0,newState=0
...
How do I keep the window active on such events?
I would prefer a pure Java solution to this. If there is no pure java solution, can someone point me towards a JNA solution (or any other solution for that fact)?
EDIT1:
* Resolved ambiguous term 'focus' to window deactivation
* Emphasized that non pure Java solutions are acceptible
1.) JNA comes with an example that does almost what you want:
http://java.net/projects/jna/sources/svn/content/trunk/jnalib/contrib/w32keyhook/KeyHook.java
In order to block a key, just return 1 instead of calling CallNextHookEx - cf. the MSDN documenttaion.
2.) JNativeHook allows you to hook into global event processing, but right now there is no way of stopping events from being deliverd - e.g. the Windows key will still activate the start menu. However it is still worth looking at since it comes with much less overhead, and you can modify it (starting with CallNextHookEx here) to behave the way you want. It is licensend under the GPL though.
3.) Another clean way of doing this, would be to switch to SWT and use the SWT Win32 Extensions to intercept keyboard events.
Can you not set the window to be focusable all the time (Window.setFocusableWindowState), because JFrame does inherit that method from Window. Something simple like: window.setFocusableWindowsState(true). And inside a key event listener call this:
(this code is modified, but originally from Unresponsive KeyListener for JFrame)
public class MyFrame extends JFrame {
private class MyDispatcher implements KeyEventDispatcher
{
private JFrame frame;
public MyDispatcher(JFrame jf)
{
this.frame = jf;
}
public boolean dispatchKeyEvent(KeyEvent e)
{
frame.setFocusableWindowState(true);
return false;
}
}
public MyFrame()
{
add(new JTextField());
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new MyDispatcher(this));
}
public static void main(String[] args)
{
MyFrame f = new MyFrame();
f.pack();
f.setVisible(true);
}
}
I did not get to test this code, but i hope at least the idea is getting you in right direction.
You can set a custom FocusManager and override the dispatchEvent method or use Toolkit.getDefaultToolkit().addAWTEventListener(..). However this won't catch OS-catched keyEvents, but you will still get events like repaint if you register at the toolkit.
You can use WindowListener.Hope this will help to capture the event .There are windowfocuslistener,windowstatelistener too.
public class WindowListenerImpl implements WindowListener(){
#Override
public void windowOpened(WindowEvent windowevent) {
//urs windowevent to get source.
}
#Override
public void windowIconified(WindowEvent windowevent) {
//urs windowevent to get source.
}
#Override
public void windowDeiconified(WindowEvent windowevent) {
//urs windowevent to get source.
}
#Override
public void windowDeactivated(WindowEvent windowevent) {
//urs windowevent to get source.
}
#Override
public void windowClosing(WindowEvent windowevent) {
//urs windowevent to get source.
}
#Override
public void windowClosed(WindowEvent windowevent) {
//urs windowevent to get source.
}
#Override
public void windowActivated(WindowEvent windowevent) {
//urs windowevent to get source.
}
public WindowEvent{
public static void main(String[] args){
WindowListenerImpl listenerImpl=new WindowListenerImpl ();
new JFrame.addWindowListener(listenerImpl);
}

Make JButton Close a Window But Run Code In WindowClosing(WindowsEvent e) Before Closing

I have to save the state of the program in a xml when the user clicks the Close Button or another JButton which says Exit on it. I have it so it runs using a WindowListener when the Window is closing it runs it. But that only works if you click the Red X close button, but not when you have the other button. I could run it before I do System.exit(0) in actionListener for button but some of the classes I need information from I do not have access from that place.
This is for the frame.
frame.addWindowListener(new WindowListener(){
public void windowActivated(WindowEvent arg0){}
public void windowClosed(WindowEvent arg0){}
public void windowClosing(WindowEvent arg0)
{
myScreen.write.writeToXmlFile(myScreen.pics, myScreen.row, myScreen.col);
}
public void windowDeactivated(WindowEvent arg0){}
public void windowDeiconified(WindowEvent arg0){}
public void windowIconified(WindowEvent arg0){}
public void windowOpened(WindowEvent arg0){}
});
This is the code in button.
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
I do not have access to myScreen from the class that has button in it.
I do not have access to myScreen from the class that has button in it.
JButton#doClick(); can do that
but correct way could be too (maybe)
public void windowClosing(WindowEvent arg0)
1st. void or class
create screenshot
2.nd void or class
setVisible(false) for Top-Level Container(s)
3rd void or class
save applications states
4th void or class
System.exit()
sure you can put all 4 voids (classes) to the one, but in a.m. case is code only more clear and readable

Changing the Background of a button after it is clicked (i.e. after Action performed)

I am trying to change the background of a JButton after it is clicked. Currently my buttons are located in a GridLayout (3x3) and look like this:
tiles.add(new JButton(new AbstractAction("") {
#Override
public void actionPerformed(ActionEvent e) {
this.setIcon("foo.png");
}
}));
This does not work. How do I manipulate the background of the image from within the actionperformed?
A JToggleButton might be ideal for this, as shown on Swing JToolbarButton pressing
Note that you would need to add some code to ensure a button was only clickable once.
Alternately you might use a standard JButton and call AbstractButton.setDisabledIcon(Icon). Disable the button when clicked, and it will flip to the alternate icon.
You can create your own listener that implements the MouseListener. This way, you can control when the background of the button changes (when the mouse is released, pressed, etc). Here is an example
//Add the listener to the button
myButton.addMouseListener(new customActionListener());
//Create the listener
class customActionListener implements MouseListener {
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
Icon icon = new ImageIcon("icon.gif");
myButton.setIcon(icon);
}
public void mouseClicked(MouseEvent e) {
}
}
At whatever point you want to set the background back to its default, use:
myButton.setIcon(new ImageIcon());

Categories

Resources