JTextField action causing JFrame to come to front

Hi,

I have attached an ActionListener to a JTextField for the explicit purpose of when the user presses "Enter", it will bring up another window.

This is what it does.

However, the window shows up behind the first window in which the text field is located. In other words, it appears the "Enter" keystroke is passed onto the first window which causes it to come to the front.

If the first window has a default button, that button will get invoked.

As a workaround, I call toFront() on the second window which causes it to come to the front. However, I am not satisfied with this workaround.

Does anyone know what to do about this? I have tried other solutions (e.g. use a KeyListener instead and try to consume the KeyEvent), all with the same results.

[795 byte] By [avromfa] at [2007-11-27 2:38:32]
# 1
Do you show the new frame inside SwingUtilities.invokeLater()?If yes, then the toFront is a valid solution and i wouldn't call it a "workaround" since this is exactly what it's supposed to do.
kirillga at 2007-7-12 2:59:46 > top of Java-index,Desktop,Core GUI APIs...
# 2

I've never noticed this problem, although usually I add the ActionListener to a button to open a window.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 2:59:46 > top of Java-index,Desktop,Core GUI APIs...
# 3

toFront() is a workaround solution considering that new windows should (and are usually) are brought to the front anyways (barring other circumstances). What is happening is that the original window is being brought to front due to the key press and then my cal to toFront brings the seond window on top, resulting in a slight flicker in the display due to a repaint.

What I want to do is prevent the key press from being invoked on the first window and only use it for the action on the text field.

avromfa at 2007-7-12 2:59:46 > top of Java-index,Desktop,Core GUI APIs...
# 4
I'll see what I can come up with as a sample.For the record, I have a button as well invoking the same action which does NOT result in the second window ending up behind the first one.
avromfa at 2007-7-12 2:59:46 > top of Java-index,Desktop,Core GUI APIs...
# 5

Unfortunately (for me), the test program I just wrote works perfectly as expected. Adding an ActionListener to a text field that brought up a new JFrame brought it up on top of the current window. (Which is not surprising, considering I have to put up with some old and not neccessarily well written code in our application.)

If you can give me any pointers as to how I can figure out where this is coming from it would be appreciated.

avromfa at 2007-7-12 2:59:46 > top of Java-index,Desktop,Core GUI APIs...
# 6
And since I forgot to mention it:Running WinXP on Java 1.5.0_11.
avromfa at 2007-7-12 2:59:46 > top of Java-index,Desktop,Core GUI APIs...
# 7
post your code plz
java_2006a at 2007-7-12 2:59:46 > top of Java-index,Desktop,Core GUI APIs...