Java AbstractTableModel 教程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9845800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:29:23  来源:igfitidea点击:

AbstractTableModel tutorial

javaabstracttablemodel

提问by Vagelism

I am working on a project that needs to show some data on a jtable. I found many tutorials about jtables but few on how to customise a AbstractTableModel, the most parts are ready code. Even in Oracle's page I found this general jtable tutorial, but few information for AbstractTableModel and how to make a customized model.Oracle Jtable TutorialI am new to programing so will be apriciate a tutorial for my level of skils. Thank you in advanced.

我正在做一个需要在 jtable 上显示一些数据的项目。我找到了很多关于 jtables 的教程,但关于如何自定义 AbstractTableModel 的教程很少,大部分都是现成的代码。即使在 Oracle 的页面中,我也找到了这个通用 jtable 教程,但是关于 AbstractTableModel 和如何制作自定义模型的信息很少。Oracle Jtable 教程我是编程新手,所以我会为我的技能水平提供一个教程。先谢谢了。

采纳答案by lanoxx

The AbstractTableModel contains three methods that need to be overwritten. These are:

AbstractTableModel 包含三个需要覆盖的方法。这些是:

public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);

The JTable uses these methods to find out how many fields (rows and columns) there are and to get the value (as type Object) of each field. When you overwrite these methods it is up to you which kind of data type you want to use. For example you can use a two dimensional Object array:

JTable 使用这些方法来找出有多少字段(行和列)并获取每个字段的值(作为对象类型)。当您覆盖这些方法时,您要使用哪种数据类型取决于您。例如,您可以使用二维对象数组:

Object[][] data;

or an ArrayList of arrays:

或数组的 ArrayList:

List<Object[]> data = new ArrayList<Object[]>();

The fixed sized array might be easier to use but it is more difficult do dynamically add values. Of course you can also use Maps or other data structures. You just need to adjust your implementation of the three methods above to return the proper information from your data structure, such as how many rows your model currently contains, etc.

固定大小的数组可能更容易使用,但动态添加值更困难。当然你也可以使用 Maps 或其他数据结构。您只需要调整上述三种方法的实现,即可从您的数据结构中返回正确的信息,例如您的模型当前包含多少行等。

There are also a couple more methods that canbe overwritten but don't have to. For example, if you want to have custom column names you must additionally overwrite the public String getColumnName(int col)method.

还有一些方法可以被覆盖,但不是必须的。例如,如果您想要自定义列名称,则必须另外覆盖该public String getColumnName(int col)方法。

For example like this:

例如像这样:

private static final String[] COLUMN_NAMES = {"User", "Password", "Age"};
public String getColumnName(int col) {
    return COLUMN_NAMES[col];
}

Look at the Javadoc for AbstractTableModelto get an overview of other methods that can be overwritten.

查看AbstractTableModel的 Javadoc以获得可以覆盖的其他方法的概述。

If you want to be able to change the Data in your TableModel then you need to overwrite the setValueAtmethod (if I am not mistaken):

如果您希望能够更改 TableModel 中的数据,那么您需要覆盖该setValueAt方法(如果我没有记错的话):

void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    //depending on your data structure add the aValue object to the specified
    //rowIndex and columnIndex position in your data object
    //notify the JTable object:
    fireTableCellUpdated(row, col);
}

Important:Whenever you add or remove a row, then the respective function in your TableModel implementation must call the respective fireTableRowsInserted (or deleted) function. Otherwise you will see strange visual effects happen to your JTable:

重要提示:无论何时添加或删除一行,TableModel 实现中的相应函数都必须调用相应的 fireTableRowsInserted(或已删除)函数。否则你会看到 JTable 发生奇怪的视觉效果:

public void addRow(Object[] dates) {
    data.add(dates);
    int row = data.indexOf(dates);
    for(int column = 0; column < dates.length; column++) {
        fireTableCellUpdated(row, column);
    }
    fireTableRowsInserted(row, row);
}