1 /* 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This source code is provided to illustrate the usage of a given feature 34 * or technique and has been deliberately simplified. Additional steps 35 * required for a production-quality application, such as security checks, 36 * input validation and proper error handling, might not be present in 37 * this sample code. 38 */ 39 40 41 42 import javax.swing.*; 43 import javax.swing.table.*; 44 45 import java.awt.event.WindowAdapter; 46 import java.awt.event.WindowEvent; 47 import java.awt.Dimension; 48 import java.util.logging.Level; 49 import java.util.logging.Logger; 50 import javax.swing.UIManager.LookAndFeelInfo; 51 52 53 /** 54 * An example showing the JTable with a dataModel that is not derived 55 * from a database. We add the optional TableSorter object to give the 56 * JTable the ability to sort. 57 * 58 * @author Philip Milne 59 */ 60 public class TableExample3 { 61 62 public TableExample3() { 63 JFrame frame = new JFrame("Table"); 64 frame.addWindowListener(new WindowAdapter() { 65 66 @Override 67 public void windowClosing(WindowEvent e) { 68 System.exit(0); 69 } 70 }); 71 72 // Take the dummy data from SwingSet. 73 final String[] names = { "First Name", "Last Name", "Favorite Color", 74 "Favorite Number", "Vegetarian" }; 75 final Object[][] data = { 76 { "Mark", "Andrews", "Red", new Integer(2), Boolean.TRUE }, 77 { "Tom", "Ball", "Blue", new Integer(99), Boolean.FALSE }, 78 { "Alan", "Chung", "Green", new Integer(838), Boolean.FALSE }, 79 { "Jeff", "Dinkins", "Turquois", new Integer(8), Boolean.TRUE }, 80 { "Amy", "Fowler", "Yellow", new Integer(3), Boolean.FALSE }, 81 { "Brian", "Gerhold", "Green", new Integer(0), Boolean.FALSE }, 82 { "James", "Gosling", "Pink", new Integer(21), Boolean.FALSE }, 83 { "David", "Karlton", "Red", new Integer(1), Boolean.FALSE }, 84 { "Dave", "Kloba", "Yellow", new Integer(14), Boolean.FALSE }, 85 { "Peter", "Korn", "Purple", new Integer(12), Boolean.FALSE }, 86 { "Phil", "Milne", "Purple", new Integer(3), Boolean.FALSE }, 87 { "Dave", "Moore", "Green", new Integer(88), Boolean.FALSE }, 88 { "Hans", "Muller", "Maroon", new Integer(5), Boolean.FALSE }, 89 { "Rick", "Levenson", "Blue", new Integer(2), Boolean.FALSE }, 90 { "Tim", "Prinzing", "Blue", new Integer(22), Boolean.FALSE }, 91 { "Chester", "Rose", "Black", new Integer(0), Boolean.FALSE }, 92 { "Ray", "Ryan", "Gray", new Integer(77), Boolean.FALSE }, 93 { "Georges", "Saab", "Red", new Integer(4), Boolean.FALSE }, 94 { "Willie", "Walker", "Phthalo Blue", new Integer(4), Boolean.FALSE }, 95 { "Kathy", "Walrath", "Blue", new Integer(8), Boolean.FALSE }, 96 { "Arnaud", "Weber", "Green", new Integer(44), Boolean.FALSE } 97 }; 98 99 // Create a model of the data. 100 @SuppressWarnings("serial") 101 TableModel dataModel = new AbstractTableModel() { 102 // These methods always need to be implemented. 103 104 public int getColumnCount() { 105 return names.length; 106 } 107 108 public int getRowCount() { 109 return data.length; 110 } 111 112 public Object getValueAt(int row, int col) { 113 return data[row][col]; 114 } 115 116 // The default implementations of these methods in 117 // AbstractTableModel would work, but we can refine them. 118 @Override 119 public String getColumnName(int column) { 120 return names[column]; 121 } 122 123 @Override 124 public Class getColumnClass(int col) { 125 return getValueAt(0, col).getClass(); 126 } 127 128 @Override 129 public boolean isCellEditable(int row, int col) { 130 return (col == 4); 131 } 132 133 @Override 134 public void setValueAt(Object aValue, int row, int column) { 135 data[row][column] = aValue; 136 } 137 }; 138 139 // Instead of making the table display the data as it would normally 140 // with: 141 // JTable tableView = new JTable(dataModel); 142 // Add a sorter, by using the following three lines instead of the one 143 // above. 144 TableSorter sorter = new TableSorter(dataModel); 145 JTable tableView = new JTable(sorter); 146 sorter.addMouseListenerToHeaderInTable(tableView); 147 148 JScrollPane scrollpane = new JScrollPane(tableView); 149 150 scrollpane.setPreferredSize(new Dimension(700, 300)); 151 frame.getContentPane().add(scrollpane); 152 frame.pack(); 153 frame.setVisible(true); 154 } 155 156 public static void main(String[] args) { 157 // Trying to set Nimbus look and feel 158 try { 159 for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 160 if ("Nimbus".equals(info.getName())) { 161 UIManager.setLookAndFeel(info.getClassName()); 162 break; 163 } 164 } 165 } catch (Exception ex) { 166 Logger.getLogger(TableExample3.class.getName()).log(Level.SEVERE, 167 "Failed to apply Nimbus look and feel", ex); 168 } 169 new TableExample3(); 170 } 171 }