jdk/src/share/demo/jfc/TableExample/TableExample3.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *   - Neither the name of Sun Microsystems nor the names of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * An example showing the JTable with a dataModel that is not derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * from a database. We add the optional TableSorter object to give the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * JTable the ability to sort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.swing.table.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.event.WindowAdapter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.event.WindowEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class TableExample3 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public TableExample3() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        JFrame frame = new JFrame("Table");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        frame.addWindowListener(new WindowAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            public void windowClosing(WindowEvent e) {System.exit(0);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        // Take the dummy data from SwingSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        final String[] names = {"First Name", "Last Name", "Favorite Color",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                "Favorite Number", "Vegetarian"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        final Object[][] data = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            {"Mark", "Andrews", "Red", new Integer(2), Boolean.TRUE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            {"Tom", "Ball", "Blue", new Integer(99), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            {"Alan", "Chung", "Green", new Integer(838), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            {"Jeff", "Dinkins", "Turquois", new Integer(8), Boolean.TRUE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            {"Amy", "Fowler", "Yellow", new Integer(3), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            {"Brian", "Gerhold", "Green", new Integer(0), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            {"James", "Gosling", "Pink", new Integer(21), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            {"David", "Karlton", "Red", new Integer(1), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            {"Dave", "Kloba", "Yellow", new Integer(14), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            {"Peter", "Korn", "Purple", new Integer(12), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            {"Phil", "Milne", "Purple", new Integer(3), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            {"Dave", "Moore", "Green", new Integer(88), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            {"Hans", "Muller", "Maroon", new Integer(5), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            {"Rick", "Levenson", "Blue", new Integer(2), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            {"Tim", "Prinzing", "Blue", new Integer(22), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            {"Chester", "Rose", "Black", new Integer(0), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            {"Ray", "Ryan", "Gray", new Integer(77), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            {"Georges", "Saab", "Red", new Integer(4), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            {"Willie", "Walker", "Phthalo Blue", new Integer(4), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            {"Kathy", "Walrath", "Blue", new Integer(8), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            {"Arnaud", "Weber", "Green", new Integer(44), Boolean.FALSE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // Create a model of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        TableModel dataModel = new AbstractTableModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            // These methods always need to be implemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            public int getColumnCount() { return names.length; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            public int getRowCount() { return data.length;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            public Object getValueAt(int row, int col) {return data[row][col];}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            // The default implementations of these methods in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            // AbstractTableModel would work, but we can refine them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            public String getColumnName(int column) {return names[column];}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            public boolean isCellEditable(int row, int col) {return (col==4);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            public void setValueAt(Object aValue, int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                data[row][column] = aValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // Instead of making the table display the data as it would normally with:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // JTable tableView = new JTable(dataModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // Add a sorter, by using the following three lines instead of the one above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        TableSorter  sorter = new TableSorter(dataModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        JTable    tableView = new JTable(sorter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        sorter.addMouseListenerToHeaderInTable(tableView);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        JScrollPane scrollpane = new JScrollPane(tableView);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        scrollpane.setPreferredSize(new Dimension(700, 300));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        frame.getContentPane().add(scrollpane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        frame.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        frame.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        new TableExample3();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
}