属性表:使用表格来显示和编辑属性 : 表 « 图形用户界面 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 图形用户界面 » 屏幕截图 
属性表:使用表格来显示和编辑属性
属性表:使用表格来显示和编辑属性
 
/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.Arrays;
import java.util.Comparator;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 * This class is a JTable subclass that displays a table of the JavaBeans
 * properties of any specified class.
 */
public class PropertyTable extends JTable {
  /** This main method allows the class to be demonstrated standalone */
  public static void main(String[] args) {
    // Specify the name of the class as a command-line argument
    Class beanClass = null;
    try {
      // Use reflection to get the Class from the classname
      beanClass = Class.forName("javax.swing.JLabel");
    catch (Exception e) { // Report errors
      System.out.println("Can't find specified class: " + e.getMessage());
      System.out.println("Usage: java TableDemo <JavaBean class name>");
      System.exit(0);
    }

    // Create a table to display the properties of the specified class
    JTable table = new PropertyTable(beanClass);

    // Then put the table in a scrolling window, put the scrolling
    // window into a frame, and pop it all up on to the screen
    JScrollPane scrollpane = new JScrollPane(table);
    JFrame frame = new JFrame("Properties of JavaBean: ");
    frame.getContentPane().add(scrollpane);
    frame.setSize(500400);
    frame.setVisible(true);
  }

  /**
   * This constructor method specifies what data the table will display (the
   * table model) and uses the TableColumnModel to customize the way that the
   * table displays it. The hard work is done by the TableModel implementation
   * below.
   */
  public PropertyTable(Class beanClass) {
    // Set the data model for this table
    try {
      setModel(new JavaBeanPropertyTableModel(beanClass));
    catch (IntrospectionException e) {
      System.err.println("WARNING: can't introspect: " + beanClass);
    }

    // Tweak the appearance of the table by manipulating its column model
    TableColumnModel colmodel = getColumnModel();

    // Set column widths
    colmodel.getColumn(0).setPreferredWidth(125);
    colmodel.getColumn(1).setPreferredWidth(200);
    colmodel.getColumn(2).setPreferredWidth(75);
    colmodel.getColumn(3).setPreferredWidth(50);

    // Right justify the text in the first column
    TableColumn namecol = colmodel.getColumn(0);
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setHorizontalAlignment(SwingConstants.RIGHT);
    namecol.setCellRenderer(renderer);
  }

  /**
   * This class implements TableModel and represents JavaBeans property data
   * in a way that the JTable component can display. If you've got some type
   * of tabular data to display, implement a TableModel class to describe that
   * data, and the JTable component will be able to display it.
   */
  static class JavaBeanPropertyTableModel extends AbstractTableModel {
    PropertyDescriptor[] properties; // The properties to display

    /**
     * The constructor: use the JavaBeans introspector mechanism to get
     * information about all the properties of a bean. Once we've got this
     * information, the other methods will interpret it for JTable.
     */
    public JavaBeanPropertyTableModel(Class beanClass)
        throws java.beans.IntrospectionException {
      // Use the introspector class to get "bean info" about the class.
      BeanInfo beaninfo = Introspector.getBeanInfo(beanClass);
      // Get the property descriptors from that BeanInfo class
      properties = beaninfo.getPropertyDescriptors();
      // Now do a case-insensitive sort by property name
      // The anonymous Comparator implementation specifies how to
      // sort PropertyDescriptor objects by name
      Arrays.sort(properties, new Comparator() {
        public int compare(Object p, Object q) {
          PropertyDescriptor a = (PropertyDescriptorp;
          PropertyDescriptor b = (PropertyDescriptorq;
          return a.getName().compareToIgnoreCase(b.getName());
        }

        public boolean equals(Object o) {
          return o == this;
        }
      });
    }

    // These are the names of the columns represented by this TableModel
    static final String[] columnNames = new String[] { "Name""Type",
        "Access""Bound" };

    // These are the types of the columns represented by this TableModel
    static final Class[] columnTypes = new Class[] { String.class,
        Class.class, String.class, Boolean.class };

    // These simple methods return basic information about the table
    public int getColumnCount() {
      return columnNames.length;
    }

    public int getRowCount() {
      return properties.length;
    }

    public String getColumnName(int column) {
      return columnNames[column];
    }

    public Class getColumnClass(int column) {
      return columnTypes[column];
    }

    /**
     * This method returns the value that appears at the specified row and
     * column of the table
     */
    public Object getValueAt(int row, int column) {
      PropertyDescriptor prop = properties[row];
      switch (column) {
      case 0:
        return prop.getName();
      case 1:
        return prop.getPropertyType();
      case 2:
        return getAccessType(prop);
      case 3:
        return new Boolean(prop.isBound());
      default:
        return null;
      }
    }

    // A helper method called from getValueAt() above
    String getAccessType(PropertyDescriptor prop) {
      java.lang.reflect.Method reader = prop.getReadMethod();
      java.lang.reflect.Method writer = prop.getWriteMethod();
      if ((reader != null&& (writer != null))
        return "Read/Write";
      else if (reader != null)
        return "Read-Only";
      else if (writer != null)
        return "Write-Only";
      else
        return "No Access"// should never happen
    }
  }
}

           
         
  
Related examples in the same category
1. 创建一个表格
2. Creates tables that allow rows and columns to be added or deleted
3. 从列表数据和列名建立一个表
4. Getting the Number of Rows and Columns in a JTable Component
5. 添加数据到表格组件
6. 通过DefaultTableModel添加列到表格
7. 通过DefaultTableModel插入一行到表格
8. Insert a row to a table through DefaultTableModel at specified row
9. 滚动表滚动表
10. 简单示范表格简单示范表格
11. 创建一个表从两个二维数组创建一个表从两个二维数组
12. 创建表与Unicode数据创建表与Unicode数据
13. 使用表,以显示输入的数据(整数,布尔,彩色)使用表,以显示输入的数据(整数,布尔,彩色)
14. 调整表的大小调整表的大小
15. 费用表费用表
16. StockTable 6:事件和动态显示StockTable 6:事件和动态显示
17. 表格选择事件表格选择事件
18. 表行和列的选择表行和列的选择
19. 表选择模式表选择模式
20. Use code to change Table Selection Use code to change Table Selection
21. 结果显示在表(表格)
22. 表格与JDBC结果表格与JDBC结果
23. 结果表:继承AbstractTable和JDBC结果集ResultSet
24. 列表界面性能,表格和排序列表界面性能,表格和排序
25. 表与工具提示,单元格和列标题表与工具提示,单元格和列标题
26. StockTable 4 :表排序StockTable 4 :表排序
27. Table Sort Test
28. 表格排序:点击表头进行表排序表格排序:点击表头进行表排序
29. 创建表格的图像
30. JTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
31. JTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
32. JTable.setColumnSelectionAllowed(boolean b);
33. JTable.setRowSelectionAllowed(boolean b);
34. JTable.setCellSelectionEnabled(boolean b);
35. 处理选择和模型的变化事件
36. ListSelectionModel rowSelMod = JTable.getSelectionModel();
37. ListSelectionModel colSelMod = JTable.getColumnModel().getSelectionModel();
38. JTree.getModel().addTreeModelListener(new TreeModelListener())
39. Move the last visible column so it becomes the first visible column
40. the last column is moved to the first position
41. 在一个表格允许用户调整列
42. 检索表中可见单元格的值
43. 从模型中检索表中的数据
44. Change a cell in the 2nd visible column
45. Change a cell in the 3rd column in the model
46. 在非可卷动的表格显示表头
47. 在一个表格改变列名称
48. 在表头显示图标
49. Implementing Variable-Height Column Headers in a JTable Component
50. Removing the Column Headers from a Scrollable in a JTable Component
51. Creating a Custom Column Header Renderer in a JTable Component
52. Setting Column Header Tool Tips in a JTable Components
53. 对表格单元格组件设置工具提示
54. 在表格使行默认选择
55. 在表格设置有效列
56. 选择表格单元格
57. 得到默认的选择模式: MULTIPLE_INTERVAL_SELECTION
58. 只允许单一选择
59. Allow selection to span one contiguous set of rows, visible columns, or block of cells
60. Allow multiple selections of rows, visible columns, or cell blocks (default)
61. 选择一个列
62. 选择一个系列
63. 取消一系列栏的选择
64. 选中某行-列0
65. 选择一个额外的一系列行
66. 取消一系列行的选择
67. 选择一个单元格:单元格( 2,1 )
68. 将选定内容扩展到包括所有单元格( 5,3 )
69. Deselect a cell: cell (3,2), All cells in the row and column containing (3,2) are deselected.
70. Toggles the selection state, if it were called again, it exactly reverses the first call.
71. 选择所有单元格
72. 取消所有单元格
73. Column selection is enabled, get the indices of the selected columns
74. Row selection is enabled, Get the indices of the selected rows
75. 活动被选定行的索引
76. 获得最大和最小范围内选定的单元格
77. Check each cell in the min and max ranges of selected cells
78. 在一个表格禁用选则
79. 获得选中的表单元格
80. 建立一个可滚动表格
81. Disable auto resizing to make the table horizontal scrollable
82. 确定是否一个单元格可见
83. 使表格中的单元格可见
84. 滚动单元格
85. 显示横向和纵向网格线(默认)
86. 不显示任何网格线
87. 只显示垂直网格线
88. 只显示水平网格线
89. 设置网格颜色
90. 获取表格组件单元格之间间隙的大小
91. 在单元格的左右添加空格
92. 增加行高
93. Programmatically Starting Cell Editing in a JTable Component
94. Save the current value in the cell being edited and stops the editing process
95. Discard any changes made by the user and stops the editing process
96. 禁用用户编辑表格
97. 禁用用户编辑表格与DefaultTableModel
98. 基于列在一个表格排序列
99. 表格排序列
100. 表格组件选择事件监听
101. Listening for Changes to the Rows and Columns of a JTable Component
102. 在一个表格监听列相关的变化事件
103. Listening for Clicks on a Column Header in a JTable Component
104. 排序和筛选表
105. 使用regexFilter过滤表
106. 设置表格行高度
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.