source: tmcsimulator/trunk/src/spikes/TableDialogEditDemo.java @ 59

Revision 59, 5.5 KB checked in by jdalbey, 9 years ago (diff)

Merge CAD Client updates for multiple incident view windows.

Line 
1package spikes;
2
3/*
4 * TableDialogEditDemo.java requires these files:
5 *   ColorRenderer.java
6 *   ColorEditor.java
7 */
8import java.awt.Color;
9import java.awt.Dimension;
10import java.awt.GridLayout;
11import javax.swing.JComponent;
12import javax.swing.JFrame;
13import javax.swing.JPanel;
14import javax.swing.JScrollPane;
15import javax.swing.JTable;
16import javax.swing.table.AbstractTableModel;
17
18/**
19 * This is like TableDemo, except that it substitutes a Favorite Color column
20 * for the Last Name column and specifies a custom cell renderer and editor for
21 * the color data.
22 */
23public class TableDialogEditDemo extends JPanel
24{
25    private boolean DEBUG = false;
26
27    public TableDialogEditDemo()
28    {
29        super(new GridLayout(1, 0));
30
31        JTable table = new JTable(new MyTableModel());
32        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
33        table.setFillsViewportHeight(true);
34
35        //Create the scroll pane and add the table to it.
36        JScrollPane scrollPane = new JScrollPane(table);
37
38        //Set up renderer and editor for the Favorite Color column.
39        table.setDefaultRenderer(Color.class,
40                new ColorRenderer(true));
41        table.setDefaultRenderer(String.class,
42                //new StringRenderer());
43                new SimpleWordRenderer());
44
45        //Add the scroll pane to this panel.
46        add(scrollPane);
47    }
48
49    class MyTableModel extends AbstractTableModel
50    {
51        private String[] columnNames =
52        {
53            "First Name",
54            "Favorite Color",
55            "Sport",
56            "# of Years",
57            "Vegetarian"
58        };
59        private Object[][] data =
60        {
61            {
62                "Mary", new Color(153, 0, 153),
63                "Snowboarding", new Integer(5), new Boolean(false)
64            },
65            {
66                "Alison", new Color(51, 51, 153),
67                "Rowing", new Integer(3), new Boolean(true)
68            },
69            {
70                "Kathy", new Color(51, 102, 51),
71                "Knitting", new Integer(2), new Boolean(false)
72            },
73            {
74                "Sharon", Color.red,
75                "Speed reading", new Integer(20), new Boolean(true)
76            },
77            {
78                "Philip", Color.pink,
79                "Pool", new Integer(10), new Boolean(false)
80            }
81        };
82
83        public int getColumnCount()
84        {
85            return columnNames.length;
86        }
87
88        public int getRowCount()
89        {
90            return data.length;
91        }
92
93        public String getColumnName(int col)
94        {
95            return columnNames[col];
96        }
97
98        public Object getValueAt(int row, int col)
99        {
100            return data[row][col];
101        }
102
103        /*
104         * JTable uses this method to determine the default renderer/
105         * editor for each cell.  If we didn't implement this method,
106         * then the last column would contain text ("true"/"false"),
107         * rather than a check box.
108         */
109        public Class getColumnClass(int c)
110        {
111            return getValueAt(0, c).getClass();
112        }
113
114        public boolean isCellEditable(int row, int col)
115        {
116            //Note that the data/cell address is constant,
117            //no matter where the cell appears onscreen.
118            if (col < 1)
119            {
120                return false;
121            }
122            else
123            {
124                return true;
125            }
126        }
127
128        public void setValueAt(Object value, int row, int col)
129        {
130            if (DEBUG)
131            {
132                System.out.println("Setting value at " + row + "," + col
133                        + " to " + value
134                        + " (an instance of "
135                        + value.getClass() + ")");
136            }
137
138            data[row][col] = value;
139            fireTableCellUpdated(row, col);
140
141            if (DEBUG)
142            {
143                System.out.println("New value of data:");
144                printDebugData();
145            }
146        }
147
148        private void printDebugData()
149        {
150            int numRows = getRowCount();
151            int numCols = getColumnCount();
152
153            for (int i = 0; i < numRows; i++)
154            {
155                System.out.print("    row " + i + ":");
156                for (int j = 0; j < numCols; j++)
157                {
158                    System.out.print("  " + data[i][j]);
159                }
160                System.out.println();
161            }
162            System.out.println("--------------------------");
163        }
164    }
165
166    /**
167     * Create the GUI and show it. For thread safety, this method should be
168     * invoked from the event-dispatching thread.
169     */
170    private static void createAndShowGUI()
171    {
172        //Create and set up the window.
173        JFrame frame = new JFrame("TableDialogEditDemo");
174        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
175
176        //Create and set up the content pane.
177        JComponent newContentPane = new TableDialogEditDemo();
178        newContentPane.setOpaque(true); //content panes must be opaque
179        frame.setContentPane(newContentPane);
180
181        //Display the window.
182        frame.pack();
183        frame.setVisible(true);
184    }
185
186    public static void main(String[] args)
187    {
188        //Schedule a job for the event-dispatching thread:
189        //creating and showing this application's GUI.
190        javax.swing.SwingUtilities.invokeLater(new Runnable()
191        {
192            public void run()
193            {
194                createAndShowGUI();
195            }
196        });
197    }
198}
Note: See TracBrowser for help on using the repository browser.