Java使用自定义的tableModel,设置可编辑方式

来源:互联网 发布:遗传算法交叉变异概率 编辑:程序博客网 时间:2024/05/31 18:56

package com.han;import java.awt.BorderLayout;import java.util.Vector;import javax.swing.JFrame;/** * Provide a fixed column in a table. *  * <code><p>public boolean isCellEditable(int row, int column) {<p>        return getModel().isCellEditable(convertRowIndexToModel(row),<p>                                         convertColumnIndexToModel(column));<p>    }<p>    </code> *  so we can also directly rewrite the isCellEditable() in the table model. *  * @author Gaowen *  */public class JTable4_Modified extends JFrame {/** *  */private static final long serialVersionUID = 805308369080023303L;public JTable4_Modified() {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_Modified panel = new MFixedColumnTable_Modified(columnNameV,tableValueV, 1);getContentPane().add(panel, BorderLayout.CENTER);}public static void main(String[] args) {// TODO Auto-generated method stubJTable4_Modified frame = new JTable4_Modified();frame.setVisible(true);}}

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;/** * <code><p>public boolean isCellEditable(int row, int column) {<p>        return getModel().isCellEditable(convertRowIndexToModel(row),<p>                                         convertColumnIndexToModel(column));<p>    }<p>    </code> *  so we can also directly rewrite the isCellEditable() in the table model. *  * @author HAN * */public class MFixedColumnTable_Modified extends JPanel {/** *  */private static final long serialVersionUID = -8001758880985479654L;private Vector<String> columnNameV; // declare the table column name vectorprivate Vector<Vector<Object>> tableValueV; // declare the table value// vectorprivate int fixedColumn = 1; // the fixed column numberprivate JTable fixedColumnTable;private FixedColumnTableModel fixedColumnTableModel;private JTable floatingColumnTable;private FloatingColumnTableModel floatingColumnTableModel;private class FixedColumnTableModel extends AbstractTableModel { // inner// class/** *  */private static final long serialVersionUID = 3935656415101599023L;@Overridepublic int getRowCount() {// TODO Auto-generated method stubreturn tableValueV.size();}@Overridepublic int getColumnCount() {// TODO Auto-generated method stubreturn fixedColumn;}@Overridepublic Object getValueAt(int rowIndex, int columnIndex) {// TODO Auto-generated method stubreturn tableValueV.get(rowIndex).get(columnIndex);}@Overridepublic String getColumnName(int columnIndex) {return columnNameV.get(columnIndex);}}private class FloatingColumnTableModel extends AbstractTableModel {/** *  */private static final long serialVersionUID = -2481466672947191281L;@Overridepublic boolean isCellEditable(int row, int column) {return true;}@Overridepublic int getRowCount() {return tableValueV.size();}@Overridepublic int getColumnCount() {return columnNameV.size() - fixedColumn;}@Overridepublic Object getValueAt(int rowIndex, int columnIndex) {return tableValueV.get(rowIndex).get(columnIndex + fixedColumn);}@Overridepublic String getColumnName(int columnIndex) {return columnNameV.get(columnIndex + fixedColumn);}}private class MListSelectionListener implements ListSelectionListener {boolean isFixedColumnTable = true;public MListSelectionListener(boolean isFixedColumnTable) {this.isFixedColumnTable = isFixedColumnTable;}@Overridepublic void valueChanged(ListSelectionEvent e) {// TODO Auto-generated method stubif (isFixedColumnTable) {int row = fixedColumnTable.getSelectedRow();floatingColumnTable.setRowSelectionInterval(row, row);} else {int row = floatingColumnTable.getSelectedRow();fixedColumnTable.setRowSelectionInterval(row, row);}}}public MFixedColumnTable_Modified(Vector<String> columnNameV,Vector<Vector<Object>> tableValueV, int fixedColumn) {super();setLayout(new BorderLayout());this.columnNameV = columnNameV;this.tableValueV = tableValueV;this.fixedColumn = fixedColumn;// create fixedColumnTablefixedColumnTableModel = new FixedColumnTableModel();fixedColumnTable = new JTable(fixedColumnTableModel);ListSelectionModel fixed = fixedColumnTable.getSelectionModel();fixed.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);fixed.addListSelectionListener(new MListSelectionListener(true));// create floatingColumnTablefloatingColumnTableModel = 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 scrollPaneJScrollPane 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);}}


原创粉丝点击