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


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

  1. package com.han;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.util.Vector;  
  5.   
  6. import javax.swing.JFrame;  
  7.   
  8. /** 
  9.  * Provide a fixed column in a table. 
  10.  *  
  11.  * <code><p>public boolean isCellEditable(int row, int column) {<p> 
  12.         return getModel().isCellEditable(convertRowIndexToModel(row),<p> 
  13.                                          convertColumnIndexToModel(column));<p> 
  14.     }<p> 
  15.     </code> 
  16.  *  so we can also directly rewrite the isCellEditable() in the table model. 
  17.  *  
  18.  * @author Gaowen 
  19.  *  
  20.  */  
  21.   
  22. public class JTable4_Modified extends JFrame {  
  23.     /** 
  24.      *  
  25.      */  
  26.     private static final long serialVersionUID = 805308369080023303L;  
  27.   
  28.     public JTable4_Modified() {  
  29.         super();  
  30.         setTitle("提供行标题栏的表格");  
  31.         setBounds(100100500400);  
  32.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  33.   
  34.         Vector<String> columnNameV = new Vector<String>();  
  35.         columnNameV.add("日期");  
  36.         for (int i = 1; i < 21; i++) {  
  37.             columnNameV.add("商品" + i);  
  38.         }  
  39.         Vector<Vector<Object>> tableValueV = new Vector<Vector<Object>>();  
  40.         for (int row = 1; row < 31; row++) {  
  41.             Vector<Object> rowV = new Vector<Object>();  
  42.             rowV.add(row);  
  43.             for (int col = 0; col < 20; col++) {  
  44.                 rowV.add((int) (Math.random() * 1000));  
  45.             }  
  46.             tableValueV.add(rowV);  
  47.         }  
  48.         final MFixedColumnTable_Modified panel = new MFixedColumnTable_Modified(columnNameV,  
  49.                 tableValueV, 1);  
  50.         getContentPane().add(panel, BorderLayout.CENTER);  
  51.     }  
  52.   
  53.     public static void main(String[] args) {  
  54.         // TODO Auto-generated method stub   
  55.         JTable4_Modified frame = new JTable4_Modified();  
  56.         frame.setVisible(true);  
  57.     }  
  58. }  

 

  1. package com.han;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.util.Vector;  
  5.   
  6. import javax.swing.JPanel;  
  7. import javax.swing.JScrollPane;  
  8. import javax.swing.JTable;  
  9. import javax.swing.JViewport;  
  10. import javax.swing.ListSelectionModel;  
  11. import javax.swing.event.ListSelectionEvent;  
  12. import javax.swing.event.ListSelectionListener;  
  13. import javax.swing.table.AbstractTableModel;  
  14.   
  15. /** 
  16.  * <code><p>public boolean isCellEditable(int row, int column) {<p> 
  17.         return getModel().isCellEditable(convertRowIndexToModel(row),<p> 
  18.                                          convertColumnIndexToModel(column));<p> 
  19.     }<p> 
  20.     </code> 
  21.  *  so we can also directly rewrite the isCellEditable() in the table model. 
  22.  *  
  23.  * @author HAN 
  24.  * 
  25.  */  
  26. public class MFixedColumnTable_Modified extends JPanel {  
  27.     /** 
  28.      *  
  29.      */  
  30.     private static final long serialVersionUID = -8001758880985479654L;  
  31.     private Vector<String> columnNameV; // declare the table column name vector   
  32.     private Vector<Vector<Object>> tableValueV; // declare the table value   
  33.                                                 // vector   
  34.     private int fixedColumn = 1// the fixed column number   
  35.     private JTable fixedColumnTable;  
  36.     private FixedColumnTableModel fixedColumnTableModel;  
  37.     private JTable floatingColumnTable;  
  38.     private FloatingColumnTableModel floatingColumnTableModel;  
  39.   
  40.     private class FixedColumnTableModel extends AbstractTableModel { // inner   
  41.                                                                         // class   
  42.   
  43.         /** 
  44.          *  
  45.          */  
  46.         private static final long serialVersionUID = 3935656415101599023L;  
  47.   
  48.         @Override  
  49.         public int getRowCount() {  
  50.             // TODO Auto-generated method stub   
  51.             return tableValueV.size();  
  52.         }  
  53.   
  54.         @Override  
  55.         public int getColumnCount() {  
  56.             // TODO Auto-generated method stub   
  57.             return fixedColumn;  
  58.         }  
  59.   
  60.         @Override  
  61.         public Object getValueAt(int rowIndex, int columnIndex) {  
  62.             // TODO Auto-generated method stub   
  63.             return tableValueV.get(rowIndex).get(columnIndex);  
  64.         }  
  65.   
  66.         @Override  
  67.         public String getColumnName(int columnIndex) {  
  68.             return columnNameV.get(columnIndex);  
  69.         }  
  70.     }  
  71.   
  72.     private class FloatingColumnTableModel extends AbstractTableModel {  
  73.         /** 
  74.          *  
  75.          */  
  76.         private static final long serialVersionUID = -2481466672947191281L;  
  77.           
  78.         @Override  
  79.         public boolean isCellEditable(int row, int column) {  
  80.             return true;  
  81.         }  
  82.   
  83.         @Override  
  84.         public int getRowCount() {  
  85.             return tableValueV.size();  
  86.         }  
  87.   
  88.         @Override  
  89.         public int getColumnCount() {  
  90.             return columnNameV.size() - fixedColumn;  
  91.         }  
  92.   
  93.         @Override  
  94.         public Object getValueAt(int rowIndex, int columnIndex) {  
  95.             return tableValueV.get(rowIndex).get(columnIndex + fixedColumn);  
  96.         }  
  97.   
  98.         @Override  
  99.         public String getColumnName(int columnIndex) {  
  100.             return columnNameV.get(columnIndex + fixedColumn);  
  101.         }  
  102.     }  
  103.   
  104.     private class MListSelectionListener implements ListSelectionListener {  
  105.         boolean isFixedColumnTable = true;  
  106.   
  107.         public MListSelectionListener(boolean isFixedColumnTable) {  
  108.             this.isFixedColumnTable = isFixedColumnTable;  
  109.         }  
  110.   
  111.         @Override  
  112.         public void valueChanged(ListSelectionEvent e) {  
  113.             // TODO Auto-generated method stub   
  114.             if (isFixedColumnTable) {  
  115.                 int row = fixedColumnTable.getSelectedRow();  
  116.                 floatingColumnTable.setRowSelectionInterval(row, row);  
  117.             } else {  
  118.                 int row = floatingColumnTable.getSelectedRow();  
  119.                 fixedColumnTable.setRowSelectionInterval(row, row);  
  120.             }  
  121.         }  
  122.     }  
  123.   
  124.     public MFixedColumnTable_Modified(Vector<String> columnNameV,  
  125.             Vector<Vector<Object>> tableValueV, int fixedColumn) {  
  126.         super();  
  127.         setLayout(new BorderLayout());  
  128.         this.columnNameV = columnNameV;  
  129.         this.tableValueV = tableValueV;  
  130.         this.fixedColumn = fixedColumn;  
  131.         // create fixedColumnTable   
  132.         fixedColumnTableModel = new FixedColumnTableModel();  
  133.         fixedColumnTable = new JTable(fixedColumnTableModel);  
  134.         ListSelectionModel fixed = fixedColumnTable.getSelectionModel();  
  135.         fixed.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  
  136.         fixed.addListSelectionListener(new MListSelectionListener(true));  
  137.         // create floatingColumnTable   
  138.         floatingColumnTableModel = new FloatingColumnTableModel();  
  139.         floatingColumnTable = new JTable(floatingColumnTableModel);  
  140.         floatingColumnTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);  
  141.         ListSelectionModel floating = floatingColumnTable.getSelectionModel();  
  142.         floating.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  
  143.         floating.addListSelectionListener(new MListSelectionListener(false));  
  144.         // create scrollPane   
  145.         JScrollPane scrollPane = new JScrollPane();  
  146.         scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER,  
  147.                 fixedColumnTable.getTableHeader());  
  148.         JViewport viewport = new JViewport();  
  149.         viewport.setView(fixedColumnTable);  
  150.         viewport.setPreferredSize(fixedColumnTable.getPreferredSize());  
  151.         scrollPane.setRowHeaderView(viewport); // viewport 视口   
  152.         scrollPane.setViewportView(floatingColumnTable);  
  153.         add(scrollPane, BorderLayout.CENTER);  
  154.     }  
  155. }  

相关内容