Java MFixedColumnTable (提供行标题栏的表格)
可以以一列或多列为行标题栏。
预览:
代码:
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<String> columnNameV; // declare the table column name vector private Vector<Vector<Object>> tableValueV; // declare the table value // vector private int fixedColumn = 1; // the fixed column number private JTable fixedColumnTable; private FixedColumnTableModel fixedColumnTableModel; private JTable floatingColumnTable; private FloatingColumnTableModel floatingColumnTableModel; private class FixedColumnTableModel extends AbstractTableModel { // inner class /** * */ private static final long serialVersionUID = 3935656415101599023L; @Override public int getRowCount() { // TODO Auto-generated method stub return tableValueV.size(); } @Override public int getColumnCount() { // TODO Auto-generated method stub return fixedColumn; } @Override public Object getValueAt(int rowIndex, int columnIndex) { // TODO Auto-generated method stub return tableValueV.get(rowIndex).get(columnIndex); } @Override public String getColumnName(int columnIndex) { return columnNameV.get(columnIndex); } } private class FloatingColumnTableModel extends AbstractTableModel { /** * */ private static final long serialVersionUID = -2481466672947191281L; @Override public int getRowCount() { return tableValueV.size(); } @Override public int getColumnCount() { return columnNameV.size() - fixedColumn; } @Override public Object getValueAt(int rowIndex, int columnIndex) { return tableValueV.get(rowIndex).get(columnIndex + fixedColumn); } @Override public String getColumnName(int columnIndex) { return columnNameV.get(columnIndex + fixedColumn); } } private class MListSelectionListener implements ListSelectionListener { boolean isFixedColumnTable = true; public MListSelectionListener(boolean isFixedColumnTable) { this.isFixedColumnTable = isFixedColumnTable; } @Override public void valueChanged(ListSelectionEvent e) { // TODO Auto-generated method stub if (isFixedColumnTable) { int row = fixedColumnTable.getSelectedRow(); floatingColumnTable.setRowSelectionInterval(row, row); } else { int row = floatingColumnTable.getSelectedRow(); fixedColumnTable.setRowSelectionInterval(row, row); } } } public MFixedColumnTable(Vector<String> columnNameV, Vector<Vector<Object>> tableValueV, int fixedColumn) { super(); setLayout(new BorderLayout()); this.columnNameV = columnNameV; this.tableValueV = tableValueV; this.fixedColumn = fixedColumn; // create fixedColumnTable fixedColumnTableModel = new FixedColumnTableModel(); fixedColumnTable = new JTable(fixedColumnTableModel); ListSelectionModel fixed = fixedColumnTable.getSelectionModel(); fixed.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); fixed.addListSelectionListener(new MListSelectionListener(true)); // create floatingColumnTable floatingColumnTableModel = new FloatingColumnTableModel(); floatingColumnTable = new JTable(floatingColumnTableModel); floatingColumnTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); ListSelectionModel floating = floatingColumnTable.getSelectionModel(); floating.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); floating.addListSelectionListener(new MListSelectionListener(false)); // create scrollPane JScrollPane scrollPane = new JScrollPane(); scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedColumnTable.getTableHeader()); JViewport viewport = new JViewport(); viewport.setView(fixedColumnTable); viewport.setPreferredSize(fixedColumnTable.getPreferredSize()); scrollPane.setRowHeaderView(viewport); // viewport 视口 scrollPane.setViewportView(floatingColumnTable); add(scrollPane, BorderLayout.CENTER); } }
然后,用一个测试提供行标题栏的表格的代码:
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<String> columnNameV = new Vector<String>(); columnNameV.add("日期"); for (int i = 1; i < 21; i++) { columnNameV.add("商品" + i); } Vector<Vector<Object>> tableValueV = new Vector<Vector<Object>>(); for (int row = 1; row < 31; row++) { Vector<Object> rowV = new Vector<Object>(); 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); } }
这是继前几个表格使用和操作的后续,讲解了提供行标题栏的表格的实现方法,以帮助读者拓宽表格的设计思路,同时这也是一种很适用的表格形式。