home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / TableExample / TableExample.jar / src / TableExample4.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  7.8 KB  |  172 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. 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.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)TableExample4.java    1.14 02/06/13
  38.  */
  39.  
  40. /**
  41.  * Another JTable example, showing how column attributes can be refined
  42.  * even when columns have been created automatically. Here we create some
  43.  * specialised renderers and editors as well as changing widths and colors
  44.  * for some of the columns in the SwingSet demo table.
  45.  *
  46.  * @version 1.14 06/13/02
  47.  * @author Philip Milne
  48.  */
  49.  
  50. import javax.swing.*;
  51. import javax.swing.table.*;
  52. import javax.swing.border.*;
  53.  
  54. import java.awt.Dimension;
  55. import java.awt.event.WindowAdapter;
  56. import java.awt.event.WindowEvent;
  57. import java.awt.Color;
  58.  
  59. public class TableExample4 {
  60.  
  61.     public TableExample4() {
  62.         JFrame frame = new JFrame("Table");
  63.         frame.addWindowListener(new WindowAdapter() {
  64.             public void windowClosing(WindowEvent e) {System.exit(0);}});
  65.  
  66.         // Take the dummy data from SwingSet.
  67.         final String[] names = {"First Name", "Last Name", "Favorite Color",
  68.                                 "Favorite Number", "Vegetarian"};
  69.         final Object[][] data = {
  70.         {"Mark", "Andrews", "Red", new Integer(2), new Boolean(true)},
  71.         {"Tom", "Ball", "Blue", new Integer(99), new Boolean(false)},
  72.         {"Alan", "Chung", "Green", new Integer(838), new Boolean(false)},
  73.         {"Jeff", "Dinkins", "Turquois", new Integer(8), new Boolean(true)},
  74.         {"Amy", "Fowler", "Yellow", new Integer(3), new Boolean(false)},
  75.         {"Brian", "Gerhold", "Green", new Integer(0), new Boolean(false)},
  76.         {"James", "Gosling", "Pink", new Integer(21), new Boolean(false)},
  77.         {"David", "Karlton", "Red", new Integer(1), new Boolean(false)},
  78.         {"Dave", "Kloba", "Yellow", new Integer(14), new Boolean(false)},
  79.         {"Peter", "Korn", "Purple", new Integer(12), new Boolean(false)},
  80.         {"Phil", "Milne", "Purple", new Integer(3), new Boolean(false)},
  81.         {"Dave", "Moore", "Green", new Integer(88), new Boolean(false)},
  82.         {"Hans", "Muller", "Maroon", new Integer(5), new Boolean(false)},
  83.         {"Rick", "Levenson", "Blue", new Integer(2), new Boolean(false)},
  84.         {"Tim", "Prinzing", "Blue", new Integer(22), new Boolean(false)},
  85.         {"Chester", "Rose", "Black", new Integer(0), new Boolean(false)},
  86.         {"Ray", "Ryan", "Gray", new Integer(77), new Boolean(false)},
  87.         {"Georges", "Saab", "Red", new Integer(4), new Boolean(false)},
  88.         {"Willie", "Walker", "Phthalo Blue", new Integer(4), new Boolean(false)},
  89.         {"Kathy", "Walrath", "Blue", new Integer(8), new Boolean(false)},
  90.         {"Arnaud", "Weber", "Green", new Integer(44), new Boolean(false)}
  91.         };
  92.  
  93.         // Create a model of the data.
  94.         TableModel dataModel = new AbstractTableModel() {
  95.             // These methods always need to be implemented.
  96.             public int getColumnCount() { return names.length; }
  97.             public int getRowCount() { return data.length;}
  98.             public Object getValueAt(int row, int col) {return data[row][col];}
  99.  
  100.             // The default implementations of these methods in
  101.             // AbstractTableModel would work, but we can refine them.
  102.             public String getColumnName(int column) {return names[column];}
  103.             public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
  104.             public boolean isCellEditable(int row, int col) {return true;}
  105.             public void setValueAt(Object aValue, int row, int column) {
  106.                 System.out.println("Setting value to: " + aValue);
  107.                 data[row][column] = aValue;
  108.             }
  109.          };
  110.  
  111.         // Create the table
  112.         JTable tableView = new JTable(dataModel);
  113.         // Turn off auto-resizing so that we can set column sizes programmatically. 
  114.     // In this mode, all columns will get their preferred widths, as set blow. 
  115.         tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  116.  
  117.     // Create a combo box to show that you can use one in a table.
  118.     JComboBox comboBox = new JComboBox();
  119.     comboBox.addItem("Red");
  120.     comboBox.addItem("Orange");
  121.     comboBox.addItem("Yellow");
  122.     comboBox.addItem("Green");
  123.     comboBox.addItem("Blue");
  124.     comboBox.addItem("Indigo");
  125.     comboBox.addItem("Violet");
  126.  
  127.         TableColumn colorColumn = tableView.getColumn("Favorite Color");
  128.         // Use the combo box as the editor in the "Favorite Color" column.
  129.         colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
  130.  
  131.         // Set a pink background and tooltip for the Color column renderer.
  132.         DefaultTableCellRenderer colorColumnRenderer = new DefaultTableCellRenderer();
  133.         colorColumnRenderer.setBackground(Color.pink);
  134.         colorColumnRenderer.setToolTipText("Click for combo box");
  135.         colorColumn.setCellRenderer(colorColumnRenderer);
  136.  
  137.         // Set a tooltip for the header of the colors column.
  138.     TableCellRenderer headerRenderer = colorColumn.getHeaderRenderer();
  139.     if (headerRenderer instanceof DefaultTableCellRenderer)
  140.         ((DefaultTableCellRenderer)headerRenderer).setToolTipText("Hi Mom!");
  141.  
  142.     // Set the width of the "Vegetarian" column.
  143.         TableColumn vegetarianColumn = tableView.getColumn("Vegetarian");
  144.         vegetarianColumn.setPreferredWidth(100);
  145.  
  146.     // Show the values in the "Favorite Number" column in different colors.
  147.         TableColumn numbersColumn = tableView.getColumn("Favorite Number");
  148.         DefaultTableCellRenderer numberColumnRenderer = new DefaultTableCellRenderer() {
  149.         public void setValue(Object value) {
  150.             int cellValue = (value instanceof Number) ? ((Number)value).intValue() : 0;
  151.             setForeground((cellValue > 30) ? Color.black : Color.red);
  152.             setText((value == null) ? "" : value.toString());
  153.         }
  154.         };
  155.         numberColumnRenderer.setHorizontalAlignment(JLabel.RIGHT);
  156.         numbersColumn.setCellRenderer(numberColumnRenderer);
  157.         numbersColumn.setPreferredWidth(110);
  158.  
  159.         // Finish setting up the table.
  160.         JScrollPane scrollpane = new JScrollPane(tableView);
  161.     scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
  162.         scrollpane.setPreferredSize(new Dimension(430, 200));
  163.         frame.getContentPane().add(scrollpane);
  164.         frame.pack();
  165.         frame.setVisible(true);
  166.     }
  167.  
  168.     public static void main(String[] args) {
  169.         new TableExample4();
  170.     }
  171. }
  172.