如何在运行时将项目插入jcombobox并保存 [英] How to insert items to a jcombobox at runtime and save it
问题描述
我需要在运行时将值保存在我的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 theDefaultComboBoxModel
underlaying theJComboBox
, the same action (by default) is possible from outside.您必须使用
MutableComboBoxMode
到添加
/从/删除项目
JComboBox
会触发本身的事件(view_to_model)。You have to use
MutableComboBoxMode
toadd
/remove Item
(s) to / fromJComboBox
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屋!