Path: blob/master/src/demo/share/jfc/TableExample/TableExample3.java
41149 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import javax.swing.*;42import javax.swing.table.*;4344import java.awt.event.WindowAdapter;45import java.awt.event.WindowEvent;46import java.awt.Dimension;47import java.util.logging.Level;48import java.util.logging.Logger;49import javax.swing.UIManager.LookAndFeelInfo;505152/**53* An example showing the JTable with a dataModel that is not derived54* from a database. We add the optional TableSorter object to give the55* JTable the ability to sort.56*57* @author Philip Milne58*/59public class TableExample3 {6061public TableExample3() {62JFrame frame = new JFrame("Table");63frame.addWindowListener(new WindowAdapter() {6465@Override66public void windowClosing(WindowEvent e) {67System.exit(0);68}69});7071// Take the dummy data from SwingSet.72final String[] names = { "First Name", "Last Name", "Favorite Color",73"Favorite Number", "Vegetarian" };74final Object[][] data = {75{ "Mark", "Andrews", "Red", Integer.valueOf(2), Boolean.TRUE },76{ "Tom", "Ball", "Blue", Integer.valueOf(99), Boolean.FALSE },77{ "Alan", "Chung", "Green", Integer.valueOf(838), Boolean.FALSE },78{ "Jeff", "Dinkins", "Turquois", Integer.valueOf(8), Boolean.TRUE },79{ "Amy", "Fowler", "Yellow", Integer.valueOf(3), Boolean.FALSE },80{ "Brian", "Gerhold", "Green", Integer.valueOf(0), Boolean.FALSE },81{ "James", "Gosling", "Pink", Integer.valueOf(21), Boolean.FALSE },82{ "David", "Karlton", "Red", Integer.valueOf(1), Boolean.FALSE },83{ "Dave", "Kloba", "Yellow", Integer.valueOf(14), Boolean.FALSE },84{ "Peter", "Korn", "Purple", Integer.valueOf(12), Boolean.FALSE },85{ "Phil", "Milne", "Purple", Integer.valueOf(3), Boolean.FALSE },86{ "Dave", "Moore", "Green", Integer.valueOf(88), Boolean.FALSE },87{ "Hans", "Muller", "Maroon", Integer.valueOf(5), Boolean.FALSE },88{ "Rick", "Levenson", "Blue", Integer.valueOf(2), Boolean.FALSE },89{ "Tim", "Prinzing", "Blue", Integer.valueOf(22), Boolean.FALSE },90{ "Chester", "Rose", "Black", Integer.valueOf(0), Boolean.FALSE },91{ "Ray", "Ryan", "Gray", Integer.valueOf(77), Boolean.FALSE },92{ "Georges", "Saab", "Red", Integer.valueOf(4), Boolean.FALSE },93{ "Willie", "Walker", "Phthalo Blue", Integer.valueOf(4), Boolean.FALSE },94{ "Kathy", "Walrath", "Blue", Integer.valueOf(8), Boolean.FALSE },95{ "Arnaud", "Weber", "Green", Integer.valueOf(44), Boolean.FALSE }96};9798// Create a model of the data.99@SuppressWarnings("serial")100TableModel dataModel = new AbstractTableModel() {101// These methods always need to be implemented.102103public int getColumnCount() {104return names.length;105}106107public int getRowCount() {108return data.length;109}110111public Object getValueAt(int row, int col) {112return data[row][col];113}114115// The default implementations of these methods in116// AbstractTableModel would work, but we can refine them.117@Override118public String getColumnName(int column) {119return names[column];120}121122@Override123public Class<?> getColumnClass(int col) {124return getValueAt(0, col).getClass();125}126127@Override128public boolean isCellEditable(int row, int col) {129return (col == 4);130}131132@Override133public void setValueAt(Object aValue, int row, int column) {134data[row][column] = aValue;135}136};137138// Instead of making the table display the data as it would normally139// with:140// JTable tableView = new JTable(dataModel);141// Add a sorter, by using the following three lines instead of the one142// above.143TableSorter sorter = new TableSorter(dataModel);144JTable tableView = new JTable(sorter);145sorter.addMouseListenerToHeaderInTable(tableView);146147JScrollPane scrollpane = new JScrollPane(tableView);148149scrollpane.setPreferredSize(new Dimension(700, 300));150frame.getContentPane().add(scrollpane);151frame.pack();152frame.setVisible(true);153}154155public static void main(String[] args) {156// Trying to set Nimbus look and feel157try {158for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {159if ("Nimbus".equals(info.getName())) {160UIManager.setLookAndFeel(info.getClassName());161break;162}163}164} catch (Exception ex) {165Logger.getLogger(TableExample3.class.getName()).log(Level.SEVERE,166"Failed to apply Nimbus look and feel", ex);167}168new TableExample3();169}170}171172173