Hi I have only one JDialog box in my Java application.I want to make it invisible if it lost the focus.
I've tried different method, But didn't able to trigger any of the window focus events. Here is my code:
public void windowGainedFocus(WindowEvent e) {
System.out.println("gained focus");
}
public void windowLostFocus(WindowEvent e) {
System.out.println("lost focus");
}
Responding to Focus events can be really tricky. My experience has been that pretty much any time someone has attempted to do non-standard things with focus, they come to regret it eventually. Not least among the issues is that it's not really all that portable - a lot of X-Windows based displays use focus-follows-mouse, which can result in the focus being transferred away when you're not expecting it to, resulting in early dismissal of your dialog.
That said, Sun's official tutorial is here: http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html . If I remember right, you can attach a PropertyChangeListener to the KeyboardFocusManager, and that will be triggered for focus changes: http://java.sun.com/javase/6/docs/api/java/awt/KeyboardFocusManager.html#addPropertyChangeListener%28java.beans.PropertyChangeListener%29
Use a WindowListener and handle the windowDeactivated event.
Related
How can I check if currently any mouse button is pressed and if so, which one it is?
The thing is that I need to use this kind of information in MouseListener.mouseEntered(). I checked MouseEvent but I could not find the method which would help me.
The getButton() method seems to only return value if there has been change in buttons' state.
Is there a way to find this out without manually keeping track of this somehow vie MouseListener.mousePressed()/mouseReleased() methods.
How can I check if currently any mouse button is pressed and if so, which one it is?
Presumably you want to invoke specific code depending on the button pressed so you can do something like:
if (SwingUtilities.isLeftMouseButton(...))
// do something
You could start by looking at How to write a Mouse Listener and the JavaDocs for MouseEvent in particular, the getButton method.
However, there are cross platform considerations that need to taken into consideration, which are overed by SwingUtilities.isLeftMouseButton and equivalent methods...
This will solve your problem
long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.out.println(e.paramString()+"-"+e.getSource());
}
}, eventMask);
This is a Global Event Listeners.
Get the source and button from AWTEvent and do whatever you want to perform.
I have built a program using Window Builder. I have used various Jbuttons, and would like to display a message such as "click here to add product" when the button is hovered over. I have added an event handler
addButton.addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent arg0) {
}
However i am unsure on the code needed within the FocusEvent to allow for this message to be displayed. I have looked at various methods such as MessageBox, but i don't think this is what i have been looking for.
No, don't use FocusListeners for this. Java Swing already has a built in tool for this, tool-tip. You will want to call setToolTipText(...) on your JButtons.
I have a small dialog frame that appears, and within this frame are a series of buttons and a textbox.
I need the frame to be able to detect when the user has put focus on something else on the screen (being: anything besides the frame and its components), so I can close down the frame.
Any advice on how to go about this? I've been trying at focus solutions for hours, to no solution!
Try using a WindowStateListener
The WindowEvent parameter it provides can tell you if the window has lost focus through the getNewState() method.
class MyFocusLostListener implements WindowStateListener {
public void windowStateChanged(WindowEvent e) {
if (e.getNewState() == WindowEvent.WINDOW_LOST_FOCUS) {
e.getWindow().setVisible(false);
}
}
}
need the frame to be able to detect when the user has put focus on something else on the screen
Use a WindowListener and listen for windowDeactivated.
listen to property changes of the property "permanentFocusOwner" of the KeyboardFocusManager. On being notified, check if the new focusOwner is in the child hierarchy under the frame, if not - close the frame.
Edit: seeing the answers suggesting a Window/StateListener - they are better than mine for a top-level window :-) Listening to the keyboardFocusManager is a good approach for containers deeper down in the hierarchy, implemented f.i. in the CellEditorRemover of a JTable (to decide if a pending edit should be terminated)
I'm searching for a solution in order to keep a JFrame always on top and with always I really mean always.
setAlwaysOnTop( true );
This won't work when I'm starting a game in fullscreen mode. I know you normally don't want your windows to stay on top but in this case it's required.
This can't be done.
For example, the Windows Task Manager, even when set to Always on Top will get covered up by full-screen applications.
This is due to the fact that full-screen applications typically use a different graphics context and can't be overlayed.
Start another process to check if the window is on top,if not, set it on top.
This is a sample code that should be helpful
public class AllWaysOnTop extends JFrame implements WindowListener {
AllWaysOnTop() {
// Code to setup your frame
addWindowListener(this);
// Code to show your frame
}
// The window event handlers. We use WindowDeactivated to
// try and keep the splash screen on top. Usually only keeps
// the splash screen on top of our own java windows.
public void windowOpened(WindowEvent event){};
public void windowActivated(WindowEvent event){};
public void windowDeactivated(WindowEvent event){
toFront();
}
public void windowIconified(WindowEvent event){};
public void windowDeiconified(WindowEvent event){};
public void windowClosed(WindowEvent event){};
public void windowClosing(WindowEvent event) {};
}
Reference
This forum post
This sounds like the kind of question that Raymond Chen always has to answer over at Link. How can you really really forever and for true keep a window in the foreground? You can't. Because what happens if somebody ELSE's window uses the same trick to keep itself always always and forever in the foreground? Which one wins?
If you mean fullscreen as in DirectX/OpenGL/whatever, I'm not sure you can (or should) really pull it off. Most operating systems disable their native windowing during full screen to improve rendering performance. Swing works via the native windowing toolkit.
You could write something that uses a timer and in short intervals (e.g., 200ms) instructs your window to go to the top. Depending on your operating system this would be exactly what you need, or a horrible cause of performance trouble or flickering.
I'm not sure but i would bet that the Fullscreen window also has Always On Top set to true, and in that case you have stumbled into the realm of undefined behavior. In general when two windows are set to always on top there is no guarantee about ordering. I think in general though the order is just dependent on the order in which they were set to always on top. So in that case i would just wait until the app has gone fullscreen to set it to always on top and see if that works.
In other cases ive seen people start threads and then occasionally reset the fram to always on top.
All these solutions are ugly, so just use the one that lets you sleep at night.
I know this post is old, but I encountered this problem and I found a satisfying solution.
My program has some notifications that I wish to be always on top, but when a movie entered full-screen, they disappeared. Fortunate, my program updates these notifications every 5 seconds, and if I call setVisible(true) on these JWindows, at each update, they regain the top position, if they have lost it.
I was looking to do the same thing as OP, have my app running in the foreground while my game ran. It doesn't work in fullscreen but if you put the game into windowed mode and adjust the window settings to fit your tv it works. I only needed the frame.setAlwaysOnTop to make it work.
I've been running up against a problem with Java Swing + my Wacom Graphire tablet for a few years in several Java applications and have now encountered it in my own.
I use a pen tablet to get around wrist issues while clicking a mouse, and it works fine under Windows except when I'm using Java applications. In Java applications, the single-click of the pen doesn't work correctly. (Usually the problem only occurs with file-selection dialog boxes or tree controls.) The pen tablet also comes with a wireless mouse that works with the same tablet, and its single-click does work correctly.
I don't know whether the problem is in the WACOM driver or in the Java Swing runtime for Windows or both. Has anyone encountered this before? I'd like to file a bug report with WACOM but I have no idea what to tell them.
I have been able to reproduce this in my own application that has a JEditorPane with an HTML document that I've added a HyperlinkListener to. I get HyperlinkEvent.ACTIVATED events on every single click with the mouse, but I do NOT get HyperlinkEvent.ACTIVATED events on every single click with the pen.
One big difference between a pen and a mouse is that when you click a button on a mouse, it's really easy to cause the button-click without mouse movement. On the pen tablet it is very hard to do this, and that seems to correlate with the lack of HyperlinkEvent.ACTIVATED events -- if I am very careful not to move the pen position when I tap the tablet, I think I can get ACTIVATED events.
Any suggestions for things to try so I can give WACOM some good information on this bug? It's really frustrating to not be able to use my pen with Java apps, especially since the pen works fine with "regular" Windows (non-Java) applications.
Normally I wouldn't ask this question here but I'd like to find out from a programmer's standpoint what might be going on so I can file a good bug report.
What you should do is add a mouseListener and see when it registers a mouseClicked(), mousePressed(), mouseReleased() event. I'm not sure if the swing reads the tablet pen as a mouse though. However, it should give you some insight into what's actually going on.
I tried dr.manhattan's suggestion and it works like a charm. I get mousePressed/mouseReleased events correctly; mouseClicked events happen always with the pen tablet mouse, but mouseClicked events do not happen with the pen unless I manage to keep the pen very still. Even a 1-pixel movement is enough to make it fail. I guess I should blame Java for this one: there's no way to specify a "click radius" for acceptible movement.
package com.example.bugs;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
public class WacomMouseClickBug {
public static void main(String[] args) {
JFrame jframe = new JFrame();
jframe.addMouseListener(new MouseListener(){
#Override public void mouseClicked(MouseEvent event) {
System.out.println("mouseClicked: "+event);
}
#Override public void mouseEntered(MouseEvent event) {}
#Override public void mouseExited(MouseEvent event) {}
#Override public void mousePressed(MouseEvent event) {
System.out.println("mousePressed: "+event);
}
#Override public void mouseReleased(MouseEvent event) {
System.out.println("mouseReleased: "+event);
}
});
jframe.setPreferredSize(new Dimension(400,400));
jframe.pack();
jframe.setLocationRelativeTo(null);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
}
I think you already got the answer yourself: Moving the pen results in some other event than a simple click, perhaps maybe a Drag and drop like event.
I'm not sure whether it's a Java/Swing or a Wacom problem, it could be that the tablet doesn't register the clicks as such but as drag events, or it could be that swing interprets the events incorrectly.
I reported this bug many years ago to Sun. It still is not fixed. Any decent ui framework will allow some movement between a press and release to generate a click event. A maximum movement of 1 pixel on a high dpi display is just ridiculous. It is not only an issue with wacom tablets, ie older people also have difficulties to keep the mouse still when clicking.