1 36 37 40 41 49 50 import javax.swing.*; 51 import javax.swing.table.*; 52 53 import java.awt.event.WindowAdapter ; 54 import java.awt.event.WindowEvent ; 55 import java.awt.Dimension ; 56 57 public class TableExample3 { 58 59 public TableExample3() { 60 JFrame frame = new JFrame("Table"); 61 frame.addWindowListener(new WindowAdapter () { 62 public void windowClosing(WindowEvent e) {System.exit(0);}}); 63 64 final String [] names = {"First Name", "Last Name", "Favorite Color", 66 "Favorite Number", "Vegetarian"}; 67 final Object [][] data = { 68 {"Mark", "Andrews", "Red", new Integer (2), Boolean.TRUE}, 69 {"Tom", "Ball", "Blue", new Integer (99), Boolean.FALSE}, 70 {"Alan", "Chung", "Green", new Integer (838), Boolean.FALSE}, 71 {"Jeff", "Dinkins", "Turquois", new Integer (8), Boolean.TRUE}, 72 {"Amy", "Fowler", "Yellow", new Integer (3), Boolean.FALSE}, 73 {"Brian", "Gerhold", "Green", new Integer (0), Boolean.FALSE}, 74 {"James", "Gosling", "Pink", new Integer (21), Boolean.FALSE}, 75 {"David", "Karlton", "Red", new Integer (1), Boolean.FALSE}, 76 {"Dave", "Kloba", "Yellow", new Integer (14), Boolean.FALSE}, 77 {"Peter", "Korn", "Purple", new Integer (12), Boolean.FALSE}, 78 {"Phil", "Milne", "Purple", new Integer (3), Boolean.FALSE}, 79 {"Dave", "Moore", "Green", new Integer (88), Boolean.FALSE}, 80 {"Hans", "Muller", "Maroon", new Integer (5), Boolean.FALSE}, 81 {"Rick", "Levenson", "Blue", new Integer (2), Boolean.FALSE}, 82 {"Tim", "Prinzing", "Blue", new Integer (22), Boolean.FALSE}, 83 {"Chester", "Rose", "Black", new Integer (0), Boolean.FALSE}, 84 {"Ray", "Ryan", "Gray", new Integer (77), Boolean.FALSE}, 85 {"Georges", "Saab", "Red", new Integer (4), Boolean.FALSE}, 86 {"Willie", "Walker", "Phthalo Blue", new Integer (4), Boolean.FALSE}, 87 {"Kathy", "Walrath", "Blue", new Integer (8), Boolean.FALSE}, 88 {"Arnaud", "Weber", "Green", new Integer (44), Boolean.FALSE} 89 }; 90 91 TableModel dataModel = new AbstractTableModel() { 93 public int getColumnCount() { return names.length; } 95 public int getRowCount() { return data.length;} 96 public Object getValueAt(int row, int col) {return data[row][col];} 97 98 public String getColumnName(int column) {return names[column];} 101 public Class getColumnClass(int col) {return getValueAt(0,col).getClass();} 102 public boolean isCellEditable(int row, int col) {return (col==4);} 103 public void setValueAt(Object aValue, int row, int column) { 104 data[row][column] = aValue; 105 } 106 }; 107 108 TableSorter sorter = new TableSorter(dataModel); 112 JTable tableView = new JTable(sorter); 113 sorter.addMouseListenerToHeaderInTable(tableView); 114 115 JScrollPane scrollpane = new JScrollPane(tableView); 116 117 scrollpane.setPreferredSize(new Dimension (700, 300)); 118 frame.getContentPane().add(scrollpane); 119 frame.pack(); 120 frame.setVisible(true); 121 } 122 123 public static void main(String [] args) { 124 new TableExample3(); 125 } 126 } 127 | Popular Tags |