Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > using editable jcombobox

Reply
Thread Tools

using editable jcombobox

 
 
Jacques Chaurette
Guest
Posts: n/a
 
      10-02-2005
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


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-02-2005
On Sun, 2 Oct 2005 07:26:01 -0400, "Jacques Chaurette"
<(E-Mail Removed)> wrote or quoted :

>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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
 
 
 
Jacques Chaurette
Guest
Posts: n/a
 
      10-02-2005
Hi Roede, are you saying that if I edit one item this will change the count?

Jacques

"Roedy Green" <(E-Mail Removed) > wrote in
message news:(E-Mail Removed)...
> On Sun, 2 Oct 2005 07:26:01 -0400, "Jacques Chaurette"
> <(E-Mail Removed)> wrote or quoted :
>
>>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.
> --
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Again taking new Java programming contracts.



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-03-2005
On Sun, 2 Oct 2005 12:51:47 -0400, "Jacques Chaurette"
<(E-Mail Removed)> wrote or quoted :

>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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Jacques Chaurette
Guest
Posts: n/a
 
      10-03-2005
Do you have another recommendation as to how to achieve the goal?

Jacques

"Roedy Green" <(E-Mail Removed) > wrote in
message news:(E-Mail Removed)...
> On Sun, 2 Oct 2005 12:51:47 -0400, "Jacques Chaurette"
> <(E-Mail Removed)> wrote or quoted :
>
>>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.
> --
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Again taking new Java programming contracts.



 
Reply With Quote
 
Alan Krueger
Guest
Posts: n/a
 
      10-04-2005
Jacques Chaurette wrote:
> 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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-04-2005
On Mon, 3 Oct 2005 07:16:50 -0400, "Jacques Chaurette"
<(E-Mail Removed)> wrote or quoted :

>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.






--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Ravindra Khyalia Ravindra Khyalia is offline
Junior Member
Join Date: Sep 2007
Posts: 1
 
      09-06-2007
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
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JComboBox: setting the editable text Ben Phillips Java 8 10-09-2008 07:58 AM
How to change the editable JComboBox's model when starting or ending typed @CL Java 1 11-23-2006 09:14 AM
Editable JComboBox problems Allen Java 0 10-16-2006 01:06 AM
Editable JComboBox Java 0 02-16-2005 01:13 AM
How do I make an Editable JCombobox within a Table Model? KLD Java 3 02-15-2004 03:29 AM



Advertisments