Results 1 to 7 of 7
- 06-07-2011, 08:47 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
Inserting rows to jtable with custom model
Hello everyone
I have created a jtable with a custom model that extends defaulttablemodel. The issue is that my insertRow method does not insert a blank line for me to type the new values at the bottom. I am using a hashmap with a point variable for the key. there are no exceptions or anything, just that nothing happens (i checked with the netbeans debugger and it is being called...)
Any ideas?
Java Code:private class EntityTableModel extends DefaultTableModel { private HashMap lookup; private int rows; private int columns; private String[] headers; public EntityTableModel(String[] headers, int rows) { this.rows = rows; this.headers = headers; this.columns = headers.length; lookup = new HashMap(); } @Override public Object getValueAt(int row, int col) { return lookup.get(new Point(row, col)); } @Override public void setValueAt(Object value, int row, int column) { if ((rows < 0) || (columns < 0)) { throw new IllegalArgumentException("Invalid row/column setting"); } if ((row < rows) && (column < columns)) { lookup.put(new Point(row, column), value); } fireTableCellUpdated(row, column); } @Override public int getColumnCount() { return columns; } @Override public int getRowCount() { return rows; } @Override public String getColumnName(int column) { return headers[column]; } @Override public void insertRow(int row, Object[] rowData) { int columnCount = getColumnCount(); for (int i = 0; i < columnCount; i++) { lookup.put(new Point(row, i), new Object()); } fireTableDataChanged(); } }
- 06-07-2011, 09:08 PM #2
Moved from Advanced Java
db
- 06-07-2011, 09:13 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Thats because your getRowCount() method still returns the old rows value!
The whole model looks a bit funny!
Usually you would add an object to a list, and then return the list.size() at the getRowCount method .I do not understand why you used the point class here
- 06-07-2011, 10:09 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
Well a hashmap has 2 "columns" and i needed 3 so i thought what the heck, it works pretty well! Anyway i added rows++ in my method and it works like a charm! Thanks so much!
- 06-08-2011, 02:40 AM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
You should NOT be extending DefaultTableModel. The DefaultTableModel already implement storage for the rowcolumn data and the headings. There are many method of the DefaultTableModel that you did not override. If somebody happens to invoke those methods then they will be referring to the data of the DefaultTableModel and not your custom model.
Instead, because you are implementing your own storage you should be extending AbstractTableModel directly.
- 06-08-2011, 03:43 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
Good tip! What do you think about the hashmap idea?
- 06-08-2011, 05:16 PM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Similar Threads
-
JPA 2.0 CascadeType.ALL is inserting two rows for each call
By dramesh123 in forum Advanced JavaReplies: 0Last Post: 05-03-2011, 05:45 AM -
Jtable rows
By riddhishah28 in forum AWT / SwingReplies: 3Last Post: 02-18-2011, 06:24 AM -
Returning a ResultSet to custom JTable Model
By Kenjitsuka in forum New To JavaReplies: 9Last Post: 10-16-2010, 09:17 PM -
accessing custom data model values - how?
By dave247 in forum New To JavaReplies: 11Last Post: 03-07-2010, 12:20 PM -
inserting multiple rows from one table into another
By xcallmejudasx in forum JDBCReplies: 1Last Post: 04-23-2009, 07:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks