5

I'm implementing a specialized version of JTextField and want to pack it into an own bean. The bean works pretty fine so far. Here's my question:

How can I detect when the JTextField is shown? So that I can init some graphical stuff everytime the fields are shown.

The ShowComponent event does not work for me. FocusGained and FocusLost works just fine.

Hope there's somebody who knows the solution to this.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
salocinx
  • 3,715
  • 8
  • 61
  • 110

2 Answers2

4

Can you use the isShowing() method? http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#isShowing%28%29

Atif
  • 942
  • 1
  • 6
  • 19
  • or the addComponentListener(...) to track changes in visibility – alex Feb 09 '12 at 00:14
  • @alex - mostly doesn't help: a componentEvent is fired when the state of the visible property changes. As that property is true by default, it is not fired when we intuitively expect it :-) – kleopatra Feb 10 '12 at 10:32
2

While using a ComponentListener and doing stuff in its componentShown intuitively seems to be the way to go, it doesn't help: it fires when the component's visible property is changed. That property is true by default (even if not yet added to any container showing on the screen), and consequently nothing fired on showing ..

Instead, use an AncestorListener and do what's needed in its ancestorAdded.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • +1 excluding ---> please question, for why reason on this world you suggested add Ancestor to the J(Formatted)TextField – mKorbel Feb 10 '12 at 11:46
  • @mKorbel don't understand what you mean: the question (as I understood it, could be wrong) is to get notified when the field is shown (aka: added) – kleopatra Feb 10 '12 at 12:28
  • hmmm thank you, I've to try what will be happends add J(Formatted)TextField to the un_visible Container before, then to dispay this Container – mKorbel Feb 10 '12 at 12:37
  • there is no such thing as an _unvisible_ (as of: its visible property == false) container - that's the whole point :-) What you need is _showing_ (which is a compound property, see @Atif 's answer) – kleopatra Feb 10 '12 at 12:53
  • very off_topic that's very nice is that SwingX_.... broken *** with NetBeans platform, I have to tested update for this plugin, thank all :-) – mKorbel Feb 10 '12 at 13:04