-1

I have a serious problem, removing rows from my JTable, my code is the same as what I saw when I tried to learn AbstractTableModel:

import javax.swing.table.AbstractTableModel;

public class MyTableModel extends AbstractTableModel {

             boolean DEBUG = true;

            input_Data input = new input_Data();
            String[] columnNames = {"First Name",
                      "Last Name",
                      "Sport",
                      "# of Years",
                      "Vegetarian"};
            Object[][] data = {
                {"Kathy", "Smith",
                    "Snowboarding", new Integer(5), new Boolean(false)},
                {"John", "Doe",
                        "Rowing", new Integer(3), new Boolean(true)},
                {"Sue", "Black",
                            "Knitting", new Integer(2), new Boolean(false)},
                {"Jane", "White",
                                "Speed reading", new Integer(20), new Boolean(true)},
                {"Joe", "Brown",
                                    "Pool", new Integer(10), new Boolean(false)};


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

                    public int getRowCount() {
                        return data.length;
                    }

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

                    public Object getValueAt(int row, int col) {
                        return data[row][col];
                    }
                    public Class getColumnClass(int c) {
                        return getValueAt(0, c).getClass();
                    }


                    public boolean isCellEditable(int row, int col) {

                        if (col < 2) {
                            return false;
                        } else {
                            return true;
                        }
                    }
                    public void removeRow(Row)
                    {

                       fireTableRowsDeleted(Row,Row);

                    }
                }

But the problem is that nothing happens when I call removeRow!! I think that maybe I should edit data too, but how? I am new to java and I really got stock into this problem...

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Marjan
  • 1
  • 2
  • Use the `DefaultTableModel`. It supports a removeRow() method already. – camickr Feb 16 '16 at 20:34
  • I cann't, I need a check box in my table, so I cann't use DefaultTableModel – Marjan Feb 17 '16 at 03:58
  • There is not reason you can't use a DefaultTableModel for that. You just store a Boolean value in the TableModel and override the `getColumnClass(...)` method to return Boolean and the table will choose an appropriate renderer. There is no reason to create a completely new class just to override a single method of the class. – camickr Feb 17 '16 at 04:21
  • thanks a lot my problem solved :) – Marjan Feb 21 '16 at 08:15

1 Answers1

3

You've not actually removed anything with this code. Don't use a 2-D array for your data nucleus. Use an ArrayList<CustomType> for the data nucleus of this class, actually remove the row of data from this List within your removeRow(int row) method, and then call the fireTableRowsDeleted(...) method.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373