java如何按索引删除行
我正在构建一个使用JTable和DefaultTableModel的Java桌面应用程序
现在,我已经实现了这个表的KeyEventListener。因此,如果我尝试单击键盘上的CANC按钮,我想删除选中的行。但有一种奇怪的行为
这是我的第一张桌子:
现在我选择倒数第二行,然后按下键盘上的“取消”按钮。这就是结果
如您所见,删除倒数第二行,但不刷新JTable
这是evetListener:
public class KeyListenerTableFattura extends KeyAdapter {
private JTable table2;
private MyTableModelFatturaManuale tableModel;
public KeyListenerTableFattura(JTable table2) {
this.table2 = table2;
this.tableModel = (MyTableModelFatturaManuale)table2.getModel();
}
public void keyReleased (KeyEvent ke) {
if (table2.getSelectedColumn() == 0) {
//tasto canc
if (ke.getKeyCode() == 127) {
try{
int riga= table.getSelectedRow();
ListSelectionModel selectionModel = table.getSelectionModel();
selectionModel.setSelectionInterval(0,0);
tableModel.rimuoviSpesa(riga);
}
catch(Exception e)
{
log.logStackTrace(e);
}
}
}
}
@SuppressWarnings("unused")
private void printIt(String title, KeyEvent keyEvent) {
}
}
这是桌面模型
package com.mcsolution.commercialista.table.tableModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import com.mcsolution.commercialista.beans.FatturaRighe;
import com.mcsolution.common.supporto.VisualMessage;
public class MyTableModelFatturaManuale extends defaultTableModel{
/**
*
*/
private static final long serialVersionUID = -3163597322286902696L;
public List<FatturaRighe> v = new ArrayList<FatturaRighe>();
public static String[] ColName = {"Descrizione","Importo"};
private Double percEnpaCL=0.0,percIva=0.0,perRitenuta=0.0;
private Double imponibileIva =0.0,empaCL=0.0,iva=0.0,ritenutaAcconto=0.0,lordo=0.0,netto=0.0,
totale=0.0;
private int rigaParziale =7;
private int rigaEnpaCl =6;
private int rigaImponibileIva =5;
private int rigaIva =4;
private int rigaRitenuta =3;
private int rigaTotaleLordo =2;
private int rigaNetto =1;
private int rigaTotale =0;
public MyTableModelFatturaManuale() {
super(ColName,0);
}
@SuppressWarnings("rawtypes")
public void inserisciRigheFisse(){
this.addRow(new Vector());
super.setValueAt("PARZIALE", this.getRowCount()-1, 0);
super.setValueAt("0,00", this.getRowCount()-1, 1);
this.addRow(new Vector());
super.setValueAt("ENPACL 0 %", this.getRowCount()-1, 0);
super.setValueAt("0,00", this.getRowCount()-1, 1);
this.addRow(new Vector());
super.setValueAt("IMPONIBILE I.V.A.", this.getRowCount()-1, 0);
super.setValueAt("0,00", this.getRowCount()-1, 1);
this.addRow(new Vector());
super.setValueAt("I.V.A. 0 %", this.getRowCount()-1, 0);
super.setValueAt("0,00", this.getRowCount()-1, 1);
this.addRow(new Vector());
super.setValueAt("RIT.ACCONTO 0 %", this.getRowCount()-1, 0);
super.setValueAt("0,00", this.getRowCount()-1, 1);
this.addRow(new Vector());
super.setValueAt("TOTALE LORDO", this.getRowCount()-1, 0);
super.setValueAt("0,00", this.getRowCount()-1, 1);
this.addRow(new Vector());
super.setValueAt("NETTO", this.getRowCount()-1, 0);
super.setValueAt("0,00", this.getRowCount()-1, 1);
this.addRow(new Vector());
super.setValueAt("TOTALE", this.getRowCount()-1, 0);
super.setValueAt("0,00", this.getRowCount()-1, 1);
}
public void settaSpesa(FatturaRighe f){
this.v.add(f);
int riga = this.getRowCount()-1;
this.insertRow(riga, new Vector());
super.setValueAt(f.getDescrizione(), riga, 0);
super.setValueAt("0.00", riga, 1);
}
private Integer getCountSpeseTaxFree(){
int cont=0;
for (FatturaRighe object : this.v) {
if(!object.getSoggettoIVA())
cont++;
}
return cont;
}
public void settaEnpaCl(Double enpa){
this.percEnpaCL=enpa;
//modifico l'enpaCL
super.setValueAt("ENPACL "+this.percEnpaCL+" %", this.getRowCount()-1-rigaEnpaCl-getCountSpeseTaxFree(), 0);
calcolaTotale();
}
public void settaIva(Double iva){
this.percIva=iva;
//modifico l'enpaCL
super.setValueAt("I.V.A. "+this.percIva+" %", this.getRowCount()-1-rigaIva-getCountSpeseTaxFree(), 0);
calcolaTotale();
}
public void settaRitenuta(Double ritenuta){
this.perRitenuta=ritenuta;
//modifico l'enpaCL
super.setValueAt("RIT.ACCONTO "+this.perRitenuta+" %", this.getRowCount()-1-rigaIva-getCountSpeseTaxFree(), 0);
calcolaTotale();
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
//verifico quante solo le righe della tabella
int righeEsentiIva = getCountSpeseTaxFree();
int righeIva = this.v.size() - righeEsentiIva;
//se è l'ultima riga non si modifica
if(rowIndex == this.getRowCount()-1) //metto il meno uno perchè a contare si paprte dallo 0
return false;
else if(rowIndex < righeIva)
return true;
else if(rowIndex >= righeIva+7 && rowIndex < this.getRowCount())
return true;
return false;
}
public void inserisciRiga(){
FatturaRighe f = new FatturaRighe();
f.setImporto(0.00);
f.setSoggettoIVA(true);
this.v.add(0,f);
this.insertRow(0, new Vector());
super.setValueAt("", 0, 0);
super.setValueAt("0.00", 0, 1);
}
public void rimuoviRiga(){
//conto il numero di righe presenti
int righeEsistenti = this.v.size() - getCountSpeseTaxFree();
if(righeEsistenti >0){
this.v.remove(0);
this.removeRow(0);
calcolaTotale();
}
}
public void rimuoviSpesa(int nRiga){
//conto il numero di righe presenti
int righeIvaEsente = getCountSpeseTaxFree();
if(righeIvaEsente<=0)
return;
this.removeRow(nRiga);
//mi vado a calcolare nella lista dove si trova questa riga
int nPosizioneInLista = nRiga - 7;
this.v.remove(nPosizioneInLista);
calcolaTotale();
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
try{
//verifico se la riga è già esistente oppure va modificata
//altero il valore di rowIndex perchè ci sono le 6 righe fisse
int rigaIniziale = rowIndex;
if(rowIndex >7)
rowIndex = rowIndex-7;
FatturaRighe f = rowIndex< this.v.size() ? this.v.get(rowIndex) : null;
if(f==null){
f = new FatturaRighe();
f.setSoggettoIVA(true);
//mi calcolo quante spese ci sono
int spese = getCountSpeseTaxFree();
if(spese>0){
this.v.add(rowIndex-spese,f);
}else
this.v.add(f);
}
if(columnIndex==0){
//descrizion
f.setDescrizione(aValue.toString());
}else if(columnIndex==1){
//importo
try{
Double importo = Double.parseDouble(aValue.toString());
f.setImporto(importo);
}catch(Exception e){
VisualMessage.getErrore();
}
}
super.setValueAt(f.getDescrizione(), rigaIniziale>7 ? rigaIniziale : rowIndex, 0);
super.setValueAt(f.getImporto(), rigaIniziale>7 ? rigaIniziale : rowIndex, 1);
calcolaTotale();
}catch(Exception e){
log.logStackTrace(e);
}
}
public void calcolaTotale(){
try{
Double parziale=0.0;
Double totaleSpese=0.0;
for (FatturaRighe fattura : this.v) {
if(fattura.getSoggettoIVA()==true){
parziale += fattura.getImporto()!=null ? fattura.getImporto() : 0.00;
}else
totaleSpese += fattura.getImporto()!=null ? fattura.getImporto() : 0.00;
}
empaCL=parziale*this.percEnpaCL/100;
imponibileIva = parziale + empaCL;
iva = imponibileIva*percIva/100;
ritenutaAcconto= parziale*perRitenuta/100;
lordo = imponibileIva+iva;
netto = lordo-ritenutaAcconto;
totale = netto+ totaleSpese;
int righeSenzaTasse = getCountSpeseTaxFree();
//io so che l'ultima riga conterrà sempre il parziale
super.setValueAt(decimalFormatter.format(parziale), this.getRowCount()-1-rigaParziale-righeSenzaTasse, 1);
super.setValueAt(decimalFormatter.format(empaCL), this.getRowCount()-1-rigaEnpaCl-righeSenzaTasse, 1);
super.setValueAt(decimalFormatter.format(imponibileIva), this.getRowCount()-1-rigaImponibileIva-righeSenzaTasse, 1);
super.setValueAt(decimalFormatter.format(iva), this.getRowCount()-1-rigaIva-righeSenzaTasse, 1);
super.setValueAt(decimalFormatter.format(ritenutaAcconto), this.getRowCount()-1-rigaRitenuta-righeSenzaTasse, 1);
super.setValueAt(decimalFormatter.format(lordo), this.getRowCount()-1-rigaTotaleLordo-righeSenzaTasse, 1);
super.setValueAt(decimalFormatter.format(netto), this.getRowCount()-1-rigaNetto-righeSenzaTasse, 1);
//super.setValueAt("TOTALE", this.getRowCount()-1-rigaTotale, 0);
super.setValueAt(decimalFormatter.format(totale), this.getRowCount()-1-rigaTotale, 1);
}catch(Exception e){
log.logStackTrace(e);
VisualMessage.getErrore();
}
}
public Class<Float> getColumnClass(Float columnIndex) {
return Float.class; // Le due colonne sono numeri interi
}
@SuppressWarnings("rawtypes")
public void stampaTabella(List<FatturaRighe> v){
int i=0;
this.v=v;
Double totale=0.0;
// for(Iterator<FatturaText> it=v.iterator(); it.hasNext();){
// FatturaText fattura =it.next();
// this.addRow(new Vector());
// super.setValueAt(fattura.getCodFattura(), i,0);
// super.setValueAt(fattura.getDataFormattata(), i,1);
// if(fattura.getCliente()!=null)
// super.setValueAt(fattura.getCliente().getRagioneSociale(), i,2);
//
// super.setValueAt(decimalFormatter.format(fattura.getTotale())+" €", i, 3);
// totale+=fattura.getTotale();
// i++;
// }
this.addRow(new Vector());
super.setValueAt("TOTALE", i, 0);
super.setValueAt(decimalFormatter.format(totale)+" €", i, 3);
}
}
# 1 楼答案
一个
KeyListener
和“点击”一个按钮有什么关系当你点击一个按钮时,你应该在按钮上添加一个
ActionListener
来处理一个事件首先,您不应该使用KeyListener来侦听KeyEvents。如果你使用了一个听者,那么就不要使用幻数,没有人知道“127”是什么。而是使用
KeyEvent
API中定义的变量。例如:KeyEvent。VK_A在任何情况下,您的问题是
JTable
会在接收到任何KeyEvent时将单元格置于编辑模式如果需要对特殊的击键进行特殊处理,那么应该使用
Key Bindings
。然后按键由绑定处理,并且编辑器将不会被调用有关更多信息和示例,请阅读Swing教程中关于How to Use Key Bindings的部分。注意:在设置密钥绑定时,您将使用
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT InputMap