【问题标题】:Updating ArrayList<Object[]> within model is producing a compilation error在模型中更新 ArrayList<Object[]> 会产生编译错误
【发布时间】:2015-01-06 20:49:20
【问题描述】:

修改了我发现的使用模型的示例。 最初该样本只会添加一条新记录。 修改了代码以允许编辑字段并包含setValueAt() 调用 为了保持任何变化。 数据存储在对象数组的ArrayList 中,这在尝试更新ArrayList 时会产生错误。

使用时:

al.set(row, value);

它会产生错误:

TableWithModel.java:66: error: no suitable method found for set(int,Object)
                    al.set(row, value);
                      ^
method ArrayList.set(int,Object[]) is not applicable
  (actual argument Object cannot be converted to Object[] by method invocation conversion)
1 error

尝试了各种不同的语法,但得到不同的错误。 一些明显的东西被忽略了,但不确定是什么。

import java.awt.*;
import java.util.*;

import javax.swing.*;
import javax.swing.table.*;

public class TableWithModel extends JFrame {

    MyModel model;

    TableWithModel(Object[][] obj, String[] header) {
        super("Static JTable example");

        JPanel panel = new JPanel(new BorderLayout());
        model = new MyModel(obj, header);
        JTable table = new JTable(model);
        panel.add(new JScrollPane(table));
        add(panel);    // adding panel to frame
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
        pack();
    }

    public static void main(String[] args) {
        String[][] rowAndColumn = {
                {"Row1", "Column2"},
                {"Row2", "Column2"},
                {"Row3", "Column2"},
                {"Row4", "Column2"}
        };
        String[] header = {"Column1", "Column2"};
        TableWithModel twm = new TableWithModel(rowAndColumn, header);
    }

    class MyModel extends AbstractTableModel {

        ArrayList<Object[]> al;
        String[] header;

        MyModel(Object[][] obj, String[] header) {
            this.header = header;
            al = new ArrayList<Object[]>();
            for(int i = 0; i < obj.length; ++i)
                al.add(obj[i]);
        }
        public int getRowCount() {
            return al.size();
        }

        public int getColumnCount() {
            return header.length;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            return al.get(rowIndex)[columnIndex];
        }

        public String getColumnName(int index) {
            return header[index];
        }

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

        public void setValueAt(Object value, int row, int col) {
            al.set(row, value);
            fireTableCellUpdated(row, col);
        }

    }
}

【问题讨论】:

    标签: java arrays swing arraylist


    【解决方案1】:

    ArrayList 变量al 保存对象的数组。要更新存储在ArrayList 中索引rowcol 列的对象数组元素,您需要使用以下内容:

    public void setValueAt(Object value, int row, int col) {
        al.get(row)[col] = value;
        fireTableCellUpdated(row, col);
    }
    

    al.get(row)[col] = value; 是以下内容的简写:

    Object[] data = al.get(row);
    data[col] = value;
    

    这与 getValueAt 方法中已经实现的类似,只是您设置的是数组元素而不是检索它。

    al.set(row, value); 无法编译的原因是set 方法期望第二个参数是Object[] 数组,而不是Object,因为这是ArrayList 所持有的。

    【讨论】:

      【解决方案2】:

      感谢您的回复。 进行了快速测试,它可以工作。 有点奇怪的是调用“get”而不是“set”。

      发布后不久,我就能够使用“set”找到解决方案

                 Object[] data = al.get(row);
                 data[col] = value;
                 al.set(row, data);
      

      使用 set 是有意义的,但是是什么让它在您的示例中使用“get”起作用?

      【讨论】:

      • 这可行,但不需要al.set(row, data);。语句al.get(row)[col] = value; 完全等同于Object[] data = al.get(row); data[col] = value;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 2014-11-19
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      相关资源
      最近更新 更多