001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.swingweb.widgetdemo;
022:
023: import java.awt.Container;
024: import java.awt.Insets;
025: import javax.swing.*;
026: import javax.swing.event.*;
027: import javax.swing.table.DefaultTableModel;
028: import org.onemind.swingweb.component.layout.TableLayout;
029:
030: public class TableDemo extends AbstractDemo implements ChangeListener {
031:
032: public TableDemo(DemoConsole console) {
033: super ("Table", console);
034: add(getDemoArea());
035: }
036:
037: public Container getDemoArea() {
038: JPanel pnl = new JPanel();
039: TableLayout layout = new TableLayout(pnl);
040: layout.getConstraint().insets = new Insets(5, 5, 5, 5);
041: pnl.setLayout(layout);
042:
043: //Simple table with table listener
044: JTable t = getExampleTable();
045: layout
046: .addNextRow(new JLabel(
047: "Simple table with TableListener (will submit on change)"));
048: layout.addNextRow(t);
049:
050: //Simple table without table listener
051: t = getExampleTable();
052: layout.addNextRow(new JLabel(
053: "Simple table without TableListener"));
054: layout.addNextRow(t);
055:
056: //Simple table without table listener, with validation renderer
057: t = getExampleTable();
058: layout
059: .addNextRow(new JLabel(
060: "Simple table without TableListener, with int field formater"));
061: //t.setCellEditor(anEditor);
062: layout.addNextRow(t);
063:
064: addListeners(t);
065: t = getExampleTable();
066: t.setEnabled(false);
067: layout.addNextRow(new JLabel("Disabled table"));
068: layout.addNextRow(t);
069: return pnl;
070: }
071:
072: private void addListeners(JTable table) {
073: table.getModel().addTableModelListener(
074: new javax.swing.event.TableModelListener() {
075:
076: public void tableChanged(
077: javax.swing.event.TableModelEvent e) {
078: getConsole().log(
079: "TableModelEvent at " + e.getFirstRow()
080: + ", " + e.getColumn() + "\n");
081: }
082: });
083: table.getSelectionModel().addListSelectionListener(
084: new ListSelectionListener() {
085:
086: public void valueChanged(ListSelectionEvent e) {
087: getConsole().log(e.toString());
088: }
089: });
090: table.getColumnModel().addColumnModelListener(
091: new TableColumnModelListener() {
092:
093: public void columnMarginChanged(ChangeEvent e) {
094: getConsole().log(e.toString());
095: }
096:
097: public void columnSelectionChanged(
098: ListSelectionEvent e) {
099: getConsole().log(e.toString());
100: }
101:
102: public void columnAdded(TableColumnModelEvent e) {
103: getConsole().log(e.toString());
104: }
105:
106: public void columnMoved(TableColumnModelEvent e) {
107: getConsole().log(e.toString());
108: }
109:
110: public void columnRemoved(TableColumnModelEvent e) {
111: getConsole().log(e.toString());
112: }
113: });
114: }
115:
116: private JTable getExampleTable() {
117: String[] columnNames = new String[] { "First Name",
118: "Last Name", "Age", "Married" };
119: Object[][] datas = new Object[][] {
120: { "John", "Smith", new Integer(24), null },
121: { "Mary", "Moo", new Integer(23), new Boolean(true) },
122: { "Anthony", "Hudson", new Integer(44), null },
123: { "Tom", "Wood", null, null } };
124: JTable table = new JTable();
125: table.setModel(new DefaultTableModel(datas, columnNames) {
126:
127: Class[] types = new Class[] { java.lang.String.class,
128: java.lang.String.class, java.lang.Integer.class,
129: java.lang.Boolean.class };
130:
131: public Class getColumnClass(int columnIndex) {
132: return types[columnIndex];
133: }
134: });
135: return table;
136: }
137:
138: public void stateChanged(ChangeEvent e) {
139: getConsole().log(e.toString());
140: }
141: }
|