wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes Highlighting TextField, JList, etc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Highlighting TextField, JList, etc" Watch "Highlighting TextField, JList, etc" New topic
Author

Highlighting TextField, JList, etc

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Hmm, how to phrase this question?
In our screen, whenever you tab into a field the cursor just blinks there. It is sometimes difficult to determine where the cursor is on the screen. In most programs, the field that has focus is a different background color. We have tried selectAll(), but this only selects the text that is in the field, if there is no text it selects nothing.
I have one Idea, which is to create a FocusAdaptorFactory, that will pass a FocusAdaptor for the type of JComponent that it is, that Adaptor will change the background color in the FocusGained, and back to the Original in the FocusLost. However, I think this is a lot of work for simply making the user interface readable to the user.
:Confused:
Thanks
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I get so much good experience searching the API on JavaRanch!!!

In MyTextField

And in a class that uses it:

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Yeah, that is pretty much what I expected. I used VCafe and changed the BackgroundColor, and it works. But I just find that messy, and what if you really want to use the focusGained, focusLost for other purposes for the JTextField. Like one validates a date, one makes sure the correct status is entered, etc. Becuase you used up the Focus methods you have to do more inheritence, and calling super. If it was a decorator of some sort it would be really cool.
Thanks for your help
Mark
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

This is totally off the top of my head, but what about re-throwing the event?

I'm at home now, and I can't try out this idea.

Is it possible to subclass JTextField, have it implement FocusListener, consume a focusGained/Lost event, and then have the component re-throw the event, so that classes that use this subclassed component can also 'see' the event??

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

So what I did to solve this was to create a FocusAdapterFactory class that would take the kind of component it is, like a JTextField, JList, and JComboBox, it would return a subclass of FocusAdapter that I made that would change the background, and in the case of the JList, change Background and SelectionForeground colors.
And since I realized that there can be many Focus Listeners, that all I need to put in there was changing colors.
Thanks for the help
Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Highlighting TextField, JList, etc
 
Similar Threads
How to remember background color in a form field
JTextField not refreshing
background color in a form field
Blur the field
Changing textbox background color in java ME