Adding rows to jtable using custom table model

I created a table model that extends DefaultTableModel.

public class TableModel extends DefaultTableModel { List<VariableDetails> data; public AttachedVariableTableModel(){ super(); this.data=Collections.synchronizedList(new ArrayList<VariableDetails>()); } //method for adding rows public void addRow(VariableDetails varDetails){ this.data.add(varDetails); fireTableRowsInserted(this.data.size()-1,this.data.size()-1); } } 

I tried adding rows to a table that already has data.

 tModel.addRow(new VariableDetails()); 

but lines cannot be added. There are no exceptions and errors. what is really wrong here? How can I solve this? Thanks in advance.

+4
source share
2 answers
  • why there is a reason super();

  • DefaultTableModel can add Object[] or Vector

  • you need to override AbstractTableModel , not DefaultTableModel , override all get/set methods using fireXxxXxx() in the methods, otherwise there are no visible ones ( JTable )

  • may start with a List Table Model or a String Table Model

+2
source

May I offer you a complete example of a table model to find out how this works. It also uses the list as data. Most importantly, you need to extend AbstractTableModel to use your own variable to store data. Here is a complete sample source code.

 import java.util.ArrayList; import java.util.List; import javax.swing.table.AbstractTableModel; public class MouseClickTableModel extends AbstractTableModel { /** * */ private static final long serialVersionUID = -1807522184370383957L; private final String[] columnNames = { "Sr", "X", "Y", "Delay (ms)", "Comment" }; public final Class[] mColTypes = { Integer.class, Integer.class, Integer.class, Integer.class, String.class }; private final List<MouseClick> data; public MouseClickTableModel(){ data = new ArrayList<MouseClick>(10); } public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.size(); } public String getColumnName(int col) { return columnNames[col]; } public Object getValueAt(int row, int col) { final MouseClick currentRow = (MouseClick) data.get(row); switch (col) { case 0: return currentRow.getSequNb(); case 1: return currentRow.getXcoo(); case 2: return currentRow.getXycoo(); case 3: return currentRow.getDelay(); case 4: return currentRow.getComment(); } return new String(); } public Class getColumnClass(int c) { return mColTypes[c]; } /* * Don't need to implement this method unless your table editable. */ public boolean isCellEditable(int row, int col) { return false; } public void updateRow(Object value, int row, int col) { } /* * Don't need to implement this method unless your table data can change. */ @Override public void setValueAt(Object value, int row, int col) { MouseClick currentRow = null; if (row >= data.size()) { // new data currentRow = new MouseClick(); data.add(0, currentRow); } // update row else { currentRow = (MouseClick) data.get(row); } switch (col) { case 0: currentRow.setSequNb(((Integer) value).intValue()); break; case 1: currentRow.setXcoo(((Integer) value).intValue()); break; case 2: currentRow.setXycoo(((Integer) value).intValue()); break; case 3: currentRow.setDelay(((Integer) value).intValue()); break; case 4: currentRow.setComment(((String) value).toString()); break; } // update fireTableCellUpdated(row, col); } public MouseClick getData(int row) { return data.get(row); } public void addMouseClick(MouseClick mc) { insertMouseClick(getRowCount(), mc); } public void insertMouseClick(int row, MouseClick mc) { data.add(row, mc); fireTableRowsInserted(row, row); } public void removeMouseClick(int row) { data.remove(row); fireTableRowsDeleted(row, row); } } 

Then you just need to update your model with ui design.

 JTable table = new JTable(new MouseClickTableModel()); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); MouseClickTableModel model = getTable().getModel()); model.insertMouseClick(0, new MouseClick(0, Integer.valueOf(xValue), Integer.valueOf(yValue), 2000, "comment")); 
0
source

Source: https://habr.com/ru/post/1444738/


All Articles