java - 遍历 hashmap 并将项目添加到 swing jcombobox

标签 java swing model-view-controller jcombobox comboboxmodel

我正在开发我的第一个 Java 应用程序,我被卡在了这一部分,我需要用 HashMap 中的项目填充我的组合框。

我正在使用 Model View Controller 方法并尝试从 GUI 中填充组合框。因此,在用户单击按钮后,将调用一个方法,该方法应与 Controller 通信并请求组合框的项目。 Controller 应将项目发送回 GUI,然后可以填充组合框。

因此,在我的一个模型文件中,我创建了一个 hashmap 并通过我的 Controller 向其中添加项目。

HashMap 看起来像这样:

HashMap<Integer, Customer> customerRegisterHashMap = new HashMap<Integer, Customer>();

让我们假设 map 现在根据 Customer 类属性填充了示例数据。

现在,我想我需要在 Controller 或模型本身中实现一个方法,该方法遍历上面的 HashMap 并返回数据(集合?)。

我会(另一个假设,可能没有必要),也需要再次遍历这些数据,只是这次在 GUI 类中,并将项目一个接一个地添加到组合框中。

所以项目看起来像这样:5 个文件,Controller、Customerregister、Customer、Frame 和一个显示 Frame 的应用程序文件。

非常感谢您的任何建议。

最佳答案

这可能会给你一个想法:

HashMap<Integer, Customer> stuff = new HashMap<Integer, Customer>();
stuff.put(0, new Customer());
stuff.put(2, new Customer());
Iterator it = stuff.keySet().iterator();
while(it.hasNext())
{
    ComboBox.addItem(stuff.get(it.next()));
}

您可以在您的应用中传递 HashMap,然后只需将其中的内容添加到 JComboBox 中,如上所示。

关于java - 遍历 hashmap 并将项目添加到 swing jcombobox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14185650/

相关文章:

java - BufferedWriter 覆盖自身

java - 为java创建词法分析器

java - HttpSession.getId() 有多独特?

java - 创建类的新对象后获取 NPE

java - 需要设计方案 : Enabling menu item from outside JFrame

java - Http 请求中的 Jmeter If Controller

java - 如何创建带有菜单的 JButton?

JavaScript MVC : model method called inside controller returns udefined -> but why?

c# - 如何在 MVC 模式中管理多个表单

javascript - 将 _Layout.cshtml 中的所有脚本作为一个包加载?