Results 1 to 7 of 7
  1. #1
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default 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();
            }
        }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Moved from Advanced Java

    db

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    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

  4. #4
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    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!

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    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.

  6. #6
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    Good tip! What do you think about the hashmap idea?

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    You haven't stated what your requirement is, or what is wrong with the DeaultTableModel, so I would say I don't like your Hashmap idea because it appears you are reinventing the wheel for no reason.

Similar Threads

  1. Replies: 0
    Last Post: 05-03-2011, 05:45 AM
  2. Jtable rows
    By riddhishah28 in forum AWT / Swing
    Replies: 3
    Last Post: 02-18-2011, 06:24 AM
  3. Returning a ResultSet to custom JTable Model
    By Kenjitsuka in forum New To Java
    Replies: 9
    Last Post: 10-16-2010, 09:17 PM
  4. accessing custom data model values - how?
    By dave247 in forum New To Java
    Replies: 11
    Last Post: 03-07-2010, 12:20 PM
  5. Replies: 1
    Last Post: 04-23-2009, 07:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •