Preventing A New Window From Grabbing Focus

Discussion in 'Java' started by Hal Vaughan, Jan 8, 2008.

  1. Hal Vaughan

    Hal Vaughan Guest

    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
     
    Hal Vaughan, Jan 8, 2008
    #1
    1. Advertising

  2. 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).

    /gordon

    --
     
    Gordon Beaton, Jan 8, 2008
    #2
    1. Advertising

  3. Hal Vaughan

    Hal Vaughan Guest

    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.

    Hal
     
    Hal Vaughan, Jan 8, 2008
    #3
  4. Hal Vaughan

    Jason Cavett Guest

    On Jan 8, 3:09 am, Hal Vaughan <> 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.
    >
    > 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.
     
    Jason Cavett, Jan 8, 2008
    #4
  5. Hal Vaughan

    Mark Space Guest

    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
    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, Jan 8, 2008
    #5
  6. Hal Vaughan

    Mark Space Guest

    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.
     
    Mark Space, Jan 8, 2008
    #6
  7. Hal Vaughan

    Hal Vaughan Guest

    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, 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
     
    Hal Vaughan, Jan 8, 2008
    #7
  8. Hal Vaughan

    Mark Space Guest

    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.
     
    Mark Space, Jan 8, 2008
    #8
  9. Hal Vaughan

    Lew Guest

    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
     
    Lew, Jan 9, 2008
    #9
  10. Hal Vaughan

    Hal Vaughan Guest

    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 may
    need to cancel the operation. If the window can't get focus, the user
    can't click the cancel button.

    Hal
     
    Hal Vaughan, Jan 9, 2008
    #10
  11. Hal Vaughan

    Hal Vaughan Guest

    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 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, Jan 9, 2008
    #11
  12. Hal Vaughan

    Mark Space Guest

    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

    }
     
    Mark Space, Jan 9, 2008
    #12
  13. Hal Vaughan

    Hal Vaughan Guest

    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!
    >
    > 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
     
    Hal Vaughan, Jan 19, 2008
    #13
  14. Hal Vaughan

    Hal Vaughan Guest

    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
    >
    > }
     
    Hal Vaughan, Jan 25, 2008
    #14
  15. Hal Vaughan

    Hal Vaughan Guest

    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 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;
    >
    > 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, Jan 26, 2008
    #15
  16. Hal Vaughan

    Mark Space Guest

    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.
     
    Mark Space, Jan 26, 2008
    #16
  17. Hal Vaughan

    Hal Vaughan Guest

    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 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
    > 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 other
    stuff from other experiments in the way. At least now we know your way is
    simple and works!

    Hal
     
    Hal Vaughan, Jan 26, 2008
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fernie
    Replies:
    2
    Views:
    2,369
    Fernie
    Nov 19, 2004
  2. Jason
    Replies:
    4
    Views:
    1,644
    Jason
    May 7, 2007
  3. Csaba Gabor

    focus on the new window.open window

    Csaba Gabor, Nov 30, 2005, in forum: Javascript
    Replies:
    4
    Views:
    220
    Csaba Gabor
    Dec 1, 2005
  4. Roger
    Replies:
    3
    Views:
    345
  5. Spizzat2
    Replies:
    2
    Views:
    189
    Spizzat2
    Jan 9, 2008
Loading...

Share This Page