Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1463 > unrolled thread

How to detect focus lost

Started by"clearlight" <clearlight@THRWHITE.remove-dii-this>
First post2011-04-27 15:33 +0000
Last post2011-04-27 15:33 +0000
Articles 4 — 3 participants

Back to article view | Back to comp.lang.java.gui


Contents

  How to detect focus lost "clearlight" <clearlight@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
    Re: How to detect focus l "Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
      Re: How to detect focus l "Jeff Higgins" <jeff.higgins@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
        Re: How to detect focus l "Jeff Higgins" <jeff.higgins@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000

#1463 — How to detect focus lost

From"clearlight" <clearlight@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectHow to detect focus lost
Message-ID<1176780250.432084.322580@o5g2000hsb.googlegroups.com>
  To: comp.lang.java.gui
Hi,

I am new to Java. As a basic learning project, I started writing a no-
frill small general ledger application.

The basic design is a Frame containing a menu and a JTabbedPanel. When
a menu option is selected, a component derived from JPanel containing
the graphical interface for the data entry of the selected option is
created, added to the JTabbedPane component and the corresponding Tab
is selected.

Inside any of this panels, there are JTextFields that have to be
validated (via InputVerifiers).

Now, if focus is inside any of this JTextFields, and I select a new
option from the frame's menu, validation shouldn't be fired.

So far, I have tried to add a FocusListener to the JPanel descendents,
but it doesn't work as I expected: JPanel fires a focusLost event when
focus enters a JTextField contained within the panel itself, and fires
nothing when a menu option is selected.

If I establish a dependency between the main JFrame and the contained
JPanel derivates, I might perhaps design a sort of bidirectional
notification protocol, but I would like to avoid any dependence that
can be avoided.

So, is there a standard event that lets me know when the focus leaves
a JPanel (FocusListener doesn't work, because -at least in my
configuration: NetBeans 5.5, JDK 1.6, WinXP- it fires when a control
within the panel gains focus)?

TIA

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1464 — Re: How to detect focus l

From"Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: How to detect focus l
Message-ID<46248ce3$0$19256$da0feed9@news.zen.co.uk>
In reply to#1463
  To: comp.lang.java.gui
clearlight@cantv.net wrote:
> Hi,
> 
> I am new to Java. As a basic learning project, I started writing a no-
> frill small general ledger application.
> 
> The basic design is a Frame containing a menu and a JTabbedPanel. When
> a menu option is selected, a component derived from JPanel containing
> the graphical interface for the data entry of the selected option is
> created, added to the JTabbedPane component and the corresponding Tab
> is selected.
> 
> Inside any of this panels, there are JTextFields that have to be
> validated (via InputVerifiers).
> 
> Now, if focus is inside any of this JTextFields, and I select a new
> option from the frame's menu, validation shouldn't be fired.
> 
> So far, I have tried to add a FocusListener to the JPanel descendents,
> but it doesn't work as I expected: JPanel fires a focusLost event when
> focus enters a JTextField contained within the panel itself, and fires
> nothing when a menu option is selected.
> 
> If I establish a dependency between the main JFrame and the contained
> JPanel derivates, I might perhaps design a sort of bidirectional
> notification protocol, but I would like to avoid any dependence that
> can be avoided.
> 
> So, is there a standard event that lets me know when the focus leaves
> a JPanel (FocusListener doesn't work, because -at least in my
> configuration: NetBeans 5.5, JDK 1.6, WinXP- it fires when a control
> within the panel gains focus)?
> 

I had a similar problem, an input panel with OK and Cancel buttons. If 
the user is editing a JTextField then clicks Cancel, you don't want the 
validation to kick in. I used this approach:

     cancelButton.setVerifyInputWhenFocusTarget(false);

In your shoes I'd read the docs to see if 
.setVerifyInputWhenFocusTarget(false) can be applied to your JMenuItems

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1472 — Re: How to detect focus l

From"Jeff Higgins" <jeff.higgins@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: How to detect focus l
Message-ID<F_cVh.24$7W2.18@newsfe06.lga>
In reply to#1464
  To: comp.lang.java.gui

Ian Wilson wrote:
> clearlight@cantv.net wrote:
>> Hi,
>>
>> I am new to Java. As a basic learning project, I started writing a no-
>> frill small general ledger application.
>>
>> The basic design is a Frame containing a menu and a JTabbedPanel. When
>> a menu option is selected, a component derived from JPanel containing
>> the graphical interface for the data entry of the selected option is
>> created, added to the JTabbedPane component and the corresponding Tab
>> is selected.
>>
>> Inside any of this panels, there are JTextFields that have to be
>> validated (via InputVerifiers).
>>
>> Now, if focus is inside any of this JTextFields, and I select a new
>> option from the frame's menu, validation shouldn't be fired.
>>
>> So far, I have tried to add a FocusListener to the JPanel descendents,
>> but it doesn't work as I expected: JPanel fires a focusLost event when
>> focus enters a JTextField contained within the panel itself, and fires
>> nothing when a menu option is selected.
>>
>> If I establish a dependency between the main JFrame and the contained
>> JPanel derivates, I might perhaps design a sort of bidirectional
>> notification protocol, but I would like to avoid any dependence that
>> can be avoided.
>>
>> So, is there a standard event that lets me know when the focus leaves
>> a JPanel (FocusListener doesn't work, because -at least in my
>> configuration: NetBeans 5.5, JDK 1.6, WinXP- it fires when a control
>> within the panel gains focus)?
>>


>
> I had a similar problem, an input panel with OK and Cancel buttons. If the 
> user is editing a JTextField then clicks Cancel, you don't want the 
> validation to kick in. I used this approach:
>
>     cancelButton.setVerifyInputWhenFocusTarget(false);
>
> In your shoes I'd read the docs to see if 
> .setVerifyInputWhenFocusTarget(false) can be applied to your JMenuItems


Strange, that JMenu, JMenuBar don't seem to honor
.setVerifyInputWhenFocusTarget(false)

I could only get your suggestion to work using:
f.getRootPane().setVerifyInputWhenFocusTarget(false);
as below. I don't know what implications that may hold.
What about pop-up context menus?

Thanks,
Jeff Higgins

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;

public class TestFocus {

  private static void createGUI() {
    JFrame f = new JFrame();
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Test");
    menu.setMnemonic(KeyEvent.VK_T);
    menuBar.add(menu);

    JTextField textFieldOne = new JTextField ();
    textFieldOne.setInputVerifier(new TestVerifier());
    JTextField textFieldTwo = new JTextField ("TextField2");
    JButton button = new JButton("Test");

    //menu.setVerifyInputWhenFocusTarget(false);
    f.getRootPane().setVerifyInputWhenFocusTarget(false);

    f.setJMenuBar(menuBar);
    f.getContentPane().add (textFieldOne, BorderLayout.NORTH);
    f.getContentPane().add (textFieldTwo, BorderLayout.CENTER);
    f.getContentPane().add (button, BorderLayout.SOUTH);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
  }

  static class TestVerifier extends InputVerifier {
    public boolean verify(JComponent input) {
      JTextField tf = (JTextField) input;
      System.out.println("verify");
      return "pass".equals(tf.getText());
    }
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        createGUI();
      }
    });
  }
}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1489 — Re: How to detect focus l

From"Jeff Higgins" <jeff.higgins@THRWHITE.remove-dii-this>
Date2011-04-27 15:33 +0000
SubjectRe: How to detect focus l
Message-ID<ztvVh.15$x%5.14@newsfe03.lga>
In reply to#1472
  To: comp.lang.java.gui

Jeff Higgins wrote>
> Ian Wilson wrote:
>> clearlight@cantv.net wrote:
>>> Inside any of this panels, there are JTextFields that have to be
>>> validated (via InputVerifiers).
>>>
>>> Now, if focus is inside any of this JTextFields, and I select a new
>>> option from the frame's menu, validation shouldn't be fired.
>>>
>
>>
>> I had a similar problem, an input panel with OK and Cancel buttons. If 
>> the user is editing a JTextField then clicks Cancel, you don't want the 
>> validation to kick in. I used this approach:
>>
>>     cancelButton.setVerifyInputWhenFocusTarget(false);
>>
>> In your shoes I'd read the docs to see if 
>> .setVerifyInputWhenFocusTarget(false) can be applied to your JMenuItems
>
>
> Strange, that JMenu, JMenuBar don't seem to honor
> .setVerifyInputWhenFocusTarget(false)
>
> I could only get your suggestion to work using:
> f.getRootPane().setVerifyInputWhenFocusTarget(false);
> as below. I don't know what implications that may hold.
> What about pop-up context menus?
>
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4403182

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web