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]
# 1
Have you tried jframe.toBack(); ?
javiousa at 2007-7-9 0:05:37 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

puckstopper31a at 2007-7-9 0:05:37 > top of Java-index,Java Essentials,Java Programming...
# 3

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

PanDemica at 2007-7-9 0:05:37 > top of Java-index,Java Essentials,Java Programming...
# 4

It's the funky console window that stumps me. I never could figure out how those windows are actually created and displayed and referenced from within the application. I don't even think they are decendants of java.awt or javax.swing components and if this is true then you'll likely need to search for documentation on your console window implementation in hopes of finding some controller methods to allow you to direct it's behaviour. Maybe it's a spawned process communicating back and forth to the java app or something. Sorry it didn't help.

javiousa at 2007-7-9 0:05:37 > top of Java-index,Java Essentials,Java Programming...
# 5

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.

kevjavaa at 2007-7-9 0:05:38 > top of Java-index,Java Essentials,Java Programming...
# 6
Double Double posted posted. (sorry sorry)
kevjavaa at 2007-7-9 0:05:38 > top of Java-index,Java Essentials,Java Programming...
# 7

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);

}

});

javiousa at 2007-7-9 0:05:38 > top of Java-index,Java Essentials,Java Programming...
# 8

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

PanDemica at 2007-7-9 0:05:38 > top of Java-index,Java Essentials,Java Programming...
# 9
I think you should decide whether you want a console application or a GUI application. Choose one. So far you've made a non-decision by trying to mix the two, and instead of recognizing the problems as symptoms of that non-decision you are trying to patch the mess.
DrClapa at 2007-7-9 0:05:38 > top of Java-index,Java Essentials,Java Programming...
# 10

Sorry, you are correct, I mis-interpreted your meaning of console. I thought it was something along the lines of a Jacada type of application or similiar where a command driven or "curses" type of application was being interfaced with java. I have worked with some of these types of apps and never had the time to actually figure out how the command window is created and managed.

javiousa at 2007-7-9 0:05:38 > top of Java-index,Java Essentials,Java Programming...