2

I am getting the message "method is never used locally" after I've implemented the removeRow method. I am also unable to use/access this method.

class TableModel extends AbstractTableModel {

    private String[] columnNames = {"ID", "Name"};

         ArrayList<Entry> list;

         public TableModel(Entry[] entries) {
              // assigns entries to list
         }

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

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

         public int getRowCount() {
              return list.size();
         }

             // this method gives a "never used locally" message
         public void removeRow(int row)
         {
             list.remove(row);
             fireTableRowsDeleted(row, row);
         }

         public Object getValueAt(int row, int col) {
              Entry entry = list.get(row);
              if(entry != null)
              {
                  switch (col) {
                  case 0:
                       return entry.getId();
                  case 1:
                       return entry.getName();
                  default:
                       return "";
                  }
              }
         }
     }

I then try to access the removeRow(int row) by the following way when the delete button is pressed:

public void actionPerformed(ActionEvent event)
{
          int i =1;
      table.getModel().removeRow(i); // removeRow not recognised
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Deniz
  • 253
  • 2
  • 7
  • 12

2 Answers2

5
class TableModel extends AbstractTableModel {

TableModel is an interface. Use a better name for your class. (I don't know what will happen when you try to make a class name the same as an interface name). Instead you should use something like:

class EntryTableModel extends AbstractTableModel {

Since your model is used to contain "Entry" objects.

table.getModel().removeRow(i);

The above code is confusing because the getModel() method returns a TableModel, but is this really the TableModel interface or the TableModel class?

To use your custom model your code should be something like:

TableModel model = table.getModel();
EntryTableModel entryModel = (EntryTableModel)model;
entryModel.removeRow(i);
camickr
  • 321,443
  • 19
  • 166
  • 288
4

You should cast to your class. When you do table.getModel you obtain an AbstractTableModel, which does not contain a method called removeRow

try

((TableModel)table.getModel()).removeRow(i)
perencia
  • 1,498
  • 3
  • 11
  • 19
  • Dude I was stuck on this for ages.. Thank you! Everything works perfect - it was just a matter of getting the method recognised.. – Deniz Apr 29 '14 at 20:38
  • 1
    Note it is very confusing naming your class `TableModel` as this is also the name of the implemented interface (`javax.swing.table.TableModel`). You should rename it to something else to avoid confusion! – Gyro Gearless Apr 29 '14 at 20:48