Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Where is a focus?  RSS feed

 
Yehor Mikhaylov
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All.
Is there any way to detect the component that has focus?
I don't want to use listeners. Maybe there is something like this:
JComponent Xxxxx.getActiveComponent() ???
Please help.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hasFocus() returns a boolean. Try looking at the API. The answer's to these types of questions are there.
 
Yehor Mikhaylov
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are methods that shows is the component has focus and which component will have focus next/prev. And that's all.
I think there must be method(maybe static) that returns reference to the component that has focus now.
But I can't find it in specification.
Please help.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you don't want to use a Listener? I don't think so. That's what Focus Listeners are for in part. So you can manipulate an object that gains/loses focus.
Why don't you want to use a Listener? Is it because you don't know how or what? We can help if this is the case.
And if it is not in the API, it's not there. Sun won't hide any methods from you, however, it could be buried in a parent component. But I think for what you are wanting, you will need to use a FocusListener.
 
Yehor Mikhaylov
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I've already done it with listeners. I was making something like statusbar and I had to "register" all components in it. Now I want to do it without "registering".
But I begin to think that it's impossible.
In winapi there is a method that points to the active component. Why java hasn't it? ((
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummm... because Java is actually object oriented and doesn't provide "quick fix" shortcut methods that break code down the road... (Sorry... that's just a cheap shot at M$... ) I think it's really that Java follows it's listener event model pretty strictly and it wouldn't want to provide blatent workarounds to it like that.

However, it would actually be pretty simple for you to do this yourself...

Extend JPanel or whatever container you are using. Add a method called getFocusedComponent() or something that returns a Component. Override the add() methods to do something like this...



Have myFocusListener be an inner class of your JPanel that just listens for focusGained() events and set the focusedComponent tracked by your JPanel to the source of the focus event.

Voila!
 
Yehor Mikhaylov
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for attention to me.
But I don't understand why Sun programmers didn't register such ability in JWindow.add???
I understand that it's easy to use FocusListener.
But to have something like

easier.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. That would be easier. But you know what would be easier than questioning why Sun didn't make things easier on you? Do what Nathan suggested, and just re-use the code everytime you need to do it again. Then you can feel good about implementing reusable code.
 
Yehor Mikhaylov
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Gregg and Nathan.
It's agreeably to me that You answered my answers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!