Two problems in code (table sheet)

PFont font;
int tableX = 50, tableY = 50, rowHeight = 30, numRows = 30, numCols = 8;
int[] colWidths = {80, 80, 80, 80, 80, 80, 80, 80};
String[] headers = {"Serial No", "A", "B", "C", "D", "E", "Equation", "G"};
String[][] cellValues; // Array to store cell values
int currentRow = -1, currentCol = -1; // Variables to track selected cell
String currentInput = ""; // Variable to store current input

void setup() {
  size(1900, 1000);
  font = createFont("Arial", 12);
  textFont(font);
  textAlign(CENTER, CENTER);
  cellValues = new String[numRows][numCols]; // Initialize cell values
  // Initialize all cell values with an empty string
  for (int i = 0; i < numRows; i++) {
    for (int j = 0; j < numCols; j++) {
      cellValues[i][j] = "";
    }
  }
}

void draw() {
  background(255);
  fill(0);
  int xPosition, yPosition;

  // Draw rows, horizontal lines, and cell values
  yPosition = tableY + rowHeight; // Start below the header
  for (int row = 0; row < numRows; row++) {
    xPosition = tableX;
    for (int col = 0; col < numCols; col++) {
      // Set fill color to light blue for the first row cells
      if (row == 0) {
        fill(173, 216, 230); // Light blue color
        rect(xPosition, yPosition - rowHeight, colWidths[col], rowHeight);
      }

      // Set text color to black and draw the text
      fill(0);
      String cellText = cellValues[row][col];
      if (row == currentRow && col == currentCol) {
        cellText = currentInput;
      }
      text(cellText, xPosition + colWidths[col] / 2, yPosition + rowHeight / 2);
      xPosition += colWidths[col];
    }
    line(tableX, yPosition, xPosition, yPosition); // Draws horizontal line at bottom of each row
    yPosition += rowHeight;
  }

  // Draw the top boundary line of the table
  line(tableX, tableY, tableX + sumArray(colWidths, numCols), tableY);

  // Drawing headers and vertical lines
  xPosition = tableX;
  fill(0); // Set text color to black for headers
  for (int col = 0; col < numCols; col++) {
    text(headers[col], xPosition + colWidths[col] / 2, tableY + rowHeight / 2);
    line(xPosition, tableY, xPosition, tableY + numRows * rowHeight + rowHeight);
    xPosition += colWidths[col];
  }
  line(xPosition, tableY, xPosition, tableY + numRows * rowHeight + rowHeight); // Right boundary line

  // Draw the bottom horizontal line of the table
  line(tableX, yPosition, xPosition, yPosition); // Corrected bottom boundary line

  // Draw the left vertical line of the table
  line(tableX, tableY, tableX, yPosition); // Left boundary line
}

// Helper function to sum up the widths of columns
int sumArray(int[] arr, int length) {
  int total = 0;
  for (int i = 0; i < length; i++) {
    total += arr[i];
  }
  return total;
}



void keyPressed() {
  if (currentRow >= 0 && currentCol >= 0) {
    if (key == BACKSPACE && currentInput.length() > 0) {
      // Remove the last character and update the cell value
      currentInput = currentInput.substring(0, currentInput.length() - 1);
      cellValues[currentRow][currentCol] = currentInput;
    } else if (key != BACKSPACE && key != ENTER && key != RETURN) {
      // Add character to the current input if it's less than 4 characters
      if (currentInput.length() < 4) {
        currentInput += key;
        cellValues[currentRow][currentCol] = currentInput;
      }
    } else if (key == ENTER || key == RETURN) {
      // When Enter is pressed, confirm the input
      currentInput = "";
      currentRow = -1; currentCol = -1; // Deselect cell
    }
  }
}



void mouseClicked() {
  int clickedRow = (mouseY - tableY) / rowHeight - 1;
  int clickedCol = findClickedColumn(mouseX);
  if (clickedRow >= 0 && clickedCol >= 0) {
    currentRow = clickedRow;
    currentCol = clickedCol;
    currentInput = cellValues[clickedRow][clickedCol];
  }
}

int findClickedColumn(int mouseX) {
  int x = tableX;
  for (int i = 0; i < numCols; i++) {
    if (mouseX < x + colWidths[i]) {
      return i;
    }
    x += colWidths[i];
  }
  return -1;
}


i want two rows in column 7 , please see code , one row should be heading and the other row should be long between row 2 and 30

1 Like