-1

i have this model table and i want to delete a selected row from it.

public class ModelTabel extends AbstractTableModel {

    private Table tSel;
    private Archivio archivio;

    public ModelTabel (Table tSel) {
        this.tSel = tSel;
    }

    public int getRowCount() {
        return tSel.getOrdine().getNumeroCibi();
    }

    public int getColumnCount() {
        return 5;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        if(columnIndex == 0) {
            return this.tSel.getOrdine().getCibo(rowIndex).getNome();
        } else if(columnIndex == 1) {
            if(this.tSel.getOrdine().getCibo(rowIndex).getQuantita() == 0) {
                return "0";
            } else {
                return this.tSel.getOrdine().getCibo(rowIndex).getQuantita();
            }
        } else if(columnIndex == 2) {
            return "€ " + this.tSel.getOrdine().getCibo(rowIndex).getPrezzo();
        } else if(columnIndex == 3) {
            if(this.tSel.getOrdine().getCibo(rowIndex).getQuantita() > 1) {
                return true;             
            } else {
                return false;
            }
        }  else if(columnIndex == 4) {

            return this.tSel.getOrdine().getCibo(rowIndex).getCuoco().getNome() + " " + this.tSel.getOrdine().getCibo(rowIndex).getCuoco().getCognome();

        } 
        return null;
    }

    public String getColumnName(int column){
        if(column == 0) {
            return "Name";
        } else if(column == 1) {
            return "Q.ta";
        } else if(column == 2) {
            return "Price U.";
        } else if(column == 3) {
            return "Wait";
        } else if(column == 4) {
            return "Chef";
        }
        return null;
    }

    public Class getColumnClass(int i) {
        if(i == 3) {
            return Boolean.class;
        }
        return (Class) super.getColumnClass(i);
    }
}

how is it possible from jButton deleting a selected row??? i need the selected row in this code, and then????

public class ActionRemoveRow extends AbstractAction {

    private Control control;

    public ActionRemoveRow (Controllo controllo) {
        this.control = control;
        this.putValue(Action.NAME, "Remove");
        this.putValue(Action.SHORT_DESCRIPTION, "Remove");
        this.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_R));
        this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("CTRL R"));
    }

    public void actionPerformed(ActionEvent e) {
        Vist vist = (Vist) this.control.getVist();
        Panel p = (Panel) vist.getUndetVist(Constant.PANEL);

        int rowSelected = p.getRowSelected();

    }

}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Marc C
  • 73
  • 1
  • 8

1 Answers1

1

Your Action class needs a reference to a current object that will allow you to do this. This could be a reference to the visualized JTable which would allow you to get the selected row. Or you could give the class that holds the JTable a method that gets the selected row and deletes it, give your Action class a reference to this container class, and then call its method. The key here is one of passing references.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @MarcC: no, your question doesn't supply enough information to provide this sort of help, otherwise I or someone else would have done this already. You should consider trying to solve this first using my suggestion, and if this doesn't work, then post your latest attempt, preferably as a [minimal code example program](http://stackoverflow.com/help/mcve) (please read the link) and flag me back. – Hovercraft Full Of Eels Aug 17 '14 at 16:46
  • @MarcC: note, that you should consider giving your model a public `deleteRow(int row)` method that calls the correct `fireXXX(...)` notification method of AbstractTableModel, I think `fireTableRowsDeleted(int firstRow, int lastRow)` after deleting the row(s). Then outside classes can call this method. – Hovercraft Full Of Eels Aug 17 '14 at 16:50