/* 3magic Coffee Table Tutorial 17 Tutorial17.java - Selection change handling. © 1997-1999 3magic. All rights reserved worldwide. */ import java.awt.*; import java.applet.*; import java.util.*; import CoffeeTable.Grid.*; public class Tutorial17 extends Applet { // Our grid. GridPanel fGrid; // Rows and Columns; int fNumCols = 6; int fNumRows = 50; // Message area. TextArea fMsg = new TextArea("Manipulate the grid with the mouse.\n", 10, 1); // sub-class a GridAdapter to handle gridSelChanged events class MyGridAdapter extends GridAdapter { // handle selection changes public void gridSelChanged(GridEvent e) { // Show the event info in the message area. fMsg.append("Current selection: ["); Vector selRows = fGrid.getSelectedRows(); for (int i = 0; i < selRows.size(); i++) { if (i > 0) fMsg.append(", "); fMsg.append(((Integer)selRows.elementAt(i)).toString()); } fMsg.append("] \n"); } } /** * 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); // Enable editing. fGrid.setGridAttributes(new GridAttributes(fGrid, new Font("Courier", Font.PLAIN, 12), null, null, 0, GridPanel.TEXT), true); // Enable demo text for the Grid fGrid.setDemoText(true); // Show the row headers. fGrid.setRowHeaderWidth(40); fGrid.setRowNumbers(true); // Turn on row and multiple selection, turn off cell selection fGrid.setMultipleSelection(true); fGrid.setRowSelection(true); fGrid.setCellSelection(false); // Let's use Times, plain, 9-point. fGrid.setGridFont(new Font("TimesRoman", Font.PLAIN, 9)); // Add the grid into the applet panel add("Center", fGrid); // add a gridListener to handle selection change events fGrid.addGridListener(new MyGridAdapter()); // Add the message area. add("South", fMsg); } }