如何在运行时将项目插入jcombobox并保存 [英] How to insert items to a jcombobox at runtime and save it

查看:82
本文介绍了如何在运行时将项目插入jcombobox并保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时将值保存在我的jcombobox中。我想做的是单击按钮后,将其设置为editable = true。然后在组合框中输入值,但不会保存。

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.

推荐答案


  • 因为可以从 DefaultComboBoxModel 添加 / 将项目删除到/ / code>放在 JComboBox 的底下,可以从外部进行相同的操作(默认情况下)。

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

      您必须使用 MutableComboBoxMode 添加 / 从/删除项目 JComboBox 会触发本身的事件(view_to_model)。

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

      <$ c $的例子很好c> MutableComboBoxModel @Robin 此处此处

      >获得更好的帮助尽快发布SSCCE ,以供将来的读者使用,否则搜索扩展AbstractListModel实现MutableComboBoxModel

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

      这篇关于如何在运行时将项目插入jcombobox并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆