Java 如何从多个复选框中检索值

发布于 2024-11-25 00:08:40 字数 1029 浏览 4 评论 0原文

我在 Java 中实现了这个基于 SOAP 的 Web 服务,其中客户端有一个复选框列表,在他选择后,这些复选框将存储在数据库中。例如,对于 Sex(maschio,femmina) 的复选框,他可以选择其中两个或其中之一我是这样实现的,但问题是在这种情况下 2 数组具有固定大小,因此如果用户仅选择其中之一 sexarra[0] 将包含它,但 sexarra[1] 将为空,所以如果我将其传递给服务器它可能会产生问题,并且其他复选框的尺寸要大得多,有没有更好的方法来处理这种情况?预先感谢大家的帮助,非常感谢!我应该补充一点,System.out 仅用于测试:

private void femminaActionPerformed(java.awt.event.ActionEvent evt) {
    if (femmina.isSelected()) {
        if (sexint == 0) {
            sexint++;
            sexarra[sexint] = femmina.getText();
        } else {
            sexarra[sexint] = femmina.getText();
        }
    }
    System.out.println(sexarra[0]);
    System.out.println(sexarra[1]);
}

private void maschioActionPerformed(java.awt.event.ActionEvent evt) {
    if (maschio.isSelected()) {
        if (sexint == 0) {
            sexarra[sexint] = maschio.getText();
            sexint++;
        } else {
            sexarra[sexint] = maschio.getText();
        }
    }
    System.out.println(sexarra[0]);
    System.out.println(sexarra[1]);
}  

I have this SOAP based webservice implemented in Java where the client has a list of checkboxes which after he selects will be stored in the DB.For example for a checkbox of Sex(maschio,femmina) he can select both of them or one of them i implemented it like this but the issue is that the array has fixed size in this case 2 so if the user selects only one of them sexarra[0] will contain it but sexarra[1] will be null so if i pass it to the server it can create problems and the other checkboxes are much larger in size is there any better way to handle this situation? Thank you all in advance your help is really appreciated! i should add that System.out are for testing only:

private void femminaActionPerformed(java.awt.event.ActionEvent evt) {
    if (femmina.isSelected()) {
        if (sexint == 0) {
            sexint++;
            sexarra[sexint] = femmina.getText();
        } else {
            sexarra[sexint] = femmina.getText();
        }
    }
    System.out.println(sexarra[0]);
    System.out.println(sexarra[1]);
}

private void maschioActionPerformed(java.awt.event.ActionEvent evt) {
    if (maschio.isSelected()) {
        if (sexint == 0) {
            sexarra[sexint] = maschio.getText();
            sexint++;
        } else {
            sexarra[sexint] = maschio.getText();
        }
    }
    System.out.println(sexarra[0]);
    System.out.println(sexarra[1]);
}  

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

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

发布评论

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

评论(3

寻找一个思念的角度 2024-12-02 00:08:40

请阅读如何使用按钮、复选框和单选按钮如果有很多复选框,那么您必须阅读如何使用 ButtonGroup 组件,有关 Action Listener 的内容>,您可以在此处找到示例

please read How to Use Buttons, Check Boxes, and Radio Buttons and if there are lots of checkboxes then you have to read How to Use the ButtonGroup Component, something about Action Listener, you can find examples here

深府石板幽径 2024-12-02 00:08:40

您不需要任何事件处理程序。

如果您确实需要填充一些数组,请仅在最后提交数据时执行此操作。每个按钮都知道其状态;每次单击按钮时,无需将此信息冗余地存储在另一个数据结构中。

最后,在提交最终确定的数据时:

System.out.println(jButton1.getName() + ": " + jButton1.isSelected());
System.out.println(jButton2.getName() + ": " + jButton2.isSelected());\
...

您只需在准备提交时填写要发送到数据库的任何数据结构,而不是在代码中打印出来。

此外,您还应该考虑同步:您希望在 Swing EDT 上运行的一个方法来填充您的数据结构,但另一个线程上的其他方法将这些方法发送到数据库。并且以某种方式您必须确保数据在两个线程之间同步。

You don't need any event handler.

If you do need to populate some arrays, only do it when you are submitting your data in the end. Each button knows its state; there is no need to redundantly store this info in another data structure each time a button is clicked.

In the end, when submitting the finalized data:

System.out.println(jButton1.getName() + ": " + jButton1.isSelected());
System.out.println(jButton2.getName() + ": " + jButton2.isSelected());\
...

Instead of printing out in your code, you just fill whatever data structure you are sending to the DB when you are ready to submit.

Also you should think about synchronization: you want one method that runs on the Swing EDT to fill your data structure, but some other method on another thread to send those to the DB. And somehow you must make sure the data is synchronized between the two threads.

野却迷人 2024-12-02 00:08:40

好的,我解决了我遇到的问题,我引入了一个整数 sexint,只要选择其中一个复选框,它就会更新,从而确定数组的大小,这里是代码:

private void femminaActionPerformed(java.awt.event.ActionEvent evt) {                                        
    if(femmina.isSelected()){
        if(sexint==0){
          sexint++;
          sexone=femmina.getText();    
        }
        else if(sexint==1){
            sexint++;
        sextwo=femmina.getText();
        }
        else
            sexint--;

    System.out.println(sexint);        
    }
}                                       

private void maschioActionPerformed(java.awt.event.ActionEvent evt) {       
    if(maschio.isSelected()){
        if(sexint==0){
          sexint++;
          sexone=maschio.getText();
        }
        else if(sexint==1){
            sexint++;
        sextwo=maschio.getText();
        }
        else
            sexint--;

    System.out.println(sexint);
    }
}   

之后我采用 sexint 并使用它来实例化数组 sexarra
感谢大家花时间查看我的问题

Ok i resolved the problem i had, i introduced an integer sexint which gets updated whenever one of the checkboxes is selected thus determining the size of the array here is the code:

private void femminaActionPerformed(java.awt.event.ActionEvent evt) {                                        
    if(femmina.isSelected()){
        if(sexint==0){
          sexint++;
          sexone=femmina.getText();    
        }
        else if(sexint==1){
            sexint++;
        sextwo=femmina.getText();
        }
        else
            sexint--;

    System.out.println(sexint);        
    }
}                                       

private void maschioActionPerformed(java.awt.event.ActionEvent evt) {       
    if(maschio.isSelected()){
        if(sexint==0){
          sexint++;
          sexone=maschio.getText();
        }
        else if(sexint==1){
            sexint++;
        sextwo=maschio.getText();
        }
        else
            sexint--;

    System.out.println(sexint);
    }
}   

after which i take sexint and use it to instantiate the array sexarra
Thanks everybody for having taken their time in looking at my problem

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