001: package examples.swingdemos;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005:
006: import javax.swing.*;
007: import javax.swing.table.*;
008:
009: public class TableDemo extends JFrame {
010: boolean DEBUG = true;
011:
012: public TableDemo() {
013: super ("TableDemo");
014:
015: MyTableModel myModel = new MyTableModel();
016: JTable table = new JTable(myModel);
017: table
018: .setPreferredScrollableViewportSize(new Dimension(500,
019: 70));
020:
021: //Create the scroll pane and add the table to it.
022: JScrollPane scrollPane = new JScrollPane(table);
023:
024: //Add the scroll pane to this window.
025: getContentPane().add(scrollPane, BorderLayout.CENTER);
026:
027: getContentPane().setLayout(new GridLayout(0, 1));
028: JButton theSaveButon = new JButton("save");
029: theSaveButon.addActionListener(new ActionListener() {
030: public void actionPerformed(ActionEvent e) {
031:
032: }
033: });
034: getContentPane().add(theSaveButon);
035:
036: addWindowListener(new WindowAdapter() {
037: public void windowClosing(WindowEvent e) {
038: System.exit(0);
039: }
040: });
041: }
042:
043: class MyTableModel extends AbstractTableModel {
044: final String[] columnNames = { "First Name", "Last Name",
045: "Sport", "# of Years", "Vegetarian" };
046: final Object[][] data = {
047: { "Mary", "Campione", "Snowboarding", new Integer(5),
048: new Boolean(false) },
049: { "Alison", "Huml", "Rowing", new Integer(3),
050: new Boolean(true) },
051: { "Kathy", "Walrath", "Chasing toddlers",
052: new Integer(2), new Boolean(false) },
053: { "Sharon", "Zakhour", "Speed reading",
054: new Integer(20), new Boolean(true) },
055: { "Angela", "Lih", "Teaching high school",
056: new Integer(4), new Boolean(false) } };
057:
058: public int getColumnCount() {
059: return columnNames.length;
060: }
061:
062: public int getRowCount() {
063: return data.length;
064: }
065:
066: public String getColumnName(int col) {
067: return columnNames[col];
068: }
069:
070: public Object getValueAt(int row, int col) {
071: return data[row][col];
072: }
073:
074: /*
075: * JTable uses this method to determine the default renderer/
076: * editor for each cell. If we didn't implement this method,
077: * then the last column would contain text ("true"/"false"),
078: * rather than a check box.
079: */
080: public Class getColumnClass(int c) {
081: return getValueAt(0, c).getClass();
082: }
083:
084: /*
085: * Don't need to implement this method unless your table's
086: * editable.
087: */
088: public boolean isCellEditable(int row, int col) {
089: //Note that the data/cell address is constant,
090: //no matter where the cell appears onscreen.
091: if (col < 2) {
092: return false;
093: } else {
094: return true;
095: }
096: }
097:
098: /*
099: * Don't need to implement this method unless your table's
100: * data can change.
101: */
102: public void setValueAt(Object value, int row, int col) {
103: if (DEBUG) {
104: System.out.println("Setting value at " + row + ","
105: + col + " to " + value + " (an instance of "
106: + value.getClass() + ")");
107: }
108:
109: if (data[0][col] instanceof Integer
110: && !(value instanceof Integer)) {
111: //With JFC/Swing 1.1 and JDK 1.2, we need to create
112: //an Integer from the value; otherwise, the column
113: //switches to contain Strings. Starting with v 1.3,
114: //the table automatically converts value to an Integer,
115: //so you only need the code in the 'else' part of this
116: //'if' block.
117: //XXX: See TableEditDemo.java for a better solution!!!
118: try {
119: data[row][col] = new Integer(value.toString());
120: fireTableCellUpdated(row, col);
121: } catch (NumberFormatException e) {
122: JOptionPane
123: .showMessageDialog(
124: TableDemo.this ,
125: "The \""
126: + getColumnName(col)
127: + "\" column accepts only integer values.");
128: }
129: } else {
130: data[row][col] = value;
131: fireTableCellUpdated(row, col);
132: }
133:
134: if (DEBUG) {
135: System.out.println("New value of data:");
136: printDebugData();
137: }
138: }
139:
140: private void printDebugData() {
141: int numRows = getRowCount();
142: int numCols = getColumnCount();
143:
144: for (int i = 0; i < numRows; i++) {
145: System.out.print(" row " + i + ":");
146: for (int j = 0; j < numCols; j++) {
147: System.out.print(" " + data[i][j]);
148: }
149: System.out.println();
150: }
151: System.out.println("--------------------------");
152: }
153: }
154:
155: public static void main(String[] args) {
156: TableDemo frame = new TableDemo();
157: frame.pack();
158: frame.setVisible(true);
159: }
160: }
|