How to prevent JFrame from taking focus away from a CLI?
Hi,
I am lack the savvy, and have very likely picked an .. uncommon approach, but is it possible to prevent a JFrame from stealing the focus from my terminal command line interface?
I started developing a text based network simulator, but then discovered the JUNG library, that allowed me to visualize what I was doing with realtime animation. So I added a JFrame to show the graph I was traversing and manipulating. Currently the JFrame is owned by a thread that is created at launch, without displaying the window - as no graph has been loaded yet.
So the JFrame currently annoyingly steels the focus from the app.menu. I see to solutions to make this work better:
1. Display the JFrame with just a label saying "Please load a graph". This way it does not feel like the focus is stolen, but it does introduce the slight delay to display the window.
2. Prevent the focus from being stolen from the CLI. This is currently my preferred solution, however if it is not possible I will settle for the solution above.
So, can anyone help me keeping the focus at the CLI?
Thanks,
Pandemic
Message was edited by: typo
PanDemic
Message was edited by:
PanDemic
[1231 byte] By [
PanDemica] at [2007-11-26 17:37:27]

Follow the bouncing link to the api
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html
JFrame inherits a method setFocusable() from Component. By setting it to false it will reject any attempt to set focus to it. Now that said it may not give you what you want.
Hope this helps,
PS.
Thanks for your suggestions, but as far as I can tell, neither did any good.
toBack() simply sent the frame to the back (changed the window/frame sorting on my desktop), but without moving the focus to the command line.
The other method did not seem to have any impact at all?
Hm.. other suggestions? Anyone?
Pan
This might be what you're looking for: the setFocusableWindowState() method in Window:
[url=http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Window.html#setFocusableWindowState(boolean)]http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Window.html#setFocusableWindowState(boolean)[/url]
From the API:
Sets whether this Window can become the focused Window if it meets the other requirements outlined in isFocusableWindow. If this Window's focusable Window state is set to false, then isFocusableWindow will return false. If this Window's focusable Window state is set to true, then isFocusableWindow may return true or false depending upon the other requirements which must be met in order for a Window to be focusable.
Setting a Window's focusability state to false is the standard mechanism for an application to identify to the AWT a Window which will be used as a floating palette or toolbar, and thus should be a non-focusable Window. Setting the focusability state on a visible Window can have a delayed effect on some platforms ?the actual change may happen only when the Window becomes hidden and then visible again. To ensure consistent behavior across platforms, set the Window's focusable state when the WIndow is invisible and then show it.
Only last things I can suggest is perhaps using one of the following
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new NewJFrame2().setVisible(true);
}
});
or
java.awt.EventQueue.invokeAndWait(new Runnable()
{
public void run()
{
new NewJFrame2().setVisible(true);
}
});
Hm, I got the sneaking feeling I am being unclear:
When I say terminal, I mean the System.out place.. you know, where you start the app by executing java myapp. So only a single JFrame is made, and no other frames either.
And I am not sure I understand how to implement the invokeAndWait approach.. As it is the JFrame is currently created by the run() method for a thread, and using your sample code resulted in an error. Sorry for being so helpless..
..
PanDemic