【问题标题】:How do I make a JTable editable in java如何在Java中使JTable可编辑
【发布时间】:2013-04-15 05:22:14
【问题描述】:

我在 java 中使用 JTable,但它不允许我编辑单元格。

private final TableModel dataModel = new AbstractTableModel() {

        public int getColumnCount() { 
            return 5; 
        }

        public int getRowCount() { 
            return 10;
        }

        public Object getValueAt(int row, int col) { 
            return new Integer(row*col); 
        }
};

private final JTable table = new JTable(dataModel);

【问题讨论】:

标签: java swing jtable


【解决方案1】:

添加以下代码

 public boolean isCellEditable(int row, int col)
      { return true; }
 public void setValueAt(Object value, int row, int col) {
    rowData[row][col] = value;
    fireTableCellUpdated(row, col);
  }

您应该有一个用于保存更改的数组

【讨论】:

    【解决方案2】:

    在匿名内部类AbstractTableModel中添加isCellEditable()函数

    public boolean isCellEditable(int row, int col) { 
        return true; 
    }
    

    【讨论】:

      【解决方案3】:

      试试

       private final TableModel dataModel = new AbstractTableModel() {
      
              public int getColumnCount() { 
                  return 5; 
              }
      
              public int getRowCount() { 
                  return 10;
              }
      
              public Object getValueAt(int row, int col) { 
                  return new Integer(row*col); 
              }
      
              public boolean isCellEditable(int row, int col) {
                          return true;
                      }
      };
      

      【讨论】:

        【解决方案4】:

        将 isCellEditable() 添加到您希望它们可编辑的行和列中,例如,如果您不希望某些列(如 ID)可编辑,则返回 false。请记住,您需要将编辑数据保存在某个位置

          public boolean isCellEditable(int row, int col) { 
               return true;  // or false for none editable columns
            }
         public void setValueAt(Object value, int row, int col) {
          rowData[row][col] = value; // save edits some where
          fireTableCellUpdated(row, col); // informe any object about changes
        }
        

        【讨论】:

          猜你喜欢
          • 2015-11-14
          • 1970-01-01
          • 2010-12-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多