歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java使用自定義的tableModel,設置可編輯方式

Java使用自定義的tableModel,設置可編輯方式

日期:2017/3/1 10:16:06   编辑:Linux編程

Java使用自定義的tableModel,設置可編輯方式:

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

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