简体   繁体   中英

Add Rows in JTable

I am trying to insert Dynamic Rows from Array. I am using following code given on Oracle site:

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. I am not used to of Java so could not grasp the idea of doing it.

I am not particularly interested to use AbstractModel, if there's some other way around then most welcome to guide me.

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 ). 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:

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

There is nothing special about your AbstractTableModel implementation. Just use the already implemented 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

  • public void addRow(Object[] rowData) - Adds a row to the end of the model. The new row will contain null values unless rowData is specified. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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