Preventing A New Window From Grabbing Focus

  • Permalink
  • submit to reddit
  • Email
  • Follow


I can't believe I couldn't find this in a FAQ, but I want to keep a JFramefrom 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 thiscase, I have two progress windows, one monitoring the overall task andanother that opens up with each subtask and tracks that subtask.  I'venoticed that if I'm working in a text editor or word processor and I'mtyping when that 2nd window opens up, it grabs focus and if I type thewrong 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 thinkit'd be easy, but I can't find anything on this.Thanks!Hal
0
Reply Hal 1/8/2008 7:45:27 AM

See related articles to this posting


On Tue, 08 Jan 2008 07:45:27 GMT, Hal Vaughan wrote:> 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 seemto be using KDE, which I'm told has focus-stealing settings thatactually work (unlike Gnome, or Windows)./gordon--
0
Reply Gordon 1/8/2008 7:48:37 AM

Gordon Beaton wrote:> On Tue, 08 Jan 2008 07:45:27 GMT, Hal Vaughan wrote:>> 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).The problem is I'm not the only one who will be using this program.  It'llbe used on Windows and other versions of Linux.Hal
0
Reply Hal 1/8/2008 8:09:13 AM

On Jan 8, 3:09 am, Hal Vaughan <h...@halblog.com> wrote:> Gordon Beaton wrote:> > On Tue, 08 Jan 2008 07:45:27 GMT, Hal Vaughan wrote:> >> 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).>> 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.>> HalI wish I could answer your question - I'm having a similar problem inwhich 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.
0
Reply Jason 1/8/2008 2:04:52 PM

Hal Vaughan wrote:> 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 thisAccording to this:<http://java.sun.com/javase/6/docs/api/java/awt/doc-files/FocusSpec.html#FocusableWindows>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.
0
Reply Mark 1/8/2008 7:01:19 PM

Mark Space wrote:> Hal Vaughan wrote:>> 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#FocusableWindows> > > > 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.
0
Reply Mark 1/8/2008 7:03:01 PM

Mark Space wrote:> Mark Space wrote:>> Hal Vaughan wrote:>>> 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#FocusableWindows>>> >> >> 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.Here's an interesting twist:I did Window.setFocusableWindowState(false), then after it was visible, didWindow.setFocusableWindowState(true).  It opened and didn't grab focus.  Sofar so good.  Then I ran my program, then pulled up a text editor.  Istarted my program and it was running through its queue.  Then I clicked onthe text editor and typed nothing but spaces, since I knew the space wouldactivate the cancel button on the pop up if it grabbed focus.  The windowsthat popped up *never* grabbed focus, but the space still activated thecancel button.I guess I'm going to have to go through a fair amount of reprogramming tokeep one window open and just reset it with each new sub-task, as well aschange 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 handlesthings than anything else.Hal
0
Reply Hal 1/8/2008 9:05:13 PM

Hal Vaughan wrote:> I did Window.setFocusableWindowState(false), then after it was visible, did> Window.setFocusableWindowState(true).  It opened and didn't grab focus.  SoWhy 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.
0
Reply Mark 1/8/2008 11:52:07 PM

Mark Space wrote:> Hal Vaughan wrote:> >> 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?-- Lew
0
Reply Lew 1/9/2008 2:35:17 AM

Mark Space wrote:> Hal Vaughan wrote:> >> 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.I tried it different ways.  There is a cancel button on it and the user mayneed to cancel the operation.  If the window can't get focus, the usercan't click the cancel button.Hal
0
Reply Hal 1/9/2008 4:07:44 AM

Lew wrote:> Mark Space wrote:>> Hal Vaughan wrote:>> >>> 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?I just find if I have to do something in Perl, I can do it 5x faster than inJava.  Sometimes I feel like Java was made by the Spaceballs.  Remember thescene where Col. Saunders issues a lot of "Prepare for..." commandsfollowed by telling people to do what he just told them to prepare?  ThenDark Helmet says, "Why do you have to prepare for everything?  Why not justdo it?"  I feel like in Java I spend 2-3 lines preparing to do somethingthat takes one line.As for this particular case, if you can tell me how the user can click on acancel button (just a JButton) in a window that can't take focus AND havethat JButton call its ActionListener, I'm open to it, but I experimentedand had to have the window focusable for a JButton in it to receive aclick.Hal
0
Reply Hal 1/9/2008 4:10:41 AM

Hal Vaughan wrote:> 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}
0
Reply Mark 1/9/2008 5:38:22 AM

Hal Vaughan wrote:> 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!> > HalI finally got a chance to get back to this issue.  I had time to think aboutit before I got back to it.  My first experience in working with Java was aprogram that had to work on Linux and Windows.  I had read how Java wasWORA, but I ran into troubles because I didn't realize a ChangeListenerwould pick up on a change that I created in a component and thought it onlypicked up on changes from the user.  On Linux (or was it Windows), thisproblem never showed up, but on the other OS, it was a mess.  Then Irealized the routines for rendering components must work differently oneach OS.  Since then, I've been suspicious of GUI issues that could changeon different platforms.After thinking about it, and looking at what other people said on thisthread, I decided I didn't want to spend a lot of time trying this ondifferent platforms and tweaking it.  While it was a pain, I basicallysolved this particular problem without dealing with the bigger picture.  Ichanged my own class that creates a simple window with a JProgressMonitorin 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 Cyloneye, then reset it again to start status for a task.  That way this monitorwindow stayed open throughout the entire queue, instead of closing aftereach data download then opening for the next one.Another point is that I didn't want to count on it opening and not grabbingfocus because there's always a chance, with a new version of Windows or KDEor X Windows, that it might not work quite the same, so I just wanted toavoid that entire questions.In some ways I took the easy way out.  It involved more recoding than Iwanted to do, but only about 45 minutes worth.Thanks for all the help.  I did learn a few things that I'll be playing within the future.  In this case, I guess I just chickened out and did what Iknew I could get working quickly.Hal
0
Reply Hal 1/19/2008 11:14:20 PM

Mark Space wrote:

> Hal Vaughan wrote:
> 
>> 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.

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

0
Reply hal (27) 1/25/2008 10:36:57 PM

Mark Space wrote:> Hal Vaughan wrote:> >> 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.I had time to download Java6 and try this earlier than I expected.  It doeswork fine on Linux.  I added in a Thread.sleep() statement so I could clickthe button, then start typing so I could watch and see if I lost anycharacters when the window opened or if I were hitting the space key overand 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 tomake a window non-focusable, but when I did, it was not possible to pressthe "Cancel" button.  I've trashed my code that wasn't working and, like Isaid, gone with a different idea.  However, I'll be looking over your codeto 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 andtest this for you.Hal> 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> > }
0
Reply Hal 1/26/2008 12:33:55 AM

Hal Vaughan wrote:> Mark Space wrote:> 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.
0
Reply Mark 1/26/2008 5:29:56 AM

Mark Space wrote:> Hal Vaughan wrote:>> Mark Space wrote:> >> 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 where I added a sleep() statement to delay it.  I stalled itsomething like 5 seconds so I could click the button, then switch to mytext editor.  The first time I just typed and kept my focus on the texteditor until I realized the window had already opened, then scanned what Ihad typed and there were no missing characters.  The next few times I justkept tapping the space bar since I've seen that trigger JButtons and therewas 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> 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.I tried to set the focusable state, but maybe I did, as you said, have otherstuff from other experiments in the way.  At least now we know your way issimple and works!Hal
0
Reply Hal 1/26/2008 9:37:45 PM
comp.lang.java.programmer 51965 articles. 38 followers. Post

16 Replies
479 Views

Similar Articles

[PageSpeed] 48


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Stop new window from grabbing focus
I'm using Sun's JDS-3 which I believe is based upon gnome 2.6. When a new window is displated it is grabbing the focus, which is really disruptive to current work. Is there a way to disable this so that focus remains in the current window? Thanks. -- Jeffery Small ...

focus on the new window.open window
Up until a few weeks ago, javascript code like window.open("http://mydomain.com", "windowName"); would always bring my new or reused window to the top, with focus. Lately, Firefox (Deer park alpha 2) only brings it to the top if the window is new. If it's being reused, the window does not come to the foreground (with IE 6 it does). Is there a new way in Mozilla/Firefox that I can ensure that this window comes to the top? Thanks, Csaba Gabor from Vienna Csaba Gabor said: > >Up until a few weeks ago, javascript code like > >window.open("http://mydo...

new window focus
does some one know whay this code doesn't works with mozilla: <body onload=\"window.focus();\"> how can i focus this window in both explorer and mozilla browsers? thanks On Oct 1, 11:55 am, Gandalf <goldn...@gmail.com> wrote: > does some one know whay this code doesn't works with mozilla: > > <body onload=\"window.focus();\"> Why the backslashes? Peter On Oct 1, 8:59 pm, Peter Michaux <petermich...@gmail.com> wrote: > On Oct 1, 11:55 am, Gandalf <goldn...@gmail.com> wrote: > > > does some one know whay th...

Prevent a frame from opening a new window
Hi All, I am having a web page with two frames in it. One frame links to a page on which i have hosted a activeX control and the other page links to a CRM Web (salesforce.com). When i click on some tab in the CRM application it opens up in a new window. How can i prevent this? this doesn't happen if the open the CRM in a different page, but when i do it from a frame it opens in a new window. Also is a there a way to refresh the CRM web page? i tried providing a page with a button and added that page to the same frameset as the CRM. when i click on the button i try to refersh the CRM but i...

Prevent gain focus on window open.......
Can you prevent a window gaining focus when it is initially opened. I have a program which i print documents from. When I click the print document button it opens another progress window. Depending on the input it could open this progress window any number of times. The problem been that it takes over the machine. I can't seem to find a way for this mini window not gain focus on openwindow. ie... if i have clicked away from my program to work on a word document or something while the doc is been generated... my program just keeps popping up. Any ideas??? ...

How to make new window not steal focus?
Hi My application has 2 windows forms. I need to create and show the secoud window form but after show form not steal focus ? How to create that form? thx Piter > My application has 2 windows forms. I need to create and show the secoud > window form but after show form not steal focus ? > How to create that form? This NG is about C++, C++ does not have windows (or focus or forms). It is better if you try a news group for your OS (and/or compiler). That said I don't even know what NG to send you to since you do not even identify for which OS you are building this UI. Befor...

How to prevent a new browser window from opening
I'm creating a site for the village where I live: www.nutteln.de. On the "Postermuseum" page, I have a link "Weitere Poster", which should insert new content on the page. However, clicking the link opens a new browser window. How should I amend the code to load new content into the existing page? Thanking you in advance for your help! John On 11 Apr, 09:28, visi...@soon.com wrote: > I have a link "Weitere Poster", which should > insert new content on the page. However, clicking the link opens a new > browser window. How should I amend the co...

changing focus to new window -- 2
I'm still testing version 6, and this problem has been there since the beginning. When I get a new email, at least when it goes to the Inbox, but probably other boxes too, it opens the mailbox if it is closed**, but it doesn't change focus to the newly opened box. Shouldn't focus change to the inbox if I am looking at the outbox, especially if the inbox was closed and the incoming mail opens it? **Set in Options/Getting atttention. Meirman If emailing, please let me know whether or not you are posting the same letter. Change domain to erols.com, if necess...

sawfish: how to make new window not steal focus?
I'm setting sawfish so that new window is not focused by default. That way when I launch applications I can still type on my current window (usually terminal) without being interrupted because the focus is 'stolen' by the new window popping up. However, if the new window happens to surround the mouse cursor at the current position, the focus is stolen still. How do I prevent this? -- Pascal Pascal Damian wrote: > I'm setting sawfish... > How do I prevent this? Asking somewhere other than comp.windows.x.kde might be a start. Daev -- Faculty of Divinity, University...

Solaris 10 metacity focus on new windows
I'm running a stock Solaris 10 system using JDS (Gnome). It all works very nicely using focus-follows-mouse mode. But one annoyance is that whenever a window pops up it steals keyboard focus, even though I have focus set to follow the mouse pointer. Googling for this issue has given some hints but none of them work. Is there some way to disable this? It's really, really annoying when I'm typing something in and a new application starts and steals focus. Ray In <sxdlk03w4tt.fsf@rtp.ericsson.se> raymond.toy@ericsson.com (Raymond Toy (RT/EUS)) writes: >I'm runni...

sawfish: How to make new window not steal focus?
I'm setting sawfish so that new window is not focused by default. That way when I launch applications I can still type on my current window (usually terminal) without being interrupted because the focus is 'stolen' by the new window popping up. However, if the new window happens to surround the mouse cursor at the current position, the focus is stolen still. How do I prevent this (aside from stacking my current window in the upper layer)? -- Pascal ...

New borderless window causes other to lose focus.
Hi all, I've got an application with two windows that I created using CreateWindowEx. The main window creates the second upon receiving a mouse-down message. The second is borderless and not a child window -- it's a popup menu and can be larger than the main window. I created the second with WS_EX_TOOLWINDOW for my extended style and WS_POPUP for my regular style. But I'm having this problem that when the app creates the 2nd window, the main window loses focus i.e. the title bar color lightens. Both windows have the same window class. When I create the 2n...

OT: What in IE can prevent Javascript from opening new window?
Hi, This is a little off-topic, because I'm really trying to find out why my IE6 is messed up. I started noticing that my IE6 seems to be working (or not working) strangely today. One thing is that when I got to a website that and click on something (e.g., a link or button) that normally opens a new window, nothing happens. Finally, when I try to go to Windows Update, I get to the page where it's asking to install a new installer, I click "Install Now", and then get a page that says "Windows Update is downloading and installing the updated software", then ...

don't lose focus when showing new window
hy, is there a possibility to supress losing focus after a new window is shown? Within my win32-c++-program I access a .NET-form (via com callable wrapper) and call the form's Show() method. This forces the .NET-form to be shown, and immediately afterwards it gains focus. But I want to keep the focus on the win32-window that currently had the focus - is there a way for me to do that, maybe to react on WM_KILLFOCUS and send any appropriate message? thx in advance, ekim "Ekim" <the.newsletter@gmx.net> wrote in message news:2ou8jjFe2hq4U1@uni-berlin.d...

Can i change focus stealing prevention level of window manager in Tcl ?
Can i change focus stealing prevention level of window manager in Tcl ? On 4 Apr., 10:11, Saurabh Srivastava <mr.saurabh.srivast...@gmail.com> wrote: > Can i change focus stealing prevention level of window manager in > Tcl ? I am sorry, for me the question is translated by Google translator and does not make much sense... But I am not a native speaker... change focus ? There are two tcl/tk commands: raise <toplevel windowpath> focus <widget windowpath> The first may be used with toplevel windows, the second to choose the focus window on a toplevel window. E...

Eudora grabs the "focus" whenever it checks for new mail...
I was in a previous discussion on this same issue last year, but got no answer to my question. I've just upgraded to Eudora 6.1 (paid) hoping this focus problem had been addressed. Apparently, that hasn't happened yet... I set my Tools|Options|Checking Mail to check mail every 30 minutes and when Eudora performs that mail check, it will pull my focus from IE6 or Netscape or any other software I'm using at the time, back to the Eudora screen to focus on Eudora. This is annoying; I don't want to see all my messages downloading 20 times a day! Does anyone understan...

New Tkinter windows don't get focus on OS X
If you try something like: $ python -m Tkinter -c 'Tkinter._test()' in Terminal on OS X, you'll notice that the window that is spawned does not get focus, rather focus remains in Terminal. Furthermore, if you hit Command+Tab to switch focus to the Python process, you'll notice that for some reason Python has been placed last in the focus order, even though it was just spawned by Terminal. This is undesirable if you're e.g. making a game where stuff happens as soon as the window is spawned and the player is expected to respond quickly but can't until she Command+Tabs o...

preventing "auto focus" (automatically comming to the fore/background) between multiple windows in PS (CS2)?
Hi, I recently installed CS2 on my computer and overall it has been quite nice but there is one thing that is really bugging me. When i have multiple photos open photos come to the foreground when my mouse passes over them. I have been trying to figure out how to make it so they only come to the foreground when i click on them (or cycle through them with ctl+tab). Any help in doing this would really be appreciated! Cheers -Gaiko gaikokujinkyofusho@gmail.com wrote: > Hi, I recently installed CS2 on my computer and overall it has been > quite nice but there is one thing that is really ...

Pre-Xmas phone numbers: Apple slips, Windows Phone grabs 1 in 10 new sales
http://www.channelregister.co.uk/2014/01/08/prexmas_phone_numbers_see_apple_slip_windows_steady/ -- MFB On 01-08-14 4:34 PM, Flint wrote: > http://www.channelregister.co.uk/2014/01/08/prexmas_phone_numbers_see_apple_slip_windows_steady/ > Best move MS made was to purchase Nokia :) On 2014-01-08 22:19:55 +0000, Nashton said: > On 01-08-14 4:34 PM, Flint wrote: >> http://www.channelregister.co.uk/2014/01/08/prexmas_phone_numbers_see_apple_slip_windows_steady/ >> >> > > Best move MS made was to purchase Nokia :) I agree with you there Na...

Spawning new windows which do a chdir(): How either prevent spawning > 1 (semaphore?) or avoid CWD conflict?
Hi all, I've got the following problem, and searches in various Perl manpages, = modules and via Google didn't yield a solution: OS: Windows 2000 Perl: 5.8.0 (Native build using MS Visual C++ 6.0) Perl/Tk: 800.024 I have a main window (Tk::MainWindow->new) which spawns a new subscreen = ($Main->Toplevel) each time I click on a certain button on the main window (this happens = in the callback (closure) associated with the corresponding button). Each of the subscreens starts a process in which I need to "chdir()" to = some direc...

this.window.focus() vs. window.focus() vs. this.focus()
Hi, I am confused about the differences between this.window.focus(), window.focus(), and this.focus(). I want to use the calls in a <body onload="..."> tag. What are the differences between these forms that may make one succeed and another fail? In particular, this.window.focus() fails in Opera 9.10 with an "object not found", and windows.focus() succeeds in Opera 9.10, Firefox 2.02, and IE 7. Roger On Mar 8, 3:32 pm, Roger <crosseyedpeng...@cox.net> wrote: > Hi, > > I am confused about the differences between this.window.focus(), > window...

MaVerick Software Foundation Announces New Members, New Website, New Features, New Name, New Focus
Hello, We are happy to announce that Manuel Goyenechea of Servertec, www.servertec.com and Herb Rubin of Pathfinders, www.pfinders.com have joined the MaVerick Development Team. We have just redone our website's look-and-feel so that it is easier to navigate. We have added phpBB online forums, phpCollab task management system, Mantis for bug tracking system and FAQ Frenzy. We have changed the name of the project from Maverick DBMS to Maverick Application Server because Maverick is not just a database management system, it is a complete Application Server with a buil...

Window.Open get new tab instead of new window in FireFox
I generated image map HTML in DreamWeaver CS3 graphically (not hand coded). <area shape="rect" coords="613,242,686,295" href="#" alt="Australia Alt Energy Fact icon" onmouseover="javascript:window.open(&quot;popups/ australia_popup.html;'Australia Alt Energy Fact',width=700px;height=400px,toolbar=no,Menubar=no,status=yes,scrollbars=no,resizable=no&quot;);" / > I'm getting a new tab instead of a new Browser (firefox) window. Am I missing an attribute in window.open() call? What do I need. F.z. On Feb 29, 12:58 pm,...

How to post the data to a new window and control the new window's property?
When we submit the form data to another page, we usually do the following: <form action="display.asp" method="post"> will submit the form data and open display.asp in the current browser <form action="display.asp" method="post" target="_blank"> will submit the form data and open display.asp in a new browser Now, what I want is to open display.asp in a new browser, but control the window's size, and disable the status bar, title bar, and address bar of a new browser. If I do this, it can only open a html page, but not post t...