home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 July / CHIP-1999-07.iso / software / jdk / jdk121.exe / disk1 / data1.cab / demos / demo / jfc / Table / TableExample3.java < prev    next >
Encoding:
Java Source  |  1999-03-27  |  4.3 KB  |  101 lines

  1. /*
  2.  * @(#)TableExample3.java    1.8 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. /**
  16.  * An example showing the JTable with a dataModel that is not derived
  17.  * from a database. We add the optional TableSorter object to give the
  18.  * JTable the ability to sort.
  19.  *
  20.  * @version 1.3 10/14/97
  21.  * @author Philip Milne
  22.  */
  23.  
  24. import javax.swing.*;
  25. import javax.swing.table.*;
  26.  
  27. import java.awt.event.WindowAdapter;
  28. import java.awt.event.WindowEvent;
  29. import java.awt.Dimension;
  30.  
  31. public class TableExample3 {
  32.  
  33.     public TableExample3() {
  34.         JFrame frame = new JFrame("Table");
  35.         frame.addWindowListener(new WindowAdapter() {
  36.             public void windowClosing(WindowEvent e) {System.exit(0);}});
  37.  
  38.         // Take the dummy data from SwingSet.
  39.         final String[] names = {"First Name", "Last Name", "Favorite Color",
  40.                                 "Favorite Number", "Vegetarian"};
  41.         final Object[][] data = {
  42.         {"Mark", "Andrews", "Red", new Integer(2), new Boolean(true)},
  43.         {"Tom", "Ball", "Blue", new Integer(99), new Boolean(false)},
  44.         {"Alan", "Chung", "Green", new Integer(838), new Boolean(false)},
  45.         {"Jeff", "Dinkins", "Turquois", new Integer(8), new Boolean(true)},
  46.         {"Amy", "Fowler", "Yellow", new Integer(3), new Boolean(false)},
  47.         {"Brian", "Gerhold", "Green", new Integer(0), new Boolean(false)},
  48.         {"James", "Gosling", "Pink", new Integer(21), new Boolean(false)},
  49.         {"David", "Karlton", "Red", new Integer(1), new Boolean(false)},
  50.         {"Dave", "Kloba", "Yellow", new Integer(14), new Boolean(false)},
  51.         {"Peter", "Korn", "Purple", new Integer(12), new Boolean(false)},
  52.         {"Phil", "Milne", "Purple", new Integer(3), new Boolean(false)},
  53.         {"Dave", "Moore", "Green", new Integer(88), new Boolean(false)},
  54.         {"Hans", "Muller", "Maroon", new Integer(5), new Boolean(false)},
  55.         {"Rick", "Levenson", "Blue", new Integer(2), new Boolean(false)},
  56.         {"Tim", "Prinzing", "Blue", new Integer(22), new Boolean(false)},
  57.         {"Chester", "Rose", "Black", new Integer(0), new Boolean(false)},
  58.         {"Ray", "Ryan", "Gray", new Integer(77), new Boolean(false)},
  59.         {"Georges", "Saab", "Red", new Integer(4), new Boolean(false)},
  60.         {"Willie", "Walker", "Phthalo Blue", new Integer(4), new Boolean(false)},
  61.         {"Kathy", "Walrath", "Blue", new Integer(8), new Boolean(false)},
  62.         {"Arnaud", "Weber", "Green", new Integer(44), new Boolean(false)}
  63.         };
  64.  
  65.         // Create a model of the data.
  66.         TableModel dataModel = new AbstractTableModel() {
  67.             // These methods always need to be implemented.
  68.             public int getColumnCount() { return names.length; }
  69.             public int getRowCount() { return data.length;}
  70.             public Object getValueAt(int row, int col) {return data[row][col];}
  71.  
  72.             // The default implementations of these methods in
  73.             // AbstractTableModel would work, but we can refine them.
  74.             public String getColumnName(int column) {return names[column];}
  75.             public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
  76.             public boolean isCellEditable(int row, int col) {return (col==4);}
  77.             public void setValueAt(Object aValue, int row, int column) {
  78.                 data[row][column] = aValue;
  79.             }
  80.          };
  81.  
  82.         // Instead of making the table display the data as it would normally with:
  83.         // JTable tableView = new JTable(dataModel);
  84.         // Add a sorter, by using the following three lines instead of the one above.
  85.         TableSorter  sorter = new TableSorter(dataModel);
  86.         JTable    tableView = new JTable(sorter);
  87.         sorter.addMouseListenerToHeaderInTable(tableView);
  88.  
  89.         JScrollPane scrollpane = new JScrollPane(tableView);
  90.  
  91.         scrollpane.setPreferredSize(new Dimension(700, 300));
  92.         frame.getContentPane().add(scrollpane);
  93.         frame.pack();
  94.         frame.setVisible(true);
  95.     }
  96.  
  97.     public static void main(String[] args) {
  98.         new TableExample3();
  99.     }
  100. }
  101.