Java Forum / General / January 2008
Preventing A New Window From Grabbing Focus
Hal Vaughan - 08 Jan 2008 07:45 GMT I can't believe I couldn't find this in a FAQ, but I want to keep a JFrame from grabbing focus when it is created and made visible. I tried using:
JFrame.setFocusable(false);
before the window was made visible, but it didn't have any effect. In this case, I have two progress windows, one monitoring the overall task and another that opens up with each subtask and tracks that subtask. I've noticed that if I'm working in a text editor or word processor and I'm typing when that 2nd window opens up, it grabs focus and if I type the wrong character, it triggers the "cancel" button.
I'd like to just open this JFrame window without it grabbing focus.
I know this is the opposite of the problem most people have and I'd think it'd be easy, but I can't find anything on this.
Thanks!
Hal
Gordon Beaton - 08 Jan 2008 07:48 GMT > I can't believe I couldn't find this in a FAQ, but I want to keep a > JFrame from grabbing focus when it is created and made visible. I > tried using: Your window manager or desktop environment can control this. You seem to be using KDE, which I'm told has focus-stealing settings that actually work (unlike Gnome, or Windows).
/gordon
--
Hal Vaughan - 08 Jan 2008 08:09 GMT >> I can't believe I couldn't find this in a FAQ, but I want to keep a >> JFrame from grabbing focus when it is created and made visible. I [quoted text clipped - 3 lines] > to be using KDE, which I'm told has focus-stealing settings that > actually work (unlike Gnome, or Windows). The problem is I'm not the only one who will be using this program. It'll be used on Windows and other versions of Linux.
Hal
Jason Cavett - 08 Jan 2008 14:04 GMT > >> I can't believe I couldn't find this in a FAQ, but I want to keep a > >> JFrame from grabbing focus when it is created and made visible. I [quoted text clipped - 8 lines] > > Hal I wish I could answer your question - I'm having a similar problem in which I open a new project and the JTree (which should have focus) does not (it belongs to the JFrame instead).
I'll let you know if I figure anything out.
Mark Space - 08 Jan 2008 19:01 GMT > I can't believe I couldn't find this in a FAQ, but I want to keep a JFrame > from grabbing focus when it is created and made visible. I tried using: > > JFrame.setFocusable(false); > > before the window was made visible, but it didn't have any effect. In this According to this:
<http://java.sun.com/javase/6/docs/api/java/awt/doc-files/FocusSpec.html#Focusabl eWindows>
what you did should work. You may wish to install a WindowListener or a WindowStateListener, and a VetoableChangeListner, as mentioned in the description, and see if you can do manually what they are saying there. You might also make certain that your situation matches what they describe in the documentation link I provided. Sometimes there can be subtle differences.
Otherwise, I dunno. Never actually tried this before.
Mark Space - 08 Jan 2008 19:03 GMT >> I can't believe I couldn't find this in a FAQ, but I want to keep a >> JFrame [quoted text clipped - 10 lines] > > what you did should work. You may wish to install a WindowListener or a Oh wait, they've actually got a different method call in there:
Window.setFocusableWindowState(false)
is what you want. My bad.
Hal Vaughan - 08 Jan 2008 21:05 GMT >>> I can't believe I couldn't find this in a FAQ, but I want to keep a >>> JFrame [quoted text clipped - 6 lines] >> >> According to this: <http://java.sun.com/javase/6/docs/api/java/awt/doc-files/FocusSpec.html#Focusabl eWindows>
>> what you did should work. You may wish to install a WindowListener or a > [quoted text clipped - 3 lines] > > is what you want. My bad. Here's an interesting twist:
I did Window.setFocusableWindowState(false), then after it was visible, did Window.setFocusableWindowState(true). It opened and didn't grab focus. So far so good. Then I ran my program, then pulled up a text editor. I started my program and it was running through its queue. Then I clicked on the text editor and typed nothing but spaces, since I knew the space would activate the cancel button on the pop up if it grabbed focus. The windows that popped up *never* grabbed focus, but the space still activated the cancel button.
I guess I'm going to have to go through a fair amount of reprogramming to keep one window open and just reset it with each new sub-task, as well as change the message in it. This is one reason I get frustrated with Java. Sometimes you have to go around your thumb just to get to your elbow.
Thanks for the info. I guess the problem is more the way Java handles things than anything else.
Hal
Mark Space - 08 Jan 2008 23:52 GMT > I did Window.setFocusableWindowState(false), then after it was visible, did > Window.setFocusableWindowState(true). It opened and didn't grab focus. So Why did you setFocusableWindowState to true? Won't a window receive events just fine if you don't have the focus? Last time I checked it did. Events are not the same as the focus in Swing.
Lew - 09 Jan 2008 02:35 GMT >> I did Window.setFocusableWindowState(false), then after it was >> visible, did >> Window.setFocusableWindowState(true). It opened and didn't grab >> focus. So > > Why did you setFocusableWindowState to true? So he could blame Java for making him go around his thumb to reach his elbow?
 Signature Lew
Hal Vaughan - 09 Jan 2008 04:10 GMT >>> I did Window.setFocusableWindowState(false), then after it was >>> visible, did [quoted text clipped - 5 lines] > So he could blame Java for making him go around his thumb to reach his > elbow? I just find if I have to do something in Perl, I can do it 5x faster than in Java. Sometimes I feel like Java was made by the Spaceballs. Remember the scene where Col. Saunders issues a lot of "Prepare for..." commands followed by telling people to do what he just told them to prepare? Then Dark Helmet says, "Why do you have to prepare for everything? Why not just do it?" I feel like in Java I spend 2-3 lines preparing to do something that takes one line.
As for this particular case, if you can tell me how the user can click on a cancel button (just a JButton) in a window that can't take focus AND have that JButton call its ActionListener, I'm open to it, but I experimented and had to have the window focusable for a JButton in it to receive a click.
Hal
Hal Vaughan - 09 Jan 2008 04:07 GMT >> I did Window.setFocusableWindowState(false), then after it was visible, >> did [quoted text clipped - 4 lines] > events just fine if you don't have the focus? Last time I checked it > did. Events are not the same as the focus in Swing. I tried it different ways. There is a cancel button on it and the user may need to cancel the operation. If the window can't get focus, the user can't click the cancel button.
Hal
Mark Space - 09 Jan 2008 05:38 GMT > I tried it different ways. There is a cancel button on it and the user may > need to cancel the operation. If the window can't get focus, the user > can't click the cancel button. Huh, well in my test it didn't. I'm on Windows though, so maybe other OSs work differently. Can you try it on Windows? I'm curious now if it's the code you have or a difference in OS.
This quick and dirty, but it does it. Quick and dirty in the generated by a GUI tool and I didn't clean it up. Three files, three classes, snip on the <<<<<'s.
package windowtest;
public class Main {
/** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here MainFrame.main( args ); }
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
package windowtest;
public class MainFrame extends javax.swing.JFrame {
/** Creates new form MainFrame */ public MainFrame() { initComponents(); setSize( 200, 200 ); setLocationRelativeTo( null ); }
/** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Make New Nonfocusable Frame"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } });
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(90, 90, 90) .addComponent(jButton1) .addContainerGap(97, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(134, 134, 134) .addComponent(jButton1) .addContainerGap(141, Short.MAX_VALUE)) );
pack(); }// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: new NFW().setVisible(true); }
/** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainFrame().setVisible(true); } }); }
// Variables declaration - do not modify private javax.swing.JButton jButton1; // End of variables declaration
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
package windowtest;
public class NFW extends javax.swing.JFrame {
/** Creates new form NFW */ public NFW() { initComponents(); setSize( 200 , 200 ); setLocationRelativeTo( null ); setFocusableWindowState(false); }
/** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Cancel"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } });
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(151, 151, 151) .addComponent(jButton1) .addContainerGap(178, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(145, Short.MAX_VALUE) .addComponent(jButton1) .addGap(130, 130, 130)) );
pack(); }// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.dispose(); }
/** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NFW().setVisible(true); } }); }
// Variables declaration - do not modify private javax.swing.JButton jButton1; // End of variables declaration
}
Hal Vaughan - 25 Jan 2008 22:36 GMT >> I tried it different ways. There is a cancel button on it and the user >> may [quoted text clipped - 8 lines] > by a GUI tool and I didn't clean it up. Three files, three classes, > snip on the <<<<<'s. I had tried this quickly earlier and got a lot of errors and had to get my work done, so I set it aside. I just got back to it to look it over and now I see the error is that I have Java5 on my system for testing but my environment set in Eclipse for 1.4.2 so I don't deal with issues with clients who haven't upgraded. You're using GroupLayout, which Java didn't put in until Java6. I'm going to have to d/l Java6 and put it in a separate directory so it doesn't interfere with my other settings. I'll do that over the weekend and see how it works.
I just found another issue that seems to be different on Windows vs. Linux, but I haven't been able to follow up on it yet. I have a program a friend was testing that uses a JList on a JScrollPane. On Linux I've had no trouble with it scrolling when I refresh, but on his XP system it seems that every time I refresh the list, it scrolls to the top.
Hal
> package windowtest; > [quoted text clipped - 169 lines] > > } Hal Vaughan - 26 Jan 2008 00:33 GMT >> I tried it different ways. There is a cancel button on it and the user >> may [quoted text clipped - 8 lines] > by a GUI tool and I didn't clean it up. Three files, three classes, > snip on the <<<<<'s. I had time to download Java6 and try this earlier than I expected. It does work fine on Linux. I added in a Thread.sleep() statement so I could click the button, then start typing so I could watch and see if I lost any characters when the window opened or if I were hitting the space key over and over, if it would trigger the "Cancel" button and it didn't.
So I'd say this works on Linux. I could swear I used the same method to make a window non-focusable, but when I did, it was not possible to press the "Cancel" button. I've trashed my code that wasn't working and, like I said, gone with a different idea. However, I'll be looking over your code to see if you did anything differently than I did.
It could also be a bug or glitch that was fixed between Java5 and Java6.
Thanks for the help on this and I'm glad I finally got time to compile and test this for you.
Hal
> package windowtest; > [quoted text clipped - 169 lines] > > } Mark Space - 26 Jan 2008 05:29 GMT > So I'd say this works on Linux. I could swear I used the same method to
> It could also be a bug or glitch that was fixed between Java5 and Java6. Well thanks for the report. At least is seems to work ok with Java 6.
Last time I worked on something like this (getting one component to hold the focus and insert caret, all the time) I came up with all kinds of crazy schemes to make it work. I made my own listeners for the FocusManager (I think that's it) and all sorts of stuff before I realized there was an easy way to do it (just set all the components in the window to non-focusable ("setFocusable(false)") except the one I wanted). I had to back out all kinds of stuff, so I'm guessing you had some little issue in the way you were setting up your windows that might have been left over from other experiments.
Incidentally, the GroupLayout is totally incidental to the program. It should work regardless what layout manager you use. If I had hand coded it to clean it up, I probably would have just used the default BorderLayout and FlowLayout.
The important bits are:
In the main window, set it up like this:
>> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) >> { >> // TODO add your handling code here: >> new NFW().setVisible(true); >> } That's it. I use the main window to hold a button that makes new NFW ("non-focusable window"). You could use any GUI builder and layout manager to set up that button.
And in the NFW, I just:
>> public class NFW extends javax.swing.JFrame { >> >> /** Creates new form NFW */ >> public NFW() { >> initComponents(); //... snip
>> setFocusableWindowState(false); >> } That's it. setFocusableWindowState(false), the rest is just boilerplate set up. The button in the NFW window has is just to make sure it can receive clicks, and disposes the window.
Again, any layout and any GUI builder would work. Bog standard Java and boilerplate.
Hal Vaughan - 26 Jan 2008 21:37 GMT >> So I'd say this works on Linux. I could swear I used the same method to > [quoted text clipped - 27 lines] >>> new NFW().setVisible(true); >>> } That's where I added a sleep() statement to delay it. I stalled it something like 5 seconds so I could click the button, then switch to my text editor. The first time I just typed and kept my focus on the text editor until I realized the window had already opened, then scanned what I had typed and there were no missing characters. The next few times I just kept tapping the space bar since I've seen that trigger JButtons and there was no indication it effected the window at all.
> That's it. I use the main window to hold a button that makes new NFW > ("non-focusable window"). You could use any GUI builder and layout [quoted text clipped - 14 lines] > set up. The button in the NFW window has is just to make sure it can > receive clicks, and disposes the window. I tried to set the focusable state, but maybe I did, as you said, have other stuff from other experiments in the way. At least now we know your way is simple and works!
Hal
Hal Vaughan - 19 Jan 2008 23:14 GMT > I can't believe I couldn't find this in a FAQ, but I want to keep a JFrame > from grabbing focus when it is created and made visible. I tried using: [quoted text clipped - 17 lines] > > Hal I finally got a chance to get back to this issue. I had time to think about it before I got back to it. My first experience in working with Java was a program that had to work on Linux and Windows. I had read how Java was WORA, but I ran into troubles because I didn't realize a ChangeListener would pick up on a change that I created in a component and thought it only picked up on changes from the user. On Linux (or was it Windows), this problem never showed up, but on the other OS, it was a mess. Then I realized the routines for rendering components must work differently on each OS. Since then, I've been suspicious of GUI issues that could change on different platforms.
After thinking about it, and looking at what other people said on this thread, I decided I didn't want to spend a lot of time trying this on different platforms and tweaking it. While it was a pain, I basically solved this particular problem without dealing with the bigger picture. I changed my own class that creates a simple window with a JProgressMonitor in it so it was possible to show status, in this case, a downloading file, then reset to the "Cylon" state with the animation that looks like a Cylon eye, then reset it again to start status for a task. That way this monitor window stayed open throughout the entire queue, instead of closing after each data download then opening for the next one.
Another point is that I didn't want to count on it opening and not grabbing focus because there's always a chance, with a new version of Windows or KDE or X Windows, that it might not work quite the same, so I just wanted to avoid that entire questions.
In some ways I took the easy way out. It involved more recoding than I wanted to do, but only about 45 minutes worth.
Thanks for all the help. I did learn a few things that I'll be playing with in the future. In this case, I guess I just chickened out and did what I knew I could get working quickly.
Hal
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|