Two problems in code (table sheet)

Table table; 
int numRows = 30; // Number of rows
int numCols = 7; // Number of columns
float cellWidth = 100; // Adjustable cell width
float cellHeight = 27; // Adjustable cell height
float tableWidth, tableHeight;

// Manual position of the table
float tableX = 150; 
float tableY = 50; 

// Declare the Arial font
PFont arialBold;

// Array to store text for each cell
String[][] cellTexts;

// Variables for interactive text editing
boolean isEditing = false;
int editingRow = -1, editingCol = -1;
String currentInput = "";

void setup() {
  size(1800, 980); // Set initial size of the window
  surface.setResizable(true); // Make the window resizable

  // Load the Arial font
  arialBold = createFont("Arial-BoldMT", 12); // Adjust the font size if needed

  // Initialize the table and add customized headings
  table = new Table();
  table.addColumn("A"); // First column heading
  table.addColumn("A");   // Second column heading
  table.addColumn("A");   // Third column heading
  table.addColumn("A");    // Fourth column heading
  table.addColumn("A"); // Fifth column heading
  for (int i = 5; i < numCols; i++) {
    table.addColumn("Column " + (i + 1));
  }

  // Initialize the cellTexts array with default values
  cellTexts = new String[numRows][numCols];
  for (int i = 0; i < numRows; i++) {
    for (int j = 0; j < numCols; j++) {
      cellTexts[i][j] = i == 0 ? getHeaderText(j) : "";
    }
  }

  tableWidth = numCols * cellWidth;
  tableHeight = numRows * cellHeight;
}

void draw() {
  background(200); // Example background
  float currentTextSize = 12; // Variable to store the current text size

  // Draw the table cells at the specified position
  for (int i = 0; i < numRows; i++) {
    for (int j = 0; j < numCols; j++) {
      // Skip drawing cells in rows 1 to 30 of column 7
      if (j == 6 && i >= 1 && i <= 30) {
        continue;
      }

      float x = j * cellWidth + tableX;
      float y = i * cellHeight + tableY;

      // Set cell background color
      fill(i == 0 ? 173 : 255, i == 0 ? 216 : 255, i == 0 ? 230 : 255); // Light blue for the first row, white for others

      stroke(0);
      rect(x, y, cellWidth, cellHeight); // Use cellWidth for the width of the cells

      // Use text from the cellTexts array or currentInput if editing
      String cellText = (isEditing && i == editingRow && j == editingCol) ? currentInput : cellTexts[i][j];

      // Apply styling for text
      textFont(i == 0 ? arialBold : createFont("Arial", currentTextSize));
      fill(27, 27, 27); // Dark text for all cells
      textSize(currentTextSize); // Set the text size

      // Calculate text position for center alignment
      float textWidth = textWidth(cellText);
      if (textWidth > cellWidth) {
        cellText = truncateText(cellText, cellWidth);
        textWidth = textWidth(cellText); // Update text width after truncation
      }
      float textX = x + (cellWidth - textWidth) / 2; // Center horizontally
      float textY = y + cellHeight / 2 + currentTextSize / 4; // Center vertically

      // Draw cell text
      text(cellText, textX, textY);
    }
  }

  // Draw boundary around the entire table
  noFill();
  rect(tableX, tableY, tableWidth, tableHeight);
}

String truncateText(String text, float maxWidth) {
  float width = textWidth(text);
  while (width > maxWidth && text.length() > 0) {
    text = text.substring(0, text.length() - 1);
    width = textWidth(text + "...");
  }
  return text + "...";
}

String getHeaderText(int columnIndex) {
  switch (columnIndex) {
    case 0: return "D";
    case 1: return "C";
    case 2: return "C)";
    case 3: return "C";
    case 4: return "C";
    case 5: return "C";
    default: return "C" + (columnIndex + 1);
  }
}

void mouseClicked() {
  int row = (mouseY - int(tableY)) / int(cellHeight);
  int col = (mouseX - int(tableX)) / int(cellWidth);

  // Check if a new cell is clicked
  if (row >= 0 && row < numRows && col >= 0 && col < numCols) {
    // If we are already editing a cell, save the current input before sswitching
    if (isEditing) {
      cellTexts[editingRow][editingCol] = currentInput; // Save the current editing text
    }

    // Now switch to the new cell
    isEditing = true;
    editingRow = row;
    editingCol = col;
    currentInput = cellTexts[row][col]; // Load existing text of the new cell into the input buffer
  }
}

void keyTyped() {
  if (isEditing) {
    if (key == '\n' || key == RETURN) { // Enter key to finish editing
      cellTexts[editingRow][editingCol] = currentInput; // Save the input text
      isEditing = false;
    } else if (key == BACKSPACE && currentInput.length() > 0) {
      currentInput = currentInput.substring(0, currentInput.length() - 1); // Remove last character
    } else if (key >= 32 && key <= 126 && currentInput.length() < 7) { // Add only printable characters and limit to 7
      currentInput += key;
    }
  }
}

i want that

  1. cursor should create in text cell when i click on cell so that i can write , when move to another cell , the cursor should change to mouse default shape than when i wat to create text in another cell , cursor should create again
  2. the rows in column 7 from 1 to 30 should filled with white colour , should not drawing rows happen , just fill with white colour
1 Like

See cursor() / Reference / Processing.org

So when mousePressed registers the mouse is in cell set to TEXT otherwise to ARROW

Upon key return / ESC set to ARROW as well etc.

might be easier to write a class Cell in the long run

Can you explain the 2nd question a bit more?

1 Like

thank you very much for your reply ,
the 7th column should have white colour excluding from header

1 Like

After this line you can check if i==6 etc. and then set fill (255); or noFill();

Instead of using continue command

When using ESC use key=0; to kill Escape so that
Processing doesn’t leave your Sketch

1 Like

therer will be mathematical calculation will happen in column 7 so how to make this processing optimize

1 Like

Not sure what you mean

Okay, when the last column of cellTexts
is only for computed results, maybe you
shouldn’t display it here in the first place.

It’s also not where you can click with the mouse.
(Use if(mouseX>…) to skip the column

Instead, display the last column separately in a new for loop

Table table; 
int numRows = 30; // Number of rows
int numCols = 7; // Number of columns
float cellWidth = 100; // Adjustable cell width
float cellHeight = 27; // Adjustable cell height
float tableWidth, tableHeight;
float cellWidthColumn7 = 200; // Adjusted width for column 7
float tableX = 150; 
float tableY = 50; 

PFont arialBold;
String[][] cellTexts;
boolean isEditing = false;
int editingRow = -1, editingCol = -1;
String currentInput = "";

// Variables for movable equation
float equationYOffset = 0; // Vertical offset for the equation
float equationStep = 5; // Step size for moving the equation up and down

void setup() {
  size(1800, 980);
  surface.setResizable(true);
  arialBold = createFont("Arial-BoldMT", 12);
  table = new Table();
  table.addColumn("A"); // First column heading
  table.addColumn("B (mm)");   // Second column heading
  table.addColumn("C (mm)");   // Third column heading
  table.addColumn("D(mm)");    // Fourth column heading
  table.addColumn("E (mm)"); // Fifth column heading
  table.addColumn("F"); // Sixth column heading
  for (int i = 6; i < numCols; i++) {
    table.addColumn("Column " + (i + 1));
  }

  cellTexts = new String[numRows][numCols];
  for (int i = 0; i < numRows; i++) {
    for (int j = 0; j < numCols; j++) {
      cellTexts[i][j] = i == 0 ? getHeaderText(j) : "";
    }
  }

  tableWidth = (numCols - 1) * cellWidth + cellWidthColumn7;
  tableHeight = numRows * cellHeight;
}

void drawColumn7Cells() {
  float x = 6 * cellWidth + tableX;
  float y = tableY;

  // Draw the heading for column 7
  fill(173, 216, 230); // Light blue background for heading
  stroke(0);
  rect(x, y, cellWidthColumn7, cellHeight); // Draw heading cell
  fill(27, 27, 27); // Dark text color
  String headingText = getHeaderText(6); // Get heading for column 7
  float headingTextWidth = textWidth(headingText);
  float headingTextX = x + (cellWidthColumn7 - headingTextWidth) / 2;
  float headingTextY = y + cellHeight / 2 + 6;
  text(headingText, headingTextX, headingTextY);

  // Start drawing from the first row after the heading
  float startY = 1;
  float numRowsInBox = 29;
  y = startY * cellHeight + tableY; // Adjust y position for the first row
  float height = numRowsInBox * cellHeight;

  fill(255);
  stroke(0);
  strokeWeight(1);
  rect(x, y, cellWidthColumn7, height); // Draw the white box

  // Equation text
  String equationText = "Equation = D+E+F+G+H+I";
  textFont(arialBold);
  fill(0);
  textSize(12);
  float textWidth = textWidth(equationText);
  float textX = x + (cellWidthColumn7 - textWidth) / 2;
  float textY = y + equationYOffset + 360; // Adjust for equation offset
  text(equationText, textX, textY);

  // Constrain the equationYOffset to keep the equation within the box
  equationYOffset = constrain(equationYOffset, -height + cellHeight, 0);
}


void draw() {
  background(200);
  for (int i = 0; i < numRows; i++) {
    for (int j = 0; j < numCols - 1; j++) {
      float x = j * cellWidth + tableX;
      float y = i * cellHeight + tableY;
      fill(i == 0 ? 173 : 255, i == 0 ? 216 : 255, i == 0 ? 230 : 255);
      stroke(0);
      rect(x, y, cellWidth, cellHeight);

      String cellText = (isEditing && i == editingRow && j == editingCol) ? currentInput : cellTexts[i][j];
      textFont(i == 0 ? arialBold : createFont("Arial", 12));
      fill(27, 27, 27);
      textSize(12);
      float textWidth = textWidth(cellText);
      if (textWidth > cellWidth) {
        cellText = truncateText(cellText, cellWidth);
        textWidth = textWidth(cellText);
      }
      float textX = x + (cellWidth - textWidth) / 2;
      float textY = y + cellHeight / 2 + 6;
      text(cellText, textX, textY);
    }
  }

  drawColumn7Cells();
  noFill();
  rect(tableX, tableY, tableWidth, tableHeight);
}

void keyPressed() {
  if (keyCode == UP) {
    equationYOffset -= equationStep; // Move the equation up
  } else if (keyCode == DOWN) {
    equationYOffset += equationStep; // Move the equation down
  }
  // Constrain the offset to keep the equation within the white box
  equationYOffset = constrain(equationYOffset, 0, (29 - 1) * cellHeight - 30); 
}

String truncateText(String text, float maxWidth) {
  float width = textWidth(text);
  while (width > maxWidth && text.length() > 0) {
    text = text.substring(0, text.length() - 1);
    width = textWidth(text + "...");
  }
  return text + "...";
}

String getHeaderText(int columnIndex) {
  switch (columnIndex) {
    case 0: return "A";
    case 1: return "B";
    case 2: return "C";
    case 3: return "D";
    case 4: return "E";
    case 5: return "F";
    default: return "Column " + (columnIndex + 1);
  }
}

void mouseClicked() {
  int row = (mouseY - int(tableY)) / int(cellHeight);
  int col = (mouseX - int(tableX)) / int(cellWidth);
  if (row >= 0 && row < numRows && col >= 0 && col < numCols) {
    if (isEditing) {
      cellTexts[editingRow][editingCol] = currentInput;
    }
    isEditing = true;
    editingRow = row;
    editingCol = col;
    currentInput = cellTexts[row][col];
  }
}

void keyTyped() {
  if (isEditing) {
    if (key == '\n' || key == RETURN) {
      cellTexts[editingRow][editingCol] = currentInput;
      isEditing = false;
    } else if (key == BACKSPACE && currentInput.length() > 0) {
      currentInput = currentInput.substring(0, currentInput.length() - 1);
    } else if (key >= 32 && key <= 126 && currentInput.length() < 7) {
      currentInput += key;
    }
  }
}


i want to make prgrame in such away that user enter all digits in ever row from column 1 to 6 , than transfer these values to column 7 and perform mathemtiical equation and stored the result from column 7 to another column 8 in every row corresponding

1 Like

I don’t know what your question is

My answer is still valid

For text cursor see caret here: Help needed on loading table and creating search box to search filter data (Bible) - #13 by Chrisir

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

Okay

As I said fill the table with the 2 for loops
to column 6

After the 2 for loops draw the next column separately

i did not understand please can you elaborate

Sorry I am not at home but on a longer journey

So I can’t really do it for you

What I mean is when the 7th column has only 2 rows, dont make the column part of the
2D array

Please try to treat this column in a separate array or with 2 new variables

Do the output separately as well

1 Like

aha got it going to try it thank you very much

1 Like