home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / SwingSet / TablePanel.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  14.3 KB  |  373 lines

  1. /*
  2.  * @(#)TablePanel.java    1.33 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import javax.swing.*;
  16. import javax.swing.table.*;
  17. import javax.swing.event.*;
  18. import javax.swing.border.*;
  19.  
  20. import java.awt.*;
  21. import java.awt.event.*;
  22. import java.util.*;
  23.  
  24. /*
  25.  * @version 1.25 01/31/98
  26.  * @author Philip Milne
  27.  * @author Steve Wilson
  28.  */
  29. public class TablePanel extends JPanel {
  30.     JTable      tableView;
  31.     JScrollPane scrollpane;
  32.     Dimension   origin = new Dimension(0, 0);
  33.  
  34.     JCheckBox   isColumnReorderingAllowedCheckBox;
  35.     JCheckBox   showHorizontalLinesCheckBox;
  36.     JCheckBox   showVerticalLinesCheckBox;
  37.  
  38.     JCheckBox   isColumnSelectionAllowedCheckBox;
  39.     JCheckBox   isRowSelectionAllowedCheckBox;
  40.     JCheckBox   isRowAndColumnSelectionAllowedCheckBox;
  41.  
  42.     JLabel      interCellSpacingLabel;
  43.     JLabel      rowHeightLabel;
  44.  
  45.     JSlider     interCellSpacingSlider;
  46.     JSlider     rowHeightSlider;
  47.  
  48.     JComponent  selectionModeButtons;
  49.     JComponent  resizeModeButtons;
  50.  
  51.     JPanel      mainPanel;
  52.     JPanel      controlPanel;
  53.     JScrollPane tableAggregate;
  54.  
  55.     public TablePanel(SwingSet swing) {
  56.     super();
  57.  
  58.     setLayout(new BorderLayout());
  59.         mainPanel = this;
  60.     controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
  61.     JPanel column1 = new JPanel (new ColumnLayout() );
  62.     JPanel column2 = new JPanel (new ColumnLayout() );
  63.     JPanel column3 = new JPanel (new ColumnLayout() );
  64.  
  65.     mainPanel.add(controlPanel, BorderLayout.NORTH);
  66.  
  67.  
  68.     // start column 1
  69.         isColumnReorderingAllowedCheckBox = new JCheckBox("Reordering allowed", true);
  70.         column1.add(isColumnReorderingAllowedCheckBox);
  71.         isColumnReorderingAllowedCheckBox.addActionListener(new ActionListener() {
  72.         public void actionPerformed(ActionEvent e) {
  73.             boolean flag = ((JCheckBox)e.getSource()).isSelected();
  74.                 tableView.getTableHeader().setReorderingAllowed(flag);
  75.                 tableView.repaint();
  76.         }
  77.         });
  78.  
  79.  
  80.         showHorizontalLinesCheckBox = new JCheckBox("Horiz. Lines", true);
  81.         column1.add(showHorizontalLinesCheckBox);
  82.         showHorizontalLinesCheckBox.addActionListener(new ActionListener() {
  83.         public void actionPerformed(ActionEvent e) {
  84.             boolean flag = ((JCheckBox)e.getSource()).isSelected();
  85.                 tableView.setShowHorizontalLines(flag); ;
  86.                 tableView.repaint();
  87.         }
  88.         });
  89.  
  90.         showVerticalLinesCheckBox = new JCheckBox("Vert. Lines", true);
  91.         column1.add(showVerticalLinesCheckBox);
  92.         showVerticalLinesCheckBox.addActionListener(new ActionListener() {
  93.         public void actionPerformed(ActionEvent e) {
  94.             boolean flag = ((JCheckBox)e.getSource()).isSelected();
  95.                 tableView.setShowVerticalLines(flag); ;
  96.                 tableView.repaint();
  97.         }
  98.         });
  99.  
  100.         interCellSpacingLabel = new JLabel("Inter-cell spacing:");
  101.     column1.add(interCellSpacingLabel);
  102.  
  103.         interCellSpacingSlider = new JSlider(JSlider.HORIZONTAL, 0, 10, 1);
  104.     interCellSpacingSlider.getAccessibleContext().setAccessibleName("Inter-cell spacing");
  105.     interCellSpacingLabel.setLabelFor(interCellSpacingSlider);
  106.         column1.add(interCellSpacingSlider);
  107.         interCellSpacingSlider.addChangeListener(new ChangeListener() {
  108.         public void stateChanged(ChangeEvent e) {
  109.             int spacing = ((JSlider)e.getSource()).getValue();
  110.                 tableView.setIntercellSpacing(new Dimension(spacing, spacing));
  111.                 tableView.repaint();
  112.         }
  113.         });
  114.  
  115.         controlPanel.add(column1);
  116.  
  117.     // start column 2
  118.  
  119.      isColumnSelectionAllowedCheckBox = new JCheckBox("Column selection", false);
  120.         column2.add(isColumnSelectionAllowedCheckBox);
  121.         isColumnSelectionAllowedCheckBox.addActionListener(new ActionListener() {
  122.         public void actionPerformed(ActionEvent e) {
  123.             boolean flag = ((JCheckBox)e.getSource()).isSelected();
  124.                 tableView.setColumnSelectionAllowed(flag); ;
  125.                 tableView.repaint();
  126.         }
  127.         });
  128.  
  129.         isRowSelectionAllowedCheckBox = new JCheckBox("Row selection", true);
  130.         column2.add(isRowSelectionAllowedCheckBox);
  131.         isRowSelectionAllowedCheckBox.addActionListener(new ActionListener() {
  132.         public void actionPerformed(ActionEvent e) {
  133.             boolean flag = ((JCheckBox)e.getSource()).isSelected();
  134.                 tableView.setRowSelectionAllowed(flag); ;
  135.                 tableView.repaint();
  136.         }
  137.         });
  138.  
  139.         isRowAndColumnSelectionAllowedCheckBox = new JCheckBox("Cell selection", false);
  140.         column2.add(isRowAndColumnSelectionAllowedCheckBox);
  141.         isRowAndColumnSelectionAllowedCheckBox.addActionListener(new ActionListener() {
  142.         public void actionPerformed(ActionEvent e) {
  143.             boolean flag = ((JCheckBox)e.getSource()).isSelected();
  144.                 tableView.setCellSelectionEnabled(flag); ;
  145.                 tableView.repaint();
  146.         }
  147.         });
  148.  
  149.         rowHeightLabel = new JLabel("Row height:");
  150.     column2.add(rowHeightLabel);
  151.  
  152.         rowHeightSlider = new JSlider(JSlider.HORIZONTAL, 5, 100, 20);
  153.     rowHeightSlider.getAccessibleContext().setAccessibleName("Row height");
  154.     rowHeightLabel.setLabelFor(rowHeightSlider);
  155.         column2.add(rowHeightSlider);
  156.         rowHeightSlider.addChangeListener(new ChangeListener() {
  157.         public void stateChanged(ChangeEvent e) {
  158.             int height = ((JSlider)e.getSource()).getValue();
  159.                 tableView.setRowHeight(height);
  160.                 tableView.repaint();
  161.         }
  162.         });
  163.  
  164.         controlPanel.add(column2);
  165.  
  166.         // Create the table.
  167.         tableAggregate = createTable();
  168.         mainPanel.add(tableAggregate, BorderLayout.CENTER);
  169.  
  170.  
  171.  
  172.         // ComboBox for selection modes.
  173.     JPanel selectMode = new JPanel();
  174.         column3.setLayout(new ColumnLayout());
  175.           selectMode.setBorder(new TitledBorder("Selection mode"));
  176.  
  177.  
  178.         JComboBox selectionModeComboBox = new JComboBox();
  179.         selectionModeComboBox.addItem("Single");
  180.         selectionModeComboBox.addItem("One range");
  181.         selectionModeComboBox.addItem("Multiple ranges");
  182.         selectionModeComboBox.setSelectedIndex(tableView.getSelectionModel().getSelectionMode());
  183.         selectionModeComboBox.addItemListener(new ItemListener() {
  184.         public void itemStateChanged(ItemEvent e) {
  185.             JComboBox source = (JComboBox)e.getSource();
  186.                 tableView.setSelectionMode(source.getSelectedIndex());
  187.         }
  188.         });
  189.  
  190.     selectMode.add(selectionModeComboBox);
  191.         column3.add(selectMode);
  192.  
  193.         // Combo box for table resize mode.
  194.  
  195.     JPanel resizeMode = new JPanel();
  196.  
  197.     resizeMode.setBorder(new TitledBorder("Autoresize mode"));
  198.  
  199.  
  200.         JComboBox resizeModeComboBox = new JComboBox();
  201.         resizeModeComboBox.addItem("Off");
  202.         resizeModeComboBox.addItem("Column boundries");
  203.         resizeModeComboBox.addItem("Subsequent columns");
  204.         resizeModeComboBox.addItem("Last column");
  205.         resizeModeComboBox.addItem("All columns");
  206.         resizeModeComboBox.setSelectedIndex(tableView.getAutoResizeMode());
  207.         resizeModeComboBox.addItemListener(new ItemListener() {
  208.         public void itemStateChanged(ItemEvent e) {
  209.             JComboBox source = (JComboBox)e.getSource();
  210.                 tableView.setAutoResizeMode(source.getSelectedIndex());
  211.         }
  212.         });
  213.  
  214.     resizeMode.add(resizeModeComboBox);
  215.         column3.add(resizeMode);
  216.  
  217.         controlPanel.add(column3);
  218.     }
  219.  
  220.  
  221.     private ImageIcon loadIcon(String name, String description) {
  222.     String path = "images/ImageClub/food/" + name;
  223.     return SwingSet.sharedInstance().loadImageIcon(path, description);
  224.     }
  225.  
  226.     public JScrollPane createTable() {
  227.  
  228.         // final
  229.         final String[] names = {"First Name", "Last Name", "Favorite Color",
  230.                                 "Favorite Sport", "Favorite Number", "Favorite Food"};
  231.  
  232.     ImageIcon burger = loadIcon("burger.gif","burger");
  233.     ImageIcon fries = loadIcon("fries.gif","fries");
  234.     ImageIcon softdrink = loadIcon("softdrink.gif","soft drink");
  235.     ImageIcon hotdog = loadIcon("hotdog.gif","hot dog");
  236.     ImageIcon pizza = loadIcon("pizza.gif","pizza");
  237.     ImageIcon icecream = loadIcon("icecream.gif","ice cream");
  238.     ImageIcon pie = loadIcon("pie.gif","pie");
  239.     ImageIcon cake = loadIcon("cake.gif","cake");
  240.     ImageIcon donut = loadIcon("donut.gif","donut");
  241.     ImageIcon treat = loadIcon("treat.gif","treat");
  242.     ImageIcon grapes = loadIcon("grapes.gif","grapes");
  243.     ImageIcon banana = loadIcon("banana.gif","banana");
  244.     ImageIcon watermelon = loadIcon("watermelon.gif","watermelon");
  245.     ImageIcon cantaloupe = loadIcon("cantaloupe.gif","cantaloupe");
  246.     ImageIcon peach = loadIcon("peach.gif","peach");
  247.     ImageIcon broccoli = loadIcon("broccoli.gif","broccoli");
  248.     ImageIcon carrot = loadIcon("carrot.gif","carrot");
  249.     ImageIcon peas = loadIcon("peas.gif","peas");
  250.     ImageIcon corn = loadIcon("corn.gif","corn");
  251.     ImageIcon radish = loadIcon("radish.gif","radish");
  252.  
  253.  
  254.         // Create the dummy data (a few rows of names)
  255.         final Object[][] data = {
  256.       {"Mike", "Albers",        Color.green, "Soccer", new Integer(44), banana},
  257.       {"Mark", "Andrews",       Color.red, "Baseball", new Integer(2), broccoli},
  258.       {"Tom", "Ball",           Color.blue, "Football", new Integer(99), burger},
  259.       {"Alan", "Chung",         Color.green, "Baseball", new Integer(838), cake},
  260.       {"Jeff", "Dinkins",       Color.magenta, "Football", new Integer(8), cantaloupe},
  261.       {"Amy", "Fowler",         Color.yellow, "Hockey", new Integer(3), carrot},
  262.       {"Brian", "Gerhold",      Color.green, "Rugby", new Integer(7), corn},
  263.       {"James", "Gosling",      Color.pink, "Tennis", new Integer(21), donut},
  264.       {"Earl", "Johnson",       Color.green, "Bicycling", new Integer(8), carrot},
  265.       {"David", "Karlton",      Color.red, "Baseball", new Integer(1), fries},
  266.       {"Dave", "Kloba",         Color.yellow, "Football", new Integer(14), grapes},
  267.       {"Peter", "Korn",         new Color(100, 100, 255), "Scuba Diving", new Integer(12), broccoli},
  268.       {"Dana", "Miller",        Color.blue, "Ice Skating", new Integer(8), banana},
  269.       {"Phil", "Milne",         Color.magenta, "Rugby", new Integer(3), banana},
  270.       {"Dave", "Moore",         Color.green, "Tennis", new Integer(88), peach},
  271.       {"Hans", "Muller",        Color.magenta, "Baseball", new Integer(5), peas},
  272.       {"Rick", "Levenson",      Color.blue, "Football", new Integer(2), pie},
  273.       {"Tim", "Prinzing",       Color.blue, "Baseball", new Integer(22), pizza},
  274.       {"Chester", "Rose",       Color.black, "Hockey", new Integer(0), radish},
  275.       {"Chris", "Ryan",         Color.black, "None", new Integer(6), softdrink},
  276.       {"Ray", "Ryan",           Color.gray, "Football", new Integer(77), treat},
  277.       {"Georges", "Saab",       Color.red, "Hockey", new Integer(4), watermelon},
  278.       {"Tom", "Santos",         Color.blue, "Football", new Integer(3), banana},
  279.       {"Rich", "Schiavi",       Color.blue, "Hockey", new Integer(4), grapes},
  280.       {"Nancy", "Schorr",       Color.blue, "Hockey", new Integer(8), corn},
  281.       {"Violet", "Scott",       Color.magenta, "Basketball", new Integer(44), grapes},
  282.       {"Joseph", "Scheuhammer", Color.green, "Hockey", new Integer(66), corn},
  283.       {"Jeff", "Shapiro",       Color.black, "Skiing", new Integer(42), peach},
  284.       {"Willie", "Walker",      Color.blue, "Hockey", new Integer(4), banana},
  285.       {"Kathy", "Walrath",      Color.blue, "Baseball", new Integer(8), banana},
  286.       {"Arnaud", "Weber",       Color.green, "Football", new Integer(993), peach},
  287.       {"Steve", "Wilson",       Color.green, "Baseball", new Integer(7), fries}
  288.         };
  289.  
  290.         // Create a model of the data.
  291.         TableModel dataModel = new AbstractTableModel() {
  292.             public int getColumnCount() { return names.length; }
  293.             public int getRowCount() { return data.length;}
  294.             public Object getValueAt(int row, int col) {return data[row][col];}
  295.             public String getColumnName(int column) {return names[column];}
  296.             public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
  297.             public boolean isCellEditable(int row, int col) {return getColumnClass(col) == String.class;}
  298.             public void setValueAt(Object aValue, int row, int column) { data[row][column] = aValue; }
  299.          };
  300.  
  301.  
  302.         // Create the table
  303.         tableView = new JTable(dataModel);
  304.  
  305.         // Show colors by rendering them in their own color.
  306.         DefaultTableCellRenderer colorRenderer = new DefaultTableCellRenderer() {
  307.         public void setValue(Object value) {
  308.             if (value instanceof Color) {
  309.                 Color c = (Color)value;
  310.                 setForeground(c);
  311.                 setText(c.getRed() + ", " + c.getGreen() + ", " + c.getBlue());
  312.             }
  313.         }
  314.  
  315.         };
  316.  
  317.         colorRenderer.setHorizontalAlignment(JLabel.RIGHT);
  318.         tableView.getColumn("Favorite Color").setCellRenderer(colorRenderer);
  319.  
  320.         tableView.setRowHeight(20);
  321.  
  322.         scrollpane = new JScrollPane(tableView);
  323.         return scrollpane;
  324.     }
  325. }
  326.  
  327. class ColumnLayout implements LayoutManager {
  328.  
  329.   int xInset = 5;
  330.   int yInset = 5;
  331.   int yGap = 2;
  332.  
  333.   public void addLayoutComponent(String s, Component c) {}
  334.  
  335.   public void layoutContainer(Container c) {
  336.       Insets insets = c.getInsets();
  337.       int height = yInset + insets.top;
  338.  
  339.       Component[] children = c.getComponents();
  340.       Dimension compSize = null;
  341.       for (int i = 0; i < children.length; i++) {
  342.       compSize = children[i].getPreferredSize();
  343.       children[i].setSize(compSize.width, compSize.height);
  344.       children[i].setLocation( xInset + insets.left, height);
  345.       height += compSize.height + yGap;
  346.       }
  347.  
  348.   }
  349.  
  350.   public Dimension minimumLayoutSize(Container c) {
  351.       Insets insets = c.getInsets();
  352.       int height = yInset + insets.top;
  353.       int width = 0 + insets.left + insets.right;
  354.  
  355.       Component[] children = c.getComponents();
  356.       Dimension compSize = null;
  357.       for (int i = 0; i < children.length; i++) {
  358.       compSize = children[i].getPreferredSize();
  359.       height += compSize.height + yGap;
  360.       width = Math.max(width, compSize.width + insets.left + insets.right + xInset*2);
  361.       }
  362.       height += insets.bottom;
  363.       return new Dimension( width, height);
  364.   }
  365.  
  366.   public Dimension preferredLayoutSize(Container c) {
  367.       return minimumLayoutSize(c);
  368.   }
  369.  
  370.   public void removeLayoutComponent(Component c) {}
  371.  
  372. }
  373.