/* 3magic Coffee Table Tutorial 11 Tutorial11.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 CoffeeTable.Grid.*; import MyDate; import MyRank; import MyGridPanel; public class Tutorial11 extends Applet { // Our grid. GridPanel fGrid; // Rows and Columns; int fNumCols = 10; int fNumRows = 42; /** * 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.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); // Read in the data. this.readData("presidents.11.txt"); Label message = new Label(""); message.setFont(new Font("TimesRoman", Font.PLAIN, 10)); message.setText("Argent Coffee Table version " + fGrid.getVersionString() + " \u00A9 1996-1998 Argent Software"); add("South", message); } /** * Read data from text file. */ void readData(String dataFile) { if (dataFile != null) { try { // Open the file. URL url = new URL(getCodeBase(), dataFile); DataInputStream in = new DataInputStream(url.openStream()); // The first line are the column widths String line = in.readLine(); StringTokenizer st = new StringTokenizer(line, "\t"); for (int col = 1; col <= fNumCols; col++) { if (st.hasMoreTokens()) { // Set widths of each column. int width = Integer.parseInt(st.nextToken()); fGrid.setColWidth(col, width, true, true); } } // The second line are the column headers line = in.readLine(); st = new StringTokenizer(line, "\t"); for (int col = 1; col <= fNumCols; col++) { if (st.hasMoreTokens()) { // Set the labels for each column. fGrid.setColHeaderText(col, st.nextToken(), false); } } // Create a MediaTracker for the images. MediaTracker tracker = new MediaTracker(this); // The real data follows, read and parse one line at a time. int row = 1; while ( row <= fNumRows ) { line = in.readLine(); if (line == null) break; st = new StringTokenizer(line, "\t"); for (int col = 1; col <= fNumCols; col++) { if (st.hasMoreTokens()) { String str = st.nextToken(); if (col == 1) { // Column 1 are the rankings. fGrid.setCellData(col, row, MyRank.parseMyRank(str), false); } else if (col == 2) { // Column 2 are the images. URL imageURL = new URL(getCodeBase(), "presidents/"+str); Image im = Toolkit.getDefaultToolkit().getImage(imageURL); fGrid.setCellData(col, row, im, false); // Let's track it so we can wait for all to load. tracker.addImage(im, row); } else if (col == 7) { // Column 7 are the birthdates. fGrid.setCellData(col, row, MyDate.parseMyDate(str), false); } else { // Other columns can be treated as text. fGrid.setCellText(col, row, str, false); } } } row++; } // Wait for all the images to load. try { tracker.waitForAll(); } catch (Exception ex) { } } catch (Exception ex) { System.out.println("Error: " + ex); } } } }