2017-02-10 7 views
0

JTableとDefaultTableModelを使用するJavaデスクトップアプリケーションを構築しています。インデックスで行を削除する方法

ここでは、このテーブルのKeyEventListenerを実装しました。ですから、私のkeyBoardのCANCボタンをクリックしようとすると、選択した行を削除したいと思います。しかし、この奇妙な行動があります。

これが私の最初のテーブルです:

enter image description here

今、私はその後、私は私のキーボードのCANCELボタンを押して最後から二番目の行を選択します。これは、結果

enter image description here

あなたが見ることができるように、最後から二番目の行を削除するが、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) { 

     } 

    } 

これは、私は、この表のKeyEventListenerを実装しているのTableModel

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

さらに詳しいヘルプが必要な場合は、適切な[mcve]を投稿してください。あなたのアプリケーションには、問題を示すコードだけは興味がありません。したがって、DefaultTableModelを使用してJTableを持つフレームを作成するだけで済みます。カスタムのTableModelでこの問題を示す必要はありません。 – camickr

答えて

1

です。私は私のキーボードのCANC]ボタンをクリックしようとした場合ので、私は選択された行

を削除するどのようなボタンのKeyListenerと「クリック」とは、互いにとしなければならないのですか?

ボタンをクリックすると、イベントを処理するボタンにActionListenerを追加する必要があります。

場合(ke.getKeyCode()== 127)すべての

まずあなたがキーイベントをリッスンするのKeyListenerを使用すべきではありません。リスナーを使用してからマジックナンバーを使用しない場合、誰も "127"が何であるかは分かりません。代わりに、KeyEvent APIで定義されている変数を使用してください。例:KeyEvent.VK_A。

いずれの場合でも、KeyEventを受け取ったときにJTableが編集モードでセルを配置するという問題があります。

特別なKeyStrokeで特別な処理を行う必要がある場合は、Key Bindingsを使用する必要があります。次に、KeyStrokeはバインディングによってハンドルされ、エディタは呼び出されません。

詳細と例については、How to Use Key BindingsのSwingチュートリアルのセクションをお読みください。キーバインディングの設定時にWHEN_ANCESTOR_OF_FOCUSED_COMPONENT InputMapを使用します。

+0

ご回答いただきありがとうございますが、これを使用するための控除を教えていただけますか? – bircastri

+0

@bircastri、チュートリアルにはサンプルコードがあります。この[リンク](https://tips4java.wordpress.com/2008/10/10/key-bindings/)は、バインディングを作成するために必要な基本コードを単純に試行します。アクションを書くだけです。フォーラムには、Key Bindingsを使用した例もたくさんあります。さらに助けが必要な場合は、あなたが試したことを示す適切な[mcve]を投稿してください。クラス全体は約20行のコードになります。フレームを作成するためのいくつかの行、テーブルを作成するための別のカップル、キーバインディングのためのいくつかの行。 'MCVE'のポイントはコンセプトを学ぶことです。コンセプトをコードに適用します。 – camickr