/* 3magic Coffee Table Tutorial 6 Tutorial6.java - Adding and Deleting Rows and Columns. © 1997-1999 3magic. All rights reserved worldwide. */ import java.awt.*; import java.applet.*; import java.util.*; import CoffeeTable.Grid.*; public class Tutorial6 extends Applet { // Labels for columns. String colLabels[] = {"First", "Last", "Fruit", "Color", "Salary"}; // Sample data. String cellData[][] = { {"Ray", "Yoshi", "apple", "red", "$50,000"}, {"Neal", "Bajaj", "mango", "orange", "$60,000"}, {"Greg", "Finn", "kiwi", "green", "$90,000"}, {"Fred", "Mertz", "banana", "yellow", "$30,000"}, {"Ethyl", "Nertz", "blueberry", "blue", "$35,000"}, {"Lucy", "Ricardo", "cherry", "magneta", "$40,000"}, {"Ricky", "Arnaz", "coconut", "white", "$45,000"}, {"Jeremy", "King", "watermelon", "pink", "$15,000"}, {"Thumper", "Lapin", "grape", "purple", "$25,000"}, {"Seargent", "Argent", "blackberry", "black?", "$65,000"}}; // Our grid. GridPanel fGrid; // Rows and Columns; int fNumRows = 10; int fNumCols = 5; // Some Buttons. int fNumButtons = 5; Button fButton[] = new Button[fNumButtons]; String buttonLabels[] = { "Insert Row", "Delete Row", "Insert Column", "Delete Column", "Reset Grid"}; // A label. Label fLabel; // A counter. int fCounter = 1; /** * Initialize. */ public void init() { // Instantiate a new Grid Panel with 10 rows and 5 columns // and other defaults like cell selection, row and column headers, // row and column lines enabled and with scrollbars. fGrid = new GridPanel(fNumRows, fNumCols); // Be sure to set the preferred size if you are using it in a // layout that resizes based on preferred size. fGrid.resize(400, 200); // Show the row headers. fGrid.setRowHeaderWidth(30); fGrid.setRowNumbers(true); // CONFIGURE SELECTIONS AND HIGHLIGHTING ----------------------- // Enable selection by rows or columns only. fGrid.setCellSelection(false); fGrid.setRowSelection(true); fGrid.setColSelection(true); // Choose the selection highlighting colors. fGrid.setHighlightColor(Color.yellow); fGrid.setHighlightTextColor(Color.red); // Add the grid into the applet panel add(fGrid); // Add some data for display. this.loadData(); // Add some control buttons. for (int i = 0; i 0) { fButton[0].enable(); fButton[1].enable(); } // Check if we have a column selected. int col = fGrid.getFirstSelectedCol(); if (col > 0) { fButton[2].enable(); fButton[3].enable(); } // Update the label too. fLabel.setText("Grid Size = " + fGrid.getNumCols() + " x " + fGrid.getNumRows()); } /** * Insert a row. */ public void insertRow() { // Check if we have a row selected. int row = fGrid.getFirstSelectedRow(); if (row > 0) { // Insert a row - this modifies the underlying GridData too. fGrid.insertRow(row); // Put in some dummy data for fun. for (int col=1; col <= fGrid.getNumCols(); col++) { fGrid.setCellText(col, row, "NewRow " + fCounter, true); } fCounter++; } } /** * Delete a row. */ public void deleteRow() { // Check if we have a row selected. int row = fGrid.getFirstSelectedRow(); if (row > 0) { // Insert a row - this modifies the underlying GridData too. fGrid.deleteRow(row); } } /** * Insert a column. */ public void insertColumn() { // Check if we have a row selected. int col = fGrid.getFirstSelectedCol(); if (col > 0) { // Insert a column - this modifies the underlying GridData too. fGrid.insertCol(col); // Put in some dummy data for fun. fGrid.setColHeaderText(col, "NewCol " + fCounter, true); for (int row=1; row <= fGrid.getNumRows(); row++) { fGrid.setCellText(col, row, "NewCol " + fCounter, true); } fCounter++; } } /** * Delete a column. */ public void deleteColumn() { // Check if we have a row selected. int col = fGrid.getFirstSelectedCol(); if (col > 0) { // Insert a row - this modifies the underlying GridData too. fGrid.deleteCol(col); } } /** * Reset the grid dimensions. */ public void resetGrid() { // Reset to original rows/columns. fGrid.setNumCols(fNumCols); fGrid.setNumRows(fNumRows); // Reload the data. this.loadData(); } /** * Catch the button presses. */ public boolean action(Event evt, Object arg) { boolean handled = false; if (evt.target instanceof Button) { if (evt.target == fButton[0]) { // Insert Row. this.insertRow(); handled = true; } else if (evt.target == fButton[1]) { // Delete Row. this.deleteRow(); handled = true; } else if (evt.target == fButton[2]) { // Insert Column. this.insertColumn(); handled = true; } else if (evt.target == fButton[3]) { // Delete Column. this.deleteColumn(); handled = true; } else if (evt.target == fButton[4]) { // Reset Grid. this.resetGrid(); handled = true; } } this.updateButtons(); return handled; } }