/* 3magic Coffee Table Tutorial 15 Tutorial15.java - Custom Drawing of Cells. © 1997-1999 3magic. All rights reserved worldwide. */ import java.awt.*; import java.applet.*; import java.io.*; import java.net.*; import java.util.*; import java.awt.event.*; import CoffeeTable.Grid.*; import MyDate; import MyRank; import MyGridPanel; public class Tutorial15 extends Applet implements ActionListener { // Our grid. GridPanel fGrid; // Rows and Columns; int fNumCols = 10; int fNumRows = 42; // Some Buttons. int fNumButtons = 2; Button fButton[] = new Button[fNumButtons]; String buttonLabels[] = { "Delete Row", "Reload Data"}; // sub-class a GridAdapter to handle gridSelChanged events class MyGridAdapter extends GridAdapter { // handle selection changes by updating the buttons public void gridSelChanged(GridEvent e) { updateButtons(); } } /** * Initialize. */ public void init() { // Let's use a BorderLayout to help us with the user input areas. setLayout(new BorderLayout()); // Instantiate a new MyGridPanel with the specified rows and columns // and other defaults like cell selection, row and column headers, // row and column lines enabled and with scrollbars. // MyGridPanel is customized to do image drawing in column 2. fGrid = new MyGridPanel(fNumRows, fNumCols); // Make room for the images. fGrid.setRowHeight(32); // Show the row headers. fGrid.setRowHeaderWidth(40); fGrid.setRowNumbers(true); // Let's use Times, plain, 9-point. fGrid.setGridFont(new Font("TimesRoman", Font.PLAIN, 9)); // Set the background color to light yellow. fGrid.setGridBackground(new Color(255, 255, 230)); // Set the grid lines to red. fGrid.setLineColor(Color.red); // CONFIGURE SELECTIONS AND HIGHLIGHTING ----------------------- // Enable selection by rows. fGrid.setCellSelection(false); fGrid.setRowSelection(true); // Choose the selection highlighting colors. fGrid.setHighlightColor(Color.blue); // CONFIGURE SORTING ------------------------------------------ // Enable sorting for all columns fGrid.setSortEnable(true); // Disable sorting for the image column. fGrid.setSortEnable(2, false); // Set sort column color to a very light gray. fGrid.setSortColumnColor(new Color(0xEFEFEF)); // USE CUSTOM SORTING BY OVERRIDING GRIDDATA ------------------- fGrid.setGridData(new MyGridData(fNumRows, fNumCols), false); // Add the grid into the applet panel add("Center", fGrid); // add a gridListener to handle selection change events fGrid.addGridListener(new MyGridAdapter()); // Read in the data. this.readData("presidents.11.txt"); // Add some control buttons. Panel buttonPanel = new Panel(); add("South", buttonPanel); for (int i = 0; i 0) { fButton[0].setEnabled(true); } // Always enable reset. fButton[1].setEnabled(true); } /** * 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); } } /** * Reset the grid dimensions. */ public void resetGrid() { // Reset to original rows/columns. fGrid.setNumCols(fNumCols); fGrid.setNumRows(fNumRows); // Read in the data. this.readData("presidents.11.txt"); fGrid.repaint(); } /** * Catch the button presses. */ public void actionPerformed(ActionEvent evt) { boolean handled = false; if (evt.getSource() instanceof Button) { if (evt.getSource() == fButton[0]) { // Delete Row. this.deleteRow(); handled = true; } else if (evt.getSource() == fButton[1]) { // Reset Grid. this.resetGrid(); handled = true; } } this.updateButtons(); } }