using editable jcombobox

  • Thread starter Jacques Chaurette
  • Start date
J

Jacques Chaurette

Hello all, I want to use an editable jcombobox to save space and provide a
list of values. When the user changes a value I want this value to be
incorporated into the jcombobox items in its proper location, how can this
be done?

Thanks for any help?

Jacques
 
R

Roedy Green

Hello all, I want to use an editable jcombobox to save space and provide a
list of values. When the user changes a value I want this value to be
incorporated into the jcombobox items in its proper location, how can this
be done?

There might be a better way than this, but here is a crude method.

In your action listener, do a getItemCount. If it has grown,
enumerate all the choices with getItemAt, sort them and reload the
choices, and reset the selection, and repaint.
 
J

Jacques Chaurette

Hi Roede, are you saying that if I edit one item this will change the count?

Jacques
 
R

Roedy Green

Hi Roede, are you saying that if I edit one item this will change the count?

I have never used the feature. I avoided it since it struck me an
cavalier to allow ANY writein, and how does it get remembered as an
official choice, or does it? It was just too murky.

Perhaps it does not add the choice to the list. I suppose when you
get a write in, you can detect that by comparing it against all
choices, and then add it if there is not some more direct way.
 
J

Jacques Chaurette

Do you have another recommendation as to how to achieve the goal?

Jacques
 
A

Alan Krueger

Jacques said:
Hello all, I want to use an editable jcombobox to save space and provide a
list of values. When the user changes a value I want this value to be
incorporated into the jcombobox items in its proper location, how can this
be done?

Like most of Swing, what's shown is a reflection of the model the view
is displaying. You need to add the item to the underlying model.

Add an item listener to notice when an item is selected. Use a mutable
combo box model and add the item to the model if it's not already there.

I believe the default combo box model is mutable and you should be able
to call addItem on the JComboBox control directly to add the item to the
underlying model.
 
R

Roedy Green

Do you have another recommendation as to how to achieve the goal?

Sorry no.

I have a general approach though you might try.

At first I am in information gathering mode. I mostly just want to
figure out how the tools work. So I trace, dump, look at source code,
packet sniff, write little throw-away apps that I keep changing until
I am fairly sure I understand the general lay of the land and what the
beasts' capabilities are.

I hate black boxes. I have a mental block against using something I
no model at all of what goes on inside. Once I get a reasonable,
though possibly false, internal model, then I can predict how it will
behave, rather than memorising specs and what appear to be irrational
behaviours.

Then I have a coffee or take a small break. For a real toughie, I go
outside for a walk. It is important NOT to have paper or computer
nearby. Then I compose the overall strategy to solve the problem,
without letting myself be overly concerned with precise details. The
thing I often come up with is an animation in my head of how it will
work under the hood. Objects look like ghostly rafts. Inner class
objects are like bulbous appendages.

Then I sit down and code. It just pours out pretty well as fast as I
can type. As long as my mental model is clear, the job is essentially
complete except for cleaning up syntax errors and various
miscellaneous gotchas.

If I that approach does not succeed, then I do a bottom up approach. I
start mentally designing tools that I think will likely be useful in
the solution. I sometimes actually code them. The advantage is it
stops my brain from nattering over details, freeing it to see the
bigger picture. Further it gives me abstraction tools to think about
the big picture.

There is a panic that sets in if I think I have bitten off a
programming task over my head. Just writing some simple code helps
reassure myself that the job is not impossible. The panic of course
creates a self-fulfilling prophesy. You can't solve difficult
problems when your mind is dwelling on how horrible it would be to
utterly fail.
 
Joined
Sep 6, 2007
Messages
1
Reaction score
0
You can use action listener as mentioned below.Add listener to combobox and use DefaultComboBoxModel because it is a mutable model.
public void actionPerformed(ActionEvent e)
{
JComboBox anCombo = (JComboBox) e.getSource();
DefaultComboBoxModel comboModel = (DefaultComboBoxModel) anCombo.getModel();
int index = comboModel.getIndexOf(anCombo.getSelectedItem());
if(index == -1)
{
comboModel.addElement(anCombo.getSelectedItem());
}//else already added
}
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top