简体   繁体   English

在JTable中添加行

[英]Add Rows in JTable

I am trying to insert Dynamic Rows from Array. 我正在尝试从数组插入动态行。 I am using following code given on Oracle site: 我正在使用以下Oracle网站上给出的代码:

class mYModel extends AbstractTableModel
{
    Object rowData[][] = { {Boolean.TRUE ,"11","OMF","C++","Jhon Doe",22}};
    Object[] arr = new Object[5];

    String columnNames[] = {
                            "Action",
                            "Pages",
                            "Name",
                            "Title",
                            "Author",
                            "TimeStamp"
                        };

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

  public String getColumnName(int column) {
    return columnNames[column];
  }

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

  public Object getValueAt(int row, int column) {
    return rowData[row][column];
  }

  public Class getColumnClass(int column) {
    return (getValueAt(0, column).getClass());
  }
  @Override
  public void setValueAt(Object value, int row, int column) {
    rowData[row][column] = value;
  }
  @Override
  public boolean isCellEditable(int row, int column) {
    return (column == 0);
  }
}

What I want that rowData[][] gets value dynamically rather than I initialize it. 我想要的rowData [] []是动态获取值,而不是初始化它。 I am not used to of Java so could not grasp the idea of doing it. 我不习惯Java,因此无法掌握执行Java的想法。

I am not particularly interested to use AbstractModel, if there's some other way around then most welcome to guide me. 我对使用AbstractModel并不是特别感兴趣,如果有其他解决方法,那么非常欢迎您来指导我。

instead of using an array of a fixed size, you could use something like a list: 而不是使用固定大小的数组,可以使用类似列表的方法:

Object rowData[][] = { {Boolean.TRUE ,"11","OMF","C++","Jhon Doe",22}};

to

ArrayList<Object[]> rowData;

etc. then to add a row you'd do rowData.add( stuff ). 等等,然后添加一行,您可以执行rowData.add(stuff)。 you'd have to convert all the methods to reference the size of the rows, etc. 您必须转换所有方法以引用行的大小,等等。

But more likely, instead of using that at all, use something like DefaultTableModel (or some other tablemodel implementation) instead of arrays like that: 但是更有可能,而不是根本不使用它,而使用像DefaultTableModel (或其他一些tablemodel实现)之类的东西而不是像这样的数组:

http://docs.oracle.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html http://docs.oracle.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html

There is nothing special about your AbstractTableModel implementation. 您的AbstractTableModel实现没有什么特别的。 Just use the already implemented DefaultTableModel 只需使用已经实现的DefaultTableModel

 String columnNames[] = {
                        "Action",
                        "Pages",
                        "Name",
                        "Title",
                        "Author",
                        "TimeStamp"
                    };
 DefualtTableModel model = new DefaultTableModel(columnNames, 0);  <-- 0 is row count
 JTable table = new JTable(model);

Then just use this method from DefaultTableModel 然后只需从DefaultTableModel使用此方法

  • public void addRow(Object[] rowData) - Adds a row to the end of the model. public void addRow(Object[] rowData) -在模型的末尾添加一行。 The new row will contain null values unless rowData is specified. 除非指定rowData否则新行将包含空值。 Notification of the row being added will be generated. 将生成有关添加行的通知。

So whenever you want to add a row just do this 因此,只要您想添加一行,就可以这样做

Object[] row = { data1, data2, data2, data4, data5, data6 };
model.addRow(row);

If you need extra functionality, you could always extend it, or if you just wanted to override getColumnClass() to get a check box, you could just do this 如果您需要额外的功能,则可以随时对其进行扩展,或者如果您只想重写getColumnClass()以获取一个复选框,则可以这样做

DefaultTableModel model = new DefaultTableModel(columnNames, 0) {
    @Override
    public Class getColumnClass(int column) {
        return (getValueAt(0, column).getClass());
    }
};

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

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