在 Java Swing 应用程序中创建复选框

发布于 2024-11-04 08:51:02 字数 794 浏览 1 评论 0原文

我正在创建此应用程序,通过发送选定的单元来与 servlet 进行通信,如果单元主服务器有任何更改,则以带有空值的映射形式发送这些单元。从桌面上,我只需要从服务器获取选定单元的数据。最初,服务器将所有单位和相关数据发送到桌面应用程序,再次在桌面上选择后,我们需要连接到服务器以获取数据。为此,我需要显示带有地图中名称的复选框。

如何在 Java Swing 应用程序中创建以 Map 键作为名称的复选框。假设最初从服务器

Map<String, String> m1 = new HashMap<String, String>();    
    m1.put("091","091");
    m1.put("061","061");
    m1.put("001","001");
    m1.put("032","031");

在桌面应用程序中选择后,它应该

Map<String, String> m1 = new HashMap<String, String>();    
        m1.put("091","091");
        m1.put("061",null);
        m1.put("001",null);
        m1.put("032","031");

带有资源。对于映射的键和值对,我想创建复选框,其中键是复选框的名称,值是选择该复选框。如果键包含一个值(不为空),那么在创建该复选框时应该选择它。地图将动态更改,并且应将创建复选框添加到 JDialog。在选择结束时,我需要构建地图并将其发送到服务器。

I am creating this application to communicate the servlet by sending the selected units, if there is any changes in unit master server sends those units in the form of map with null values. From the desktop I need to get data from the server for only selected units. Initially server sends all the units and associated data to the desktop app, after selection at desktop again we need to connect to server to get data. For that I need show the checkboxes with the names which are there in map.

How to create checkboxes in Java Swing application with Map keys as their names. Assume initially from the server

Map<String, String> m1 = new HashMap<String, String>();    
    m1.put("091","091");
    m1.put("061","061");
    m1.put("001","001");
    m1.put("032","031");

After selection at desk application, it should be

Map<String, String> m1 = new HashMap<String, String>();    
        m1.put("091","091");
        m1.put("061",null);
        m1.put("001",null);
        m1.put("032","031");

With res. to key and value pair of map I want to create the checkboxes where key is the name of the checkbox and value is to select that checkbox. If key contains a value (not null) then while creating that checkbox it should be selected. The map is going to be changed dynamically and creating checkboxes should be added to the JDialog. At the end of the selection I need to construct the map and I want to send that to the server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半山落雨半山空 2024-11-11 08:51:11

创建一个JCheckBoxs数组,数组的大小应为m1的大小。 迭代 m1 并相应地初始化 JCheckBoxes ,例如,如果entry是当前条目(在迭代中),cbArr是复选框数组,并且i在循环开始之前初始化为0:

cbArr[i] = new JCheckBox(entry.getKey());
if (entry.getValue() != null)
{
    cbArr[i].setSelected(true);
}
else
{
    cbArr[i].setSelected(false);
}
yourPanel.add(cbArr[i++]);

这是基本的,用于创建更新的HashMap(发送到服务器)迭代在复选框数组上插入键作为复选框的当前名称,并根据复选框是否被选中插入值。

Hash<String,String> m2 = new HashMap<String,String>();
for (int i = 0; i < cbArr.length; i++)
{
    m2.put(cbArr[i].getText(), cbArr[i].isSelected() ? cbArr[i].getText() : null);
}

Create an array of JCheckBoxs, the size of the array should be the size of m1. Iterate over m1 and initialize the JCheckBoxes accordingly, for example if entry is the current entry (in your iteration), cbArr is the array of checkboxes and i initialized to 0 before the start of the loop:

cbArr[i] = new JCheckBox(entry.getKey());
if (entry.getValue() != null)
{
    cbArr[i].setSelected(true);
}
else
{
    cbArr[i].setSelected(false);
}
yourPanel.add(cbArr[i++]);

This is the basic, to create the updated HashMap (to send to server) iterate over the array of CheckBoxes and insert key as current name of the check box, and value depending if the checkbox is selected or not.

Hash<String,String> m2 = new HashMap<String,String>();
for (int i = 0; i < cbArr.length; i++)
{
    m2.put(cbArr[i].getText(), cbArr[i].isSelected() ? cbArr[i].getText() : null);
}
-残月青衣踏尘吟 2024-11-11 08:51:10

我总是从复选框教程中的示例开始。如果这不是您的意思,可以编辑您的问题吗?

I always start with the examples in the tutorial on Check Boxes. If that's not what you mean, can edit your question?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文