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
- 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
- the rows in column 7 from 1 to 30 should filled with white colour , should not drawing rows happen , just fill with white colour