G4P - Checkbox grid

This is quite simple and does not require you to modify the CheckboxGrid class. Simply create the GWindow, add a draw handler with a background statement and pass the reference to the new window in the CheckboxGrid constructor.

This is a modified version of my previous example. It will create 2 extra windows and 3 CheckboxGrids, one for each window.

import g4p_controls.*;

// Demonstration of a 2D grid of checkboxes. The size of the grid is user defineable.
// 
CheckboxGrid cbg0, cbg1, cbg2;
GWindow gw0, gw1, gw2;

void setup() {
  size(600, 400);
  G4P.messagesEnabled(false);
  // First create the windows
  gw1 = GWindow.getWindow(this, "Window gw1", 30, 50, 180, 200, JAVA2D);
  gw2 = GWindow.getWindow(this, "Window gw2", 300, 60, 160, 220, JAVA2D);
  // Now the grids
  cbg0 = new CheckboxGrid(this, 20, 50, 10, 8, 8);
  cbg1 = new CheckboxGrid(gw1, 20, 20, 8, 3, 5);
  cbg2 = new CheckboxGrid(gw2, 10, 30, 8, 7, 4);
  // Finally the draw handlers for the extra windows.
  gw1.addDrawHandler(this, "win_draw_1");
  gw2.addDrawHandler(this, "win_draw_2");
}

public void win_draw_1(PApplet appc, GWinData data) {
  appc.background(200, 255, 200);
}

public void win_draw_2(PApplet appc, GWinData data) {
  appc.background(255, 200, 200);
}


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

/*
This class encapsulates a 2D array of checkboxes
 */
public class CheckboxGrid {

  final GCheckbox [][] grid;
  final int nbrRows, nbrCols;

  /*
   Use this constructor to create the grid
   */
  CheckboxGrid(PApplet papp, 
    int left, int top, // top left corner of box encompassing all checkboxes
    int gutter, // The spacing between each row and column
    int nbrRows, int nbrCols // number of rows and columns in the grid
    ) {
    this.nbrRows = nbrRows;
    this.nbrCols = nbrCols;
    grid = new GCheckbox[nbrRows][nbrCols];
    int colSpace = 20 + gutter;
    int rowSpace = 16 + gutter;
    for (int row = 0; row < nbrRows; row ++) {
      for (int col = 0; col < nbrCols; col ++) {
        GCheckbox cbx = new GCheckbox(papp, col * colSpace + left, row * rowSpace + top, 22, 10, " ");
        cbx.tagNo = row * 10000 + col;
        cbx.tag = "Box [row " + row + ",  col " + col +"]";
        cbx.addEventHandler(this, "handleEvents");
        grid[row][col] = cbx;
      }
    }
  }

  /*
   Probably don't need this since you will be checking the state using getState(...)
   in the main code
   */
  public void handleEvents(GCheckbox source, GEvent event) {
    String action = event == GEvent.SELECTED ? " was SELECETED " : " was DESELECETED ";
    println(source.tag + action + "\t" + millis());
  }

  /*
   Get the state (true / false) of a checkbox based on its position in the grid.
   If the user specifies a position outside the grid it returns false.
   */
  public boolean getState(int row, int col) {
    if (row >=0 && row <= nbrRows && col >= 0 && col <= nbrCols) {
      return grid[col][row].isSelected();
    }
    return false; // row, col is not inside grid
  }

  /*
   Allows the user to set the state of a particular checkbox instead of clicking on it
   If the user specifies a position outside the grid it does nothing.
   */

  public void setState(int row, int col, boolean state) {
    if (row >=0 && row <= nbrRows && col >= 0 && col <= nbrCols) {
      grid[row][col].setSelected(state);
    }
  }
}
2 Likes