开发者

How should one pass values of multiple checkboxes to server

What cuold be the best solution to this scenario: I have a SOAP based webservice implemented in Netbeans where the client is supposed to click on a number of checkboxes which are then sent to server and stored. Suppose my webservice has these checkboxes where all or some can be selected:

Ethnicity: 1.Caucasian 2.South-Est Asian 3.South Asian 4.African 5.Other

in another part of the same webservice i have implemented the checkboxes related to

Sex: 1.Male 2.Female

As following where either one or both can be selected but the solution looks very complicated to me for the Ethnicity part and i have other parts with even more checkboxes!

Client side code:

     private void salvaActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:
           disease=malattia.getText();
           etastr=eta.getText();
           age=java.lang.Integer.parseInt(etastr);
           description=descrizione.getText();

         //Here i'm initiating the array using sexint as the dimension
         sexarra=new String[sexint];
        if(sexint==1)
        sexarra[0]=sexone;
        else if(sexint==0)
            JOptionPane.showMessageDialog(null, "Bisogna specificare     almeno un valore del campo sesso", "Errore", JOptionPane.ERROR_MESSAGE);
        else{
            sexarra[0]=sexone;
            sexarra[1]=sextwo;}


         // I define the parameters and afterwards send them to server
          Vector parametri = new Vector();
                  parametri.addElement(new Parameter("malattia", String.class, disease, null));
                 parametri.addElement(new Parameter("age", int.class, age, null));
                 parametri.addElement(new Parameter("descrizione", String.class, description, null));
                    parametri.addElement(new Parameter("sexarra",String[].class, sexarra, null));



       //Code related to calculating sexint which is used above as the dimension to array


     private void femaleActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:

        if(female.isSelected()){
            if(sexint==0){
              sexint++;
              sexone=female.getText();

            }

            else if(sexint==1){
            sexint++;

            sextwo=female.getText();
            }
            else
            sexint--;
        }
        }                     开发者_高级运维                  

        private void maleActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
              if(male.isSelected()){
            if(sexint==0){
              sexint++;
              sexone=male.getText();

            }

            else if(sexint==1){
            sexint++;

            sextwo=male.getText();
            }
            else
            sexint--;
        }
        }                

Code related to the server side:

       public String aggiungi_malattia(String malattia, int eta, String descrizione, String[] sexarra) {
                String ris = "no";
                String q = null, w = null;
                String errore = connetti();

               if(sexarra.length == 2){
         q = "INSERT INTO malattia (nome, eta, descrizione, sesso) "
                    + "VALUES ('" + malattia + "','" + eta + "','" + descrizione + "','" + sexarra[0] + "')";
         w="INSERT INTO malattia (nome, eta, descrizione, sesso) "
                    + "VALUES ('" + malattia + "','" + eta + "','" + descrizione + "','" + sexarra[1] + "')";
               }
         {
          q = "INSERT INTO malattia (nome, eta, descrizione, sesso) "
                    + "VALUES ('" + malattia + "','" + eta + "','" + descrizione + "','" + sexarra[0] + "')";

Thank you all for your time and effort!


Create a list or array to maintain list of selected items(strings) at the class level.

List<String> selectedEthnicities = new ArrayList();

You should start with converting multiple check boxes to an array of check boxes such as :

 JCheckBox[] ethnicities = new JCheckBox[5];
 ethnicities[0] = new JCheckBox("USA"); 
 ethnicities[1] = new JCheckBox("Brazil");
 ethnicities[2] = new JCheckBox("Mexico");
 ethnicities[3] = new JCheckBox("Canada");

Then you can add ItemListener to the boxes in a loop :

for(int i=0; i< ethnicities .length; i++) {
    ethnicities [i].addItemListener(new EthnicityItemListener(selectedEthnicities ));
}

Note that you have passed selectedEthnicities array list to the item listener. Now the EthnicityItemListener item listener's implementation may look like this:

   private class EthnicityItemListener implements ItemListener {
        ArrayList selectedList;
        private EthnicityItemListener(ArrayList<String> selectedEthnicities) {
            this.selectedList = selectedEthnicities;
        }

        @Override
        public void itemStateChanged(ItemEvent e) {
            //add or remove selected chk box text to the list based on 
            //selection state.
            if(e.getStateChange() == ItemEvent.SELECTED){
                this.selectedList.add(((JCheckBox)e.getSource()).getText());
            }else {
                this.selectedList.remove(((JCheckBox)e.getSource()).getText());
            }
            System.out.println(selectedList);
        }
    }   

The class variable selectedEthnicities can be looped to create SQL statements. This way you can reduce a lot of unnecessary code complexity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜