从jTable中删除选定的行

时间:2014-08-17 12:24:17

标签: java swing jtable row action-interface

我有这个模型表,我想从中删除一个选定的行。

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

如何从jButton删除所选行? 我需要在此代码中选择行,然后????

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

    }

}

1 个答案:

答案 0 :(得分:1)

您的Action类需要对当前对象的引用,以允许您执行此操作。这可以是对可视化JTable的引用,它允许您获取所选行。或者你可以给持有JTable的类一个获取所选行并删除它的方法,给你的Action类引用这个容器类,然后调用它的方法。这里的关键是传递参考文献。