# G4P - Checkbox grid

**URL:** https://discourse.processing.org/t/g4p-checkbox-grid/25585
**Category:** Libraries
**Created:** [November 21, 2020, 5:00am UTC](https://discourse.processing.org/t/g4p-checkbox-grid/25585 "2020-11-21T05:00:25Z")
**Posts on this page:** 1
**Showing post:** 11

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [November 22, 2020, 8:49am UTC](https://discourse.processing.org/t/g4p-checkbox-grid/25585/11 "2020-11-22T08:49:01Z")

</div>

> [@RenanRabelo](#):
>
> How can I draw the grid in another window?

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.

```auto
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);
    }
  }
}

```

---

_[View the full topic](https://discourse.processing.org/t/g4p-checkbox-grid/25585)._
