3

I need to save the values in my jcombobox at the runtime. What I am trying to do is after clicking on a button, I am setting it to editable = true. Then type the value in the combobox, but it doesn't save.

private void btadbknameActionPerformed(java.awt.event.ActionEvent evt) {
  if(evt.getSource()== btadbkname){
    cb_bkname.setEditable(true);
    cb_bkname.getText();
    cb_bkname.addItem(evt);
  }else{
    cb_bkname.setEditable(false);
  }
}

I have already added some elements in it on the designing level, but it's limited if some random value comes then its a problem.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Sumeet Gavhale
  • 800
  • 4
  • 13
  • 39
  • 2
    What do you mean by "it doesn't save"? Do you mean that you're not seeing the ActionEvent.toString() representation in the combobox? Or that you are seeing a new item in the JComboBox, but it doesn't persist if you close the program and then re-open it? – Hovercraft Full Of Eels Jan 13 '13 at 20:08
  • no at the runtime if i enter some random value other than what i have set previously then that is not adding in the combobox list. i need to add some value to the combobox list when i click on the button. i dont have much knowledge of progrmaing what i know i have tried but it dosent work – Sumeet Gavhale Jan 13 '13 at 20:12
  • 5
    I second the recommendation that you will want to review your previous questions and accept more of them -- we greatly appreciate that, but I'm not sure how we can help based on such limited information. Consider creating and posting a small compilable and runnable program (please no NetBeans-generated code) that demonstrates your problem, and I'll bet we can help better, an [SSCCE](http://sscce.org). Note that JComboBoxes don't even have a `getText()` method, so if cb_bkname is your JComboBox, I'm surprised if this code even compiles. – Hovercraft Full Of Eels Jan 13 '13 at 20:15

3 Answers3

3
  • Because it is possible to add / remove Item(s) to / from the DefaultComboBoxModel underlaying the JComboBox, the same action (by default) is possible from outside.

  • You have to use MutableComboBoxMode to add / remove Item(s) to / from JComboBox that fires event from itself (view_to_model).

  • There are excellent examples of MutableComboBoxModel by @Robin here and here.

  • For better help sooner post an SSCCE, for future readers, otherwise search for extends AbstractListModel implements MutableComboBoxModel.

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

it can't possibly work the way you're trying it.

the comboBox has to be editable before you click the button then you just need this line

cb_bkname.addItem(((JTextField)cb_bkname.getEditor().getEditorComponent()).getText());
Michael Dunn
  • 818
  • 5
  • 3
1

Try this

private void btadbknameActionPerformed(java.awt.event.ActionEvent evt) {
      if(evt.getSource()== btadbkname){
        cb_bkname.setEditable(true);
        String newItem=cb_bkname.getText();
        cb_bkname.addItem(newItem);
      }else{
        cb_bkname.setEditable(false);
      }
    }