简体   繁体   English

Java:如果通过AbstractTableModel插入行,如何将行(数据)显式插入JTable

[英]Java: How to insert rows(data) into JTable explicitly if rows are inserted by AbstractTableModel

In my application there is a JTable and I want to insert row after creating table. 在我的应用程序中,有一个JTable,我想在创建表后插入行。

Following all codes are in the constructor of the frame. 以下所有代码都在框架的构造函数中。

Code: 码:

private TableModel model = new AbstractTableModel(){
String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"};
private Object[][] data = {};

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

            public int getRowCount() {
                return data.length;
            }

            public String getColumnName(int col) {
                return columnNames[col];
            }
            public Class getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }

            public Object getValueAt(int row, int col) {
                return data[row][col];
            }
            public boolean isCellEditable(int row, int col) {
                retuen false;
            }
            public Class getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }
};

table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);

JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(5, 218, 884, 194);
//now adding this to the frame where I want to show 
frame.add(scrollPane);

now I want to insert rows or data into the table. 现在我想在表中插入行或数据。 How this is possible. 这怎么可能。 Previously I used DefaultTableModel but we cannot use isCellEditable and other method in DefaultTableModel so I change that with the code above. 以前,我使用DefaultTableModel,但是我们不能在DefaultTableModel中使用isCellEditable和其他方法,因此我使用上面的代码对其进行了更改。 But in the above code I am not able to insert data (rows) explicitly, please help me. 但是在上面的代码中,我无法显式插入数据(行),请帮助我。

Like this, you can implement other method like deleteRow(int rowIndex) and insertRowToIndex(int rowIndex, List rowData). 这样,您可以实现其他方法,例如deleteRow(int rowIndex)和insertRowToIndex(int rowIndex,List rowData)。

Rememer after you change the data, you have to fire the table event, like fireTableRowsInserted() etc 更改数据后,请记住,必须启动表事件,例如fireTableRowsInserted()等

public static class MyTableModel extends AbstractTableModel
{
    private List<String> columnNames = new ArrayList();
    private List<List> data = new ArrayList();

    {
        columnNames.add("First Name");
        columnNames.add("Last Name");
        columnNames.add("Sport");
        columnNames.add("# of Years");
        columnNames.add("Vegetarian");
    }

    public void addRow(List rowData)
    {
        data.add(rowData);
        fireTableRowsInserted(data.size() - 1, data.size() - 1);
    }

    public int getColumnCount()
    {
        return columnNames.size();
    }

    public int getRowCount()
    {
        return data.size();
    }

    public String getColumnName(int col)
    {
        try
        {
            return columnNames.get(col);
        }
        catch(Exception e)
        {
            return null;
        }
    }

    public Object getValueAt(int row, int col)
    {
        return data.get(row).get(col);
    }

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

    public Class getColumnClass(int c)
    {
        return getValueAt(0, c).getClass();
    }
};

public static void main(String[] args)
{
    MyTableModel model = new MyTableModel();

    model.addRow(Arrays.asList("yi", "chen", "sleep", 35, true));

    JTable table = new JTable(model);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setBounds(5, 218, 884, 194);
    //now adding this to the frame where I want to show 
    JFrame frame = new JFrame();
    frame.add(scrollPane);
    frame.setVisible(true);
}

仅从DefaultTableModel扩展而不是从AbstractTableModel扩展就可以解决问题。

AbstractTableModel will not do what you want to do, For this purpose you need to use DefaultTableModel, AbstractTableModel不会做您想要做的,为此,您需要使用DefaultTableModel,

When you use DefaultTableModel you can set isCellEditable or any other method in following ways, 使用DefaultTableModel时,可以通过以下方式设置isCellEditable或任何其他方法,

    DefaultTableModel model = new DefaultTableModel(data, columnNames) {
        private static final long serialVersionUID = 1L;

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }       
    };

in above code data will be String[ ][ ]. 在上面的代码中,数据将为String [] []。 And by writing above code you are setting cell editable as false. 通过编写以上代码,您将可编辑单元格设置为false。

Other than this you can also load data in swingworker or afterwords like you might have done. 除此之外,您还可以像平时一样将数据加载到swingworker或后缀中。

@Peter is right +1 use DefaultTableModel rather than AbstractTableModel , because your AbstractTableModel missed method addRow(); @Peter是正确的+1使用DefaultTableModel而不是AbstractTableModel ,因为您的AbstractTableModel缺少方法addRow(); (and another methods too) (还有其他方法)

public void addRow(Object[][] data) {
    this.data.add(data);
    this.fireTableRowsInserted(data.size() - 1, data.size() - 1);
}

example about AbstractTableModel 有关AbstractTableModel的示例

I think you need to implement setValueAt() function in your AbstractTableModel so that you can modify any data given the position. 我认为您需要在AbstractTableModel中实现setValueAt()函数,以便您可以根据位置修改任何数据。

Take a close look at the following tutorials: 请仔细阅读以下教程:

http://www.java2s.com/Code/Java/Swing-JFC/TablewithacustomTableModel.htm http://www.java2s.com/Code/Java/Swing-JFC/TablewithacustomTableModel.htm

http://www.informit.com/articles/article.aspx?p=332278 http://www.informit.com/articles/article.aspx?p=332278

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM