using editable jcombobox

Discussion in 'Java' started by Jacques Chaurette, Oct 2, 2005.

  1. 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
     
    Jacques Chaurette, Oct 2, 2005
    #1
    1. Advertising

  2. Jacques Chaurette

    Roedy Green Guest

    On Sun, 2 Oct 2005 07:26:01 -0400, "Jacques Chaurette"
    <> 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.
     
    Roedy Green, Oct 2, 2005
    #2
    1. Advertising

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

    Jacques

    "Roedy Green" <> wrote in
    message news:...
    > On Sun, 2 Oct 2005 07:26:01 -0400, "Jacques Chaurette"
    > <> 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.
     
    Jacques Chaurette, Oct 2, 2005
    #3
  4. Jacques Chaurette

    Roedy Green Guest

    On Sun, 2 Oct 2005 12:51:47 -0400, "Jacques Chaurette"
    <> 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.
     
    Roedy Green, Oct 3, 2005
    #4
  5. Do you have another recommendation as to how to achieve the goal?

    Jacques

    "Roedy Green" <> wrote in
    message news:...
    > On Sun, 2 Oct 2005 12:51:47 -0400, "Jacques Chaurette"
    > <> 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.
     
    Jacques Chaurette, Oct 3, 2005
    #5
  6. Jacques Chaurette

    Alan Krueger Guest

    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.
     
    Alan Krueger, Oct 4, 2005
    #6
  7. Jacques Chaurette

    Roedy Green Guest

    On Mon, 3 Oct 2005 07:16:50 -0400, "Jacques Chaurette"
    <> 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.
     
    Roedy Green, Oct 4, 2005
    #7
  8. Jacques Chaurette

    Ravindra Khyalia

    Joined:
    Sep 6, 2007
    Messages:
    1
    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
    }
     
    Ravindra Khyalia, Sep 6, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KLD
    Replies:
    3
    Views:
    1,227
    Tony Morris
    Feb 15, 2004
  2. Guest

    Editable JComboBox

    Guest, Feb 16, 2005, in forum: Java
    Replies:
    0
    Views:
    531
    Guest
    Feb 16, 2005
  3. Allen

    Editable JComboBox problems

    Allen, Oct 16, 2006, in forum: Java
    Replies:
    0
    Views:
    329
    Allen
    Oct 16, 2006
  4. @CL
    Replies:
    1
    Views:
    498
  5. Ben Phillips

    JComboBox: setting the editable text

    Ben Phillips, Oct 8, 2008, in forum: Java
    Replies:
    8
    Views:
    7,883
    Ben Phillips
    Oct 9, 2008
Loading...

Share This Page