Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to use a method declared in a class extending AbstractTableModel in JTable UI programming

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
}
like image 838
Deniz Avatar asked Nov 26 '25 16:11

Deniz


1 Answers

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);
like image 97
camickr Avatar answered Nov 28 '25 04:11

camickr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!