Java Netbeans - 在 jComboBox 中输入项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1542081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Netbeans - Entering items in a jComboBox
提问by Yatendra Goel
I have generated a GUI from netbeans in which I have placed a combobox too.
我已经从 netbeans 生成了一个 GUI,我也在其中放置了一个组合框。
By default, the items in combobox are item1, item2, item3, item4.
默认情况下,组合框中的项目为 item1、item2、item3、item4。
But I want my own items. Netbeans doesn't allow editing generated code so how can i edit the comnbobox according to me.
但我想要我自己的东西。Netbeans 不允许编辑生成的代码,所以我如何根据我编辑组合框。
Note: I know one method by editing the "model" property of that jComboBox but I don't want to do it like that because I want various items (which are in an array) in that jComboBox so I want to pass that array in that jComboBox like as follows:
注意:我通过编辑该 jComboBox 的“模型”属性知道一种方法,但我不想这样做,因为我想要该 jComboBox 中的各种项目(位于数组中),所以我想将该数组传入jComboBox 如下所示:
jComboBox2 = new javax.swing.JComboBox();
String [] date = new String[31];
for(int i = 0; i < 31; i++) {
date[i] = i + 1;
}
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(date));
回答by blurec
you can inject your code by using "custom code" feature in the GUI editor for the "model" of combobox
您可以使用 GUI 编辑器中的“自定义代码”功能为组合框的“模型”注入代码
回答by Devon_C_Miller
There are 2 approaches I am aware of:
我知道有两种方法:
Simple approach - After the call to
initComponents()
in the constructor add you code to build your model and calljComboBox2.setModel(myModel)
to set it. So the constructor would look something like:public SomeClass() { initComponents(); String [] date = new String[31]; for(int i = 0; i < 31; i++) { date[i] = i + 1; } jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(date)); }
Complex approach - add a readable property that holds the desired model. For example:
private ComboBoxModel getComboBoxModel() { String[] items = {"Item A", "Item B", "Item C"}; return new DefaultComboBoxModel(items); }
Then, in the jComboBox2 property sheet, click the button to edit the model.
In the editor panel change the dropdown from
Combo Box Model Editor
toValue from existing component
.Select
Property
. Choose the comboBoxModel property. Click OK
简单的方法 - 在
initComponents()
构造函数中调用 之后,添加代码来构建模型并调用jComboBox2.setModel(myModel)
来设置它。所以构造函数看起来像:public SomeClass() { initComponents(); String [] date = new String[31]; for(int i = 0; i < 31; i++) { date[i] = i + 1; } jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(date)); }
复杂的方法 - 添加一个可读的属性来保存所需的模型。例如:
private ComboBoxModel getComboBoxModel() { String[] items = {"Item A", "Item B", "Item C"}; return new DefaultComboBoxModel(items); }
然后,在 jComboBox2 属性表中,单击按钮以编辑模型。
在编辑器面板中,将下拉列表从 更改
Combo Box Model Editor
为Value from existing component
。选择
Property
。选择 comboBoxModel 属性。单击确定
I tried the second way once. Never really used it again. Too much work, no real much gain. Plus it displays an empty combo box in the designer which just makes layout harder.
第二种方法我试过一次。再也没有真正使用过它。太多的工作,没有真正的收获。此外,它会在设计器中显示一个空的组合框,这只会使布局更加困难。
I use the first approach, plus use NetBean's model editor to supply some representative values for the model. That gives me the sensible size behavior in the designer at the cost of one unnecessary line in initComments()
.
我使用第一种方法,并使用 NetBean 的模型编辑器为模型提供一些有代表性的值。这给了我设计器中合理的尺寸行为,但代价是在initComments()
.
回答by tmendo
Completing blurec answer (I cannot comment yet), in the GUI editor select the comboxbox, go properties, then model, then hit the three dots. Then select Custome Code and add your code, for instance:
完成 blurec 答案(我还不能评论),在 GUI 编辑器中选择组合框,转到属性,然后模型,然后点击三个点。然后选择客户代码并添加您的代码,例如:
new DefaultComboBoxModel<>(functionThatReturnsAnStringArray())
回答by Omari Victor Omosa
回答by Hemantha
public NewJFrame() {
initComponents();
reformatComboBox();
}
private void reformatComboBox() {
JComboBoxName.removeAllItems();
JComboBoxName.addItem("item1");
JComboBoxName.addItem("item2");
}
回答by Dorian-Catalin Badirca
For the posterity:
为后人:
Right click the ComboBox and select Customize Code. Here at the comboBox.setModel, in the left select custom property. After new String, add your values in the following form:
右键单击 ComboBox 并选择Customize Code。在 comboBox.setModel 的左侧,选择custom property。在 new String 之后,按以下形式添加您的值:
Value 1: Integer.toString(myInt1) Value 2: Integer.toString(myInt2)
值 1:Integer.toString(myInt1) 值 2:Integer.toString(myInt2)
If your variables are int of course. If not just put the String variable and you are done.
如果你的变量当然是 int 。如果不只是把字符串变量,你就完成了。
Hope it helps.
希望能帮助到你。