G4P - Checkbox grid

You might try the GUI Builder tool (can install from Contribution Manager). This tool helps you design and create GUIs with G4P.

The link above takes you to my website where there are a number of videos showing how to use GUI Builder.

If you don’t want to use GUI Builder then this example shows how you can create a grid of checkboxes from a 2D String array. Since Java treats 2D arrays as a 1D array of arrays we can create irregular arrangements as shown in this sketch.

import g4p_controls.*;

String[][] shopping_list = {
  {"Apples", "Pears", "Oranges", "Bananas"}, // Row 1 has 4 boxes
  {"Lamb", "Beef", "Pork", "Buscuits"}, // Row 2 has 4 boxes
  {"Salt", "Pepper"}, // Row 3 has 2 boxes
  {"Jam", "Marmalade", "Tomatos"}, // Row 4 has 3 boxes
  {"Wheatabix", "Butter", "Marmite", "Pizza"}   // Row 5 has 4 boxes
};

void setup() {
  size(600, 300);
  G4P. messagesEnabled(false);
  makeShoppingList();
}

void makeShoppingList() {
  GCheckbox cbx;
  for (int row = 0; row < shopping_list.length; row ++) {
    for (int col = 0; col < shopping_list[row].length; col ++) {
      cbx = new GCheckbox(this, col * 100 + 20, row * 25 + 30, 90, 20, shopping_list[row][col]);
      cbx .addEventHandler(this, "shoppingListChange");
    }
  }
}

void shoppingListChange(GCheckbox source, GEvent event) {
  String action = event == GEvent.SELECTED ? " ADDED " : " REMOVED ";
  println(source.getText() + action + "\t" + millis());
}

void draw() {
  background(200, 200, 255);
}
3 Likes