Highlight a text in a JComboBox

B

bruce

I have an editable JComboBox that I initialize using distinct values
from a database. I pre-select, in this JComboBox, the last value that
was entered into the database. This all works fine.

The client tabs through the fields on the form until this JComboBox is
reached. My problem is that I would like the text in this JComboBox to
be highlighted when it gets the focus.

Can someone show me how to do this, or point me to tutorial that does
this?

Thank you...

Bruce
 
J

John B. Matthews

bruce said:
I have an editable JComboBox that I initialize using distinct values
from a database. I pre-select, in this JComboBox, the last value that
was entered into the database. This all works fine.

The client tabs through the fields on the form until this JComboBox is
reached. My problem is that I would like the text in this JComboBox to
be highlighted when it gets the focus.

Can someone show me how to do this, or point me to tutorial that does
this?

Have you tried combo.getEditor().selectAll()? This appears to be the
default in Mac's Aqua L&F; but if need be, you can always extend
BasicComboBoxEditor.
 
M

markspace

The client tabs through the fields on the form until this JComboBox is
reached. My problem is that I would like the text in this JComboBox to
be highlighted when it gets the focus.


What look and feel are you using?

For me, when I tab to a JComboBox, the text "highlights" by getting a
dotted box drawn around it (same as for buttons). I think that's the
default Java GUI L&F (Metal?), not the Windows one. Regardless, you
should probably let the L&F handle it, since changing it will mean it
won't look right for some L&Fs.

And if you're certain you need to hard code a behavior, highlight how?
Like I say, my L&F already does highlight, so... not sure what to do here.
 
B

bruce

Have you tried combo.getEditor().selectAll()? This appears to be the
default in Mac's Aqua L&F; but if need be, you can always extend
BasicComboBoxEditor.

I tried cboMyCombo.getEditor().selectAll() with NO Joy!!

Where can I find out how to extend BasicComboBoxEditor?

As for L&F, I'm not sure what I am using.. NetBeans default is all I
know..

Thanks for the help..

Bruce
 
B

bruce

What look and feel are you using?

For me, when I tab to a JComboBox, the text "highlights" by getting a
dotted box drawn around it (same as for buttons).  I think that's the
default Java GUI L&F (Metal?), not the Windows one.  Regardless, you
should probably let the L&F handle it, since changing it will mean it
won't look right for some L&Fs.

And if you're certain you need to hard code a behavior, highlight how?
Like I say, my L&F already does highlight, so... not sure what to do here..

I have no idea what L&F I'm using. It's whatever is the NetBeans
default.

I can get the JComboBox to highlight when the box is non-editable.
When I turn editable on, I lose the highlighting.

I'll try some different L&F and see what happens..

Thanks for the help..

Bruce
 
J

John B. Matthews

bruce said:
I tried cboMyCombo.getEditor().selectAll() with NO Joy!!

I'm surprised; but my platform's UI does this by default, so I may not
Where can I find out how to extend BasicComboBoxEditor?

Here's an example of extending a class to override a method; it may
suggest the general approach:

<http://groups.google.com/group/comp.lang.java.programmer/msg/92253e51cff619b8>

For something more specific, I'd search for "implements ComboBoxEditor"
or "extends BasicComboBoxEditor". Note in particular how the class
BasicComboBoxEditor does the former. NetBeans should be able to let you
browse the source.
As for L&F, I'm not sure what I am using. NetBeans default is all I
know.

The UIManager API is a good starting point:

<http://download.oracle.com/javase/6/docs/api/javax/swing/UIManager.html>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top