How to handle dynamic checkbox array for I/O

edited July 2016 in Library Questions

I am trying to get input value from 3checkbox in one array, upto 5 or more array here is my code. I entre value in the textbox to genertate the checkbox arrays.

// Need G4P library import g4p_controls.*;

public void setup(){ size(480, 320, JAVA2D); createGUI(); customGUI(); // Place your setup code here

}

public void draw(){ background(230);

}

// Use this method to add additional statements // to customise the GUI controls public void customGUI(){

}

public void textfield1_change1(GTextField source, GEvent event) { //CODE:txtno:852228: // println("txtno - GTextField >> GEvent." + event + " @ " + millis());

// println(txtno.getText()); } //CODE:txtno:852228:

public void button1_click1(GButton source, GEvent event) { //CODE:btnSubmit:452090: //println("btnSubmit - GButton >> GEvent." + event + " @ " + millis()); //Integer.parseInt(

String actant = txtno.getText(); if(actant.isEmpty()){ println("increase the number of acctant"); } else{ println(txtno.getText()); int i = Integer.parseInt(txtno.getText()); for(int x=1; x<=i; x++){ //Actant number to display

G4P.messagesEnabled(false);
G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME); G4P.setCursor(ARROW); //surface.setTitle("Actant Input");

for(int j=1; j<=3; j++){ int a = 100 * j; int b= 20 * x;

//for creating Label for actant.
  if(j==1){
 lblTitle = new GLabel(this, -95+a, 30 + b, 85, 20);
  lblTitle.setText("Actant No: "+ x + ".");
  lblTitle.setOpaque(false);
  }
 //For Creating checkbox
  chkA1 = new GCheckbox(this, 5+a, 30 + b, 85, 20);
  chkA1.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
if(j==1){
chkA1.setText("Heat");
}
else if(j==2){
chkA1.setText("Noice");
}
else{
chkA1.setText("Smell");
}

chkA1.setOpaque(false);
chkA1.addEventHandler(this, "chka1");

}
}

}

} //CODE:btnSubmit:452090:

// Create all the GUI controls. // autogenerated do not edit public void createGUI(){ G4P.messagesEnabled(false); G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME); G4P.setCursor(ARROW); surface.setTitle("Sketch Window"); lblActant = new GLabel(this, 5, 5, 95, 22); lblActant.setText("Actant Number"); lblActant.setTextBold(); lblActant.setOpaque(false); txtno = new GTextField(this, 104, 4, 43, 30, G4P.SCROLLBARS_NONE); txtno.setOpaque(true); txtno.addEventHandler(this, "textfield1_change1"); btnSubmit = new GButton(this, 158, 8, 80, 30); btnSubmit.setText("Submit"); btnSubmit.addEventHandler(this, "button1_click1");

}

// Variable declarations // autogenerated do not edit GLabel lblActant; GTextField txtno; GButton btnSubmit; GLabel lblTitle;

//Creating CheckBox GCheckbox chkA1;

// Create all the GUI controls. // autogenerated do not edit public void chkBox(){ //G4P.messagesEnabled(false); //G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME); //G4P.setCursor(ARROW); //surface.setTitle("Sketch Window"); //for(int j=1; j<=3; j++){ // int a = 100 * j; // chkA1 = new GCheckbox(this, 5, 40, 85, 20);

//chkA1.setTextAlign(GAlign.LEFT, GAlign.MIDDLE); //chkA1.setText("1"); //chkA1.setOpaque(false); //chkA1.addEventHandler(this, "chka1"); } //}

thank you in advance.

Answers

  • Answer ✓

    Several problems with your code - there are syntax errors. Also you need to use Ctrl+T in the Processing IDE, this will layout your code making it easier for other programmers to read it.

    Anyway I have corrected the syntax errors so I could run your code and try and work out what you are trying to achieve.

    It seem to me that you are creating a 2D array of checkboxes where there are 3 boxes per row and the number of rows determined by the number entered in the textfield. Since you need to no which checkbox is being clicked you need a 2D array of check boxes which my code creates. It also gives a unique number (tagNo) for each checkbox so you can work out which one is being clicked.

    If you have never used arrays before then you will have to learn how to use them since there is no real alternative to get what you want. There are tutorial on arrays. :)

    import g4p_controls.*;
    
    public void setup() { 
      size(480, 320, JAVA2D); 
      createGUI(); 
      customGUI(); 
      // Place your setup code here
    }
    
    public void draw() { 
      background(230);
    }
    
    // Use this method to add additional statements 
    // to customise the GUI controls 
    public void customGUI() {
    }
    
    public void textfield1_change1(GTextField source, GEvent event) { //CODE:txtno:852228:
    } //CODE:txtno:852228:
    
    public void button1_click1(GButton source, GEvent event) {  //CODE:btnSubmit:452090: 
      String actant = txtno.getText(); 
      if (actant.isEmpty()) { 
        println("increase the number of acctant");
      } else { 
        int i = Integer.parseInt(txtno.getText()); 
        if (i > 0) {
          cbxArray = new GCheckbox[i][3];
          for (int x=0; x < i; x++) { //Actant number to display
            lblTitle = new GLabel(this, 5, 40 + 20 * x, 85, 20);
            lblTitle.setText("Actant No: "+ (x+1) + ".");
            lblTitle.setOpaque(false);
            for (int j=0; j<3; j++) { 
              //For Creating checkbox
              cbxArray[x][j] = new GCheckbox(this, 105+100*j, 40 + 20 * x, 85, 20);
              cbxArray[x][j].setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
              if (j==1) {
                cbxArray[x][j].setText("Heat");
              } else if (j==2) {
                cbxArray[x][j].setText("Noise");
              } else {
                cbxArray[x][j].setText("Smell");
              }
              cbxArray[x][j].setOpaque(false);
              cbxArray[x][j].addEventHandler(this, "cbx_array_handler");
              cbxArray[x][j].tagNo = (x+1)*1000 + j + 1;
            }
          }
        }
      }
    } //CODE:btnSubmit:452090:
    
    // Create all the GUI controls. // autogenerated do not edit 
    public void createGUI() { 
      G4P.messagesEnabled(false); 
      G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME); 
      G4P.setCursor(ARROW); 
      surface.setTitle("Sketch Window"); 
      lblActant = new GLabel(this, 5, 5, 95, 22); 
      lblActant.setText("Actant Number"); 
      lblActant.setTextBold(); 
      lblActant.setOpaque(false);
      txtno = new GTextField(this, 104, 4, 43, 30, G4P.SCROLLBARS_NONE);
      txtno.setOpaque(true); 
      txtno.addEventHandler(this, "textfield1_change1");
      btnSubmit = new GButton(this, 158, 8, 80, 30); 
      btnSubmit.setText("Submit");
      btnSubmit.addEventHandler(this, "button1_click1");
    }
    
    // Variable declarations // autogenerated do not edit 
    GLabel lblActant; 
    GTextField txtno; 
    GButton btnSubmit; 
    GLabel lblTitle;
    // Create Checkbox array
    GCheckbox[][] cbxArray;
    
    public void cbx_array_handler(GCheckbox checkbox, GEvent event) { 
      int actant_nbr = checkbox.tagNo / 1000;
      int cbx_nbr = checkbox.tagNo % 1000;
      print("Checkbox click for actant nbr: " + actant_nbr + "    for checkbox: ");
      switch(cbx_nbr) {
      case 1:
        print("Heat");
        break;
      case 2:
        print("Noise");
        break;
      case 3:
        print("Smell");
        break;
      default:
        print("Unknown");
      }
      println("   value: " + checkbox.isSelected());
    }
    
  • thank you dear

Sign In or Register to comment.