jdk/src/share/demo/jfc/TableExample/TableExample4.java
author tbell
Fri, 27 Feb 2009 10:54:11 -0800
changeset 2091 7faffd237305
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
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
 * Another JTable example, showing how column attributes can be refined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * even when columns have been created automatically. Here we create some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * specialised renderers and editors as well as changing widths and colors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * for some of the columns in the SwingSet demo table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.swing.table.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.awt.event.WindowAdapter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.awt.event.WindowEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class TableExample4 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public TableExample4() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        JFrame frame = new JFrame("Table");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        frame.addWindowListener(new WindowAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            public void windowClosing(WindowEvent e) {System.exit(0);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        // Take the dummy data from SwingSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        final String[] names = {"First Name", "Last Name", "Favorite Color",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                "Favorite Number", "Vegetarian"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        final Object[][] data = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            {"Mark", "Andrews", "Red", new Integer(2), Boolean.TRUE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            {"Tom", "Ball", "Blue", new Integer(99), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            {"Alan", "Chung", "Green", new Integer(838), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            {"Jeff", "Dinkins", "Turquois", new Integer(8), Boolean.TRUE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            {"Amy", "Fowler", "Yellow", new Integer(3), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            {"Brian", "Gerhold", "Green", new Integer(0), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            {"James", "Gosling", "Pink", new Integer(21), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            {"David", "Karlton", "Red", new Integer(1), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            {"Dave", "Kloba", "Yellow", new Integer(14), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            {"Peter", "Korn", "Purple", new Integer(12), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            {"Phil", "Milne", "Purple", new Integer(3), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            {"Dave", "Moore", "Green", new Integer(88), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            {"Hans", "Muller", "Maroon", new Integer(5), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            {"Rick", "Levenson", "Blue", new Integer(2), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            {"Tim", "Prinzing", "Blue", new Integer(22), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            {"Chester", "Rose", "Black", new Integer(0), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            {"Ray", "Ryan", "Gray", new Integer(77), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            {"Georges", "Saab", "Red", new Integer(4), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            {"Willie", "Walker", "Phthalo Blue", new Integer(4), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            {"Kathy", "Walrath", "Blue", new Integer(8), Boolean.FALSE},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            {"Arnaud", "Weber", "Green", new Integer(44), Boolean.FALSE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // Create a model of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        TableModel dataModel = new AbstractTableModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // These methods always need to be implemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            public int getColumnCount() { return names.length; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            public int getRowCount() { return data.length;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            public Object getValueAt(int row, int col) {return data[row][col];}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            // The default implementations of these methods in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // AbstractTableModel would work, but we can refine them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            public String getColumnName(int column) {return names[column];}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            public boolean isCellEditable(int row, int col) {return true;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            public void setValueAt(Object aValue, int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                System.out.println("Setting value to: " + aValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                data[row][column] = aValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // Create the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        JTable tableView = new JTable(dataModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // Turn off auto-resizing so that we can set column sizes programmatically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // In this mode, all columns will get their preferred widths, as set blow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // Create a combo box to show that you can use one in a table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        JComboBox comboBox = new JComboBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        comboBox.addItem("Red");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        comboBox.addItem("Orange");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        comboBox.addItem("Yellow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        comboBox.addItem("Green");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        comboBox.addItem("Blue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        comboBox.addItem("Indigo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        comboBox.addItem("Violet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        TableColumn colorColumn = tableView.getColumn("Favorite Color");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // Use the combo box as the editor in the "Favorite Color" column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // Set a pink background and tooltip for the Color column renderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        DefaultTableCellRenderer colorColumnRenderer = new DefaultTableCellRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        colorColumnRenderer.setBackground(Color.pink);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        colorColumnRenderer.setToolTipText("Click for combo box");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        colorColumn.setCellRenderer(colorColumnRenderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // Set a tooltip for the header of the colors column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        TableCellRenderer headerRenderer = colorColumn.getHeaderRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (headerRenderer instanceof DefaultTableCellRenderer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            ((DefaultTableCellRenderer)headerRenderer).setToolTipText("Hi Mom!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // Set the width of the "Vegetarian" column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        TableColumn vegetarianColumn = tableView.getColumn("Vegetarian");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        vegetarianColumn.setPreferredWidth(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // Show the values in the "Favorite Number" column in different colors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        TableColumn numbersColumn = tableView.getColumn("Favorite Number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        DefaultTableCellRenderer numberColumnRenderer = new DefaultTableCellRenderer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            public void setValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                int cellValue = (value instanceof Number) ? ((Number)value).intValue() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                setForeground((cellValue > 30) ? Color.black : Color.red);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                setText((value == null) ? "" : value.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        numberColumnRenderer.setHorizontalAlignment(JLabel.RIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        numbersColumn.setCellRenderer(numberColumnRenderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        numbersColumn.setPreferredWidth(110);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // Finish setting up the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        JScrollPane scrollpane = new JScrollPane(tableView);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        scrollpane.setPreferredSize(new Dimension(430, 200));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        frame.getContentPane().add(scrollpane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        frame.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        frame.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        new TableExample4();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}