简体   繁体   English

如何将数组值加载到jcombobox?

[英]How to load Array values to jcombobox?

I have an array and want to load the values of the to a jcombobox. 我有一个数组,想要将的值加载到jcombobox。 I'm designing the application using Netbeans. 我正在使用Netbeans设计应用程序。 i want something like this in c# 我想要在C#中像这样的东西

Combobox1.datasource=myarray;

I don't want something like this 我不要这样的东西

JComboBox b = new JComboBox(bla bla);

because i'm designing the GUI using the palette. 因为我正在使用调色板设计GUI。

Thanks. 谢谢。

Inside the properties of the combo box in the netbeans properties editor you can set the model that is being used for it. 在netbeans属性编辑器的组合框的属性内,您可以设置用于它的模型。 Look at the options it presents for that model and one of them does what you are looking for. 查看它为该模型提供的选项,其中之一可以满足您的需求。

You can directly give array that contains values while creating JComboBox as - 您可以在创建JComboBox直接给包含值的JComboBox -

 String[] strArray = new String[]{"A","B","C","D","E"};
 JComboBox comboBox = new JComboBox(strArray);

All the values get loaded to combo box on its creation. 所有值将在创建时加载到组合框。

As you are creating JComboBox using Palette then you can edit the creation by using option - 使用Palette创建JComboBox时,可以使用以下选项编辑创建-

  • Select the JComboBox added to your frame or panel. 选择添加到框架或面板的JComboBox。
  • Choose combo box component and select right click option as " Customize Code.. " 选择组合框组件,然后选择右键单击选项作为“ 自定义代码”。
  • Here "Code Customizer" opened, choose first option from combo as " Custom Creation " from left side. 在此处打开“代码定制器”,从左侧选择组合中的第一个选项作为“ 定制创建 ”。
  • Here you can add code as - JComboBox comboBox = new JComboBox(strArray); 在这里,您可以将代码添加为JComboBox comboBox = new JComboBox(strArray); and clock on OK. 然后按OK。
  • Make sure your array contains value to avoid exceptions before creating JComboBox in code. 在代码中创建JComboBox之前,请确保您的数组包含值以避免异常。

OR 要么

If you do not wish to provide array on create in new JComboBox(); 如果您不希望在new JComboBox();创建时提供数组new JComboBox();参见 then you can do 那你就可以

  • Manually in code via this lines of code - 通过以下代码行手动输入代码-

    String[] strAyyary = new String[]{"A", "B", "C", "D", "E"}; String [] strAyyary = new String [] {“ A”,“ B”,“ C”,“ D”,“ E”}; DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel(strAyyary); DefaultComboBoxModel defaultComboBoxModel =新的DefaultComboBoxModel(strAyyary); comboBox.setModel(defaultComboBoxModel); comboBox.setModel(defaultComboBoxModel);

  • Using NetBeans Palette - 使用NetBeans调色板-

    • Select the JComboBox added to your frame or panel. 选择添加到框架或面板的JComboBox。
    • Choose combo box component and select right click option as " Customize Code.. " 选择组合框组件,然后选择右键单击选项作为“ 自定义代码”。
    • Here "Code Customizer" opened, choose option from second combo as " Custom Property " from left side. 在这里打开了“代码定制器”,从第二个组合中选择选项作为左侧的“ 定制属性 ”。
    • Here you can add code as - comboBox.setModel(new javax.swing.DefaultComboBoxModel(strAyyary)); 在这里,您可以添加代码- comboBox.setModel(new javax.swing.DefaultComboBoxModel(strAyyary)); and clock on OK. 然后按OK。
    • Make sure your array contains value to avoid exceptions before creating JComboBox in code. 在代码中创建JComboBox之前,请确保您的数组包含值以避免异常。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM