可以以一列或多列为行标题栏。
预览:
代码:
package com.han;import java.awt.BorderLayout;import java.util.Vector;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JViewport;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.table.AbstractTableModel;public class MFixedColumnTable extends JPanel { /** * */ private static final long serialVersionUID = -8001758880985479654L; private Vector columnNameV; // declare the table column name vector private Vector
然后,用一个测试提供行标题栏的表格的代码:
package com.han;import java.awt.BorderLayout;import java.util.Vector;import javax.swing.JFrame;/** * Provide a fixed column in a table * * @author Gaowen * */public class JTable4 extends JFrame { /** * */ private static final long serialVersionUID = 805308369080023303L; public JTable4() { super(); setTitle("提供行标题栏的表格"); setBounds(100, 100, 500, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Vector columnNameV = new Vector (); columnNameV.add("日期"); for (int i = 1; i < 21; i++) { columnNameV.add("商品" + i); } Vector > tableValueV = new Vector >(); for (int row = 1; row < 31; row++) { Vector rowV = new Vector(); rowV.add(row); for (int col = 0; col < 20; col++) { rowV.add((int) (Math.random() * 1000)); } tableValueV.add(rowV); } final MFixedColumnTable panel = new MFixedColumnTable(columnNameV, tableValueV, 1); getContentPane().add(panel, BorderLayout.CENTER); } public static void main(String[] args) { // TODO Auto-generated method stub JTable4 frame = new JTable4(); frame.setVisible(true); }}
这是继前几个表格使用和操作的后续,讲解了提供行标题栏的表格的实现方法,以帮助读者拓宽表格的设计思路,同时这也是一种很适用的表格形式。