📄 tabledemo.java
字号:
/* * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * -Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduct the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that Software is not designed, licensed or intended for * use in the design, construction, operation or maintenance of any nuclear * facility. *//* * @(#)TableDemo.java 1.12 03/01/23 */package com.sunking.swing;import javax.swing.*;import javax.swing.event.*;import javax.swing.text.*;import javax.swing.table.*;import javax.swing.border.*;import javax.swing.colorchooser.*;import javax.swing.filechooser.*;import javax.accessibility.*;import java.awt.*;import java.awt.event.*;import java.beans.*;import java.util.*;import java.io.*;import java.applet.*;import java.net.*;/** * Table demo * * @version 1.12 01/23/03 * @author Philip Milne * @author Steve Wilson */public class TableDemo extends DemoModule { JTable tableView; JScrollPane scrollpane; Dimension origin = new Dimension(0, 0); JCheckBox isColumnReorderingAllowedCheckBox; JCheckBox showHorizontalLinesCheckBox; JCheckBox showVerticalLinesCheckBox; JCheckBox isColumnSelectionAllowedCheckBox; JCheckBox isRowSelectionAllowedCheckBox; // JCheckBox isRowAndColumnSelectionAllowedCheckBox; JLabel interCellSpacingLabel; JLabel rowHeightLabel; JSlider interCellSpacingSlider; JSlider rowHeightSlider; JComboBox selectionModeComboBox = null; JComboBox resizeModeComboBox = null; JPanel controlPanel; JScrollPane tableAggregate; String path = "ImageClub/food/"; final int INITIAL_ROWHEIGHT = 33; /** * main method allows us to run as a standalone demo. */ public static void main(String[] args) { TableDemo demo = new TableDemo(null); demo.mainImpl(); } /** * TableDemo Constructor */ public TableDemo(SwingSet2 swingset) { super(swingset, "TableDemo", "toolbar/JTable.gif"); getDemoPanel().setLayout(new BorderLayout()); controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel column1 = new JPanel (new ColumnLayout() ); JPanel column2 = new JPanel (new ColumnLayout() ); JPanel column3 = new JPanel (new ColumnLayout() ); getDemoPanel().add(controlPanel, BorderLayout.NORTH); Vector relatedComponents = new Vector(); // start column 1 isColumnReorderingAllowedCheckBox = new JCheckBox(getString("TableDemo.reordering_allowed"), true); column1.add(isColumnReorderingAllowedCheckBox); isColumnReorderingAllowedCheckBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { boolean flag = ((JCheckBox)e.getSource()).isSelected(); tableView.getTableHeader().setReorderingAllowed(flag); tableView.repaint(); } }); showHorizontalLinesCheckBox = new JCheckBox(getString("TableDemo.horz_lines"), true); column1.add(showHorizontalLinesCheckBox); showHorizontalLinesCheckBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { boolean flag = ((JCheckBox)e.getSource()).isSelected(); tableView.setShowHorizontalLines(flag); ; tableView.repaint(); } }); showVerticalLinesCheckBox = new JCheckBox(getString("TableDemo.vert_lines"), true); column1.add(showVerticalLinesCheckBox); showVerticalLinesCheckBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { boolean flag = ((JCheckBox)e.getSource()).isSelected(); tableView.setShowVerticalLines(flag); ; tableView.repaint(); } }); // Show that showHorizontal/Vertical controls are related relatedComponents.removeAllElements(); relatedComponents.add(showHorizontalLinesCheckBox); relatedComponents.add(showVerticalLinesCheckBox); buildAccessibleGroup(relatedComponents); interCellSpacingLabel = new JLabel(getString("TableDemo.intercell_spacing_colon")); column1.add(interCellSpacingLabel); interCellSpacingSlider = new JSlider(JSlider.HORIZONTAL, 0, 10, 1); interCellSpacingSlider.getAccessibleContext().setAccessibleName(getString("TableDemo.intercell_spacing")); interCellSpacingLabel.setLabelFor(interCellSpacingSlider); column1.add(interCellSpacingSlider); interCellSpacingSlider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { int spacing = ((JSlider)e.getSource()).getValue(); tableView.setIntercellSpacing(new Dimension(spacing, spacing)); tableView.repaint(); } }); controlPanel.add(column1); // start column 2 isColumnSelectionAllowedCheckBox = new JCheckBox(getString("TableDemo.column_selection"), false); column2.add(isColumnSelectionAllowedCheckBox); isColumnSelectionAllowedCheckBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { boolean flag = ((JCheckBox)e.getSource()).isSelected(); tableView.setColumnSelectionAllowed(flag); ; tableView.repaint(); } }); isRowSelectionAllowedCheckBox = new JCheckBox(getString("TableDemo.row_selection"), true); column2.add(isRowSelectionAllowedCheckBox); isRowSelectionAllowedCheckBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { boolean flag = ((JCheckBox)e.getSource()).isSelected(); tableView.setRowSelectionAllowed(flag); ; tableView.repaint(); } }); // Show that row/column selections are related relatedComponents.removeAllElements(); relatedComponents.add(isColumnSelectionAllowedCheckBox); relatedComponents.add(isRowSelectionAllowedCheckBox); buildAccessibleGroup(relatedComponents); /* isRowAndColumnSelectionAllowedCheckBox = new JCheckBox(getString("TableDemo.cell_selection"), false); column2.add(isRowAndColumnSelectionAllowedCheckBox); isRowAndColumnSelectionAllowedCheckBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { boolean flag = ((JCheckBox)e.getSource()).isSelected(); tableView.setCellSelectionEnabled(flag); ; tableView.repaint(); } }); */ rowHeightLabel = new JLabel(getString("TableDemo.row_height_colon")); column2.add(rowHeightLabel); rowHeightSlider = new JSlider(JSlider.HORIZONTAL, 5, 100, INITIAL_ROWHEIGHT); rowHeightSlider.getAccessibleContext().setAccessibleName(getString("TableDemo.row_height")); rowHeightLabel.setLabelFor(rowHeightSlider); column2.add(rowHeightSlider); rowHeightSlider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { int height = ((JSlider)e.getSource()).getValue(); tableView.setRowHeight(height); tableView.repaint(); } }); controlPanel.add(column2); // Show that spacing controls are related relatedComponents.removeAllElements(); relatedComponents.add(interCellSpacingSlider); relatedComponents.add(rowHeightSlider); buildAccessibleGroup(relatedComponents); // Create the table. tableAggregate = createTable(); getDemoPanel().add(tableAggregate, BorderLayout.CENTER); // ComboBox for selection modes. JPanel selectMode = new JPanel(); column3.setLayout(new ColumnLayout()); selectMode.setBorder(new TitledBorder(getString("TableDemo.selection_mode"))); selectionModeComboBox = new JComboBox(); selectionModeComboBox.addItem(getString("TableDemo.single")); selectionModeComboBox.addItem(getString("TableDemo.one_range")); selectionModeComboBox.addItem(getString("TableDemo.multiple_ranges")); selectionModeComboBox.setSelectedIndex(tableView.getSelectionModel().getSelectionMode()); selectionModeComboBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { JComboBox source = (JComboBox)e.getSource(); tableView.setSelectionMode(source.getSelectedIndex()); } }); selectMode.add(selectionModeComboBox); column3.add(selectMode); // Combo box for table resize mode. JPanel resizeMode = new JPanel(); resizeMode.setBorder(new TitledBorder(getString("TableDemo.autoresize_mode"))); resizeModeComboBox = new JComboBox(); resizeModeComboBox.addItem(getString("TableDemo.off")); resizeModeComboBox.addItem(getString("TableDemo.column_boundries")); resizeModeComboBox.addItem(getString("TableDemo.subsequent_columns")); resizeModeComboBox.addItem(getString("TableDemo.last_column")); resizeModeComboBox.addItem(getString("TableDemo.all_columns")); resizeModeComboBox.setSelectedIndex(tableView.getAutoResizeMode()); resizeModeComboBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { JComboBox source = (JComboBox)e.getSource(); tableView.setAutoResizeMode(source.getSelectedIndex()); } }); resizeMode.add(resizeModeComboBox); column3.add(resizeMode); controlPanel.add(column3); setTableControllers(); // Set accessibility information } // TableDemo() /** * Sets the Accessibility MEMBER_OF property to denote that * these components work together as a group. Each object * is set to be a MEMBER_OF an array that contains all of * the objects in the group, including itself. * * @param components The list of objects that are related */ void buildAccessibleGroup(Vector components) { AccessibleContext context = null; int numComponents = components.size(); Object[] group = components.toArray(); Object object = null; for (int i = 0; i < numComponents; ++i) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -