/* 3magic Coffee Table Tutorial 10 Tutorial10.java - Custom Data and Sorting. © 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; public class Tutorial10 extends Applet { // Our grid. GridPanel fGrid; // Rows and Columns; int fNumCols = 9; 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 Grid Panel with the specified rows and columns // and other defaults like cell selection, row and column headers, // row and column lines enabled and with scrollbars. fGrid = new GridPanel(fNumRows, fNumCols); // 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)); // CONFIGURE SORTING ------------------------------------------ // Enable sorting for all columns fGrid.setSortEnable(true); // Set sort column color to a very light gray. fGrid.setSortColumnColor(new Color(0xEFEFEF)); // Add the grid into the applet panel add("Center", fGrid); // Read in the data. this.readData("presidents.txt"); } /** * Read data from text file. */ void readData(String dataFile) { if (dataFile != null) { try { // Open the file. DataInputStream aStream = new DataInputStream(this.getClass().getResourceAsStream(dataFile)); if (aStream != null) { BufferedReader in = new BufferedReader(new InputStreamReader(aStream)); // 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); } } // 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.getGridData().setCellData(col, row, MyRank.parseMyRank(str)); } else if (col == 6) { // Column 6 are the birthdates. fGrid.getGridData().setCellData(col, row, MyDate.parseMyDate(str)); } else { // Other columns can be treated as text. fGrid.setCellText(col, row, str, false); } } } row++; } } } catch (Exception ex) { System.out.println("Error: " + ex); } } } }