-1

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

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

    }

}
4

1 回答 1

1

您的 Action 类需要对允许您执行此操作的当前对象的引用。这可能是对可视化 JTable 的引用,它允许您获取选定的行。或者,您可以为持有 JTable 的类提供一个获取选定行并将其删除的方法,为您的 Action 类提供对此容器类的引用,然后调用其方法。这里的关键是传递引用之一。

于 2014-08-17T12:52:02.207 回答