2013-08-19 49 views
0

更改單元格值後,我的JTable不刷新。我可以雙擊一個單元格並更改它的值,但是當我按下確定或單擊單元格外的值重置爲上一個單元格時,它不會更新表格。這是我的自定義表模型的代碼,我不知道如何更新數據庫,因爲當我更改單元格的值時,該表是從數據庫中獲取的。isCellEditable和Database不能一起工作

package adisys.server.strumenti; 

import java.sql.*; 
import java.util.Formatter; 

import javax.swing.event.TableModelEvent; 
import javax.swing.table.AbstractTableModel; 
import javax.swing.table.TableModel; 

import adisys.server.boundary.EditorPatologie; 
import adisys.server.data.Database; 
import adisys.server.data.Patologie; 
import adisys.server.entity.Patologia; 


/** 
* @author Francesco 
* 
*/ 
public class ADISysTableModel extends AbstractTableModel implements TableModel { 


    ResultSet dati; 

    public ADISysTableModel(ResultSet nuoviDati) 
    { 
     dati=nuoviDati; 

     try { 
      //Trace 
      System.out.println("- Creazione modello tabella: \""+ dati.getMetaData().getTableName(1) +"\""); 
     } catch (SQLException e1) { 
      e1.printStackTrace(); 
     } 

    } 

    @Override 
    public int getColumnCount() { 

     try { 
      return dati.getMetaData().getColumnCount(); 
     } 
     catch (SQLException e) { 
      e.printStackTrace(); 
      System.out.println("ERRORE: Calcolo del numero di colonne errato."); 
      return 0; 
     } 
    } 

    @Override 
    public int getRowCount() { 

     try { 
      //Seleziona l'ultimo elemento 
      dati.last(); 
      //Restituisce l'indice dell'elemento 
      return (dati.getRow()); 


     } catch (SQLException e) { 
      e.printStackTrace(); 
      System.out.println("ERRORE: Calcolo del numero di righe errato. (metodo getRowCount())"); 
      return 0; 
     } 
    } 

    @Override 
    public Object getValueAt(int riga, int colonna) { 

     try { 
      //Sposta il cursore alla riga desiderata (con sfasamento di 1) 
      dati.absolute(riga+1); 

      //Estrae il valore nella colonna specificata e lo restituisce (con sfasamento di 1) 
      return dati.getObject(colonna+1); 

     } catch (SQLException e) { 
      // In caso di errore restituisce un oggetto vuoto 
      e.printStackTrace(); 

      //Trace 
      System.out.println("ERRORE: Valore dell'elemento della tabella non valido."); 
      return null; 
     } 
    } 

    @Override 
    public boolean isCellEditable(int rIndex, int cIndex) 
    { 
     return true; 
    } 


    @Override 
    public String getColumnName(int col) { 
     try { 
      return dati.getMetaData().getColumnName(col+1); 
     } catch (SQLException e) { 
      // Eccezione 
      e.printStackTrace(); 
      return "?"; 
     } 
    } 

    public Integer getID(int riga) 
    { 
     //Ricerca colonna ID 
     for(int i=0; i<=getColumnCount(); i++) 
      if(getColumnName(i).equals("ID")) 
       return i; 
     return null; 
    } 


    /** 
    * Restituisce l'indice della colonna a partire dal nome della colonna ricercata 
    * <b>N.B. L'indice della prima colonna è 0, l'ultimo è numeroColonne-1.</b> 
    * @param Nome - Stringa con il nome della colonna 
    * @return -1 se la colonna non e' stata trovata, altrimenti l'indice della colonna 
    */ 
    public int getColumnIndex(String Nome) 
    { 
     for (int i=0; i<getColumnCount();i++) 
      if(getColumnName(i)==Nome) return i; 
     return -1; 
    } 


} 

如果您需要別的東西,如數據庫或表編輯器的代碼只是讓我知道;)感謝您的幫助:)

- 編輯 - 好吧,我實施setValueAt方法,但是當我在電池點擊它給了我這樣的

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at adisys.server.strumenti.ADISysTableModel.setValueAt(ADISysTableModel.java:149) 
    at javax.swing.JTable.setValueAt(Unknown Source) 
    at javax.swing.JTable.editingStopped(Unknown Source) 
    at javax.swing.AbstractCellEditor.fireEditingStopped(Unknown Source) 
    at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(Unknown Source) 
    at javax.swing.DefaultCellEditor.stopCellEditing(Unknown Source) 
    at javax.swing.JTable$GenericEditor.stopCellEditing(Unknown Source) 
    at javax.swing.DefaultCellEditor$EditorDelegate.actionPerformed(Unknown Source) 
    at javax.swing.JTextField.fireActionPerformed(Unknown Source) 
    at javax.swing.JTextField.postActionEvent(Unknown Source) 
    at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source) 
    at javax.swing.SwingUtilities.notifyAction(Unknown Source) 
    at javax.swing.JComponent.processKeyBinding(Unknown Source) 
    at javax.swing.JComponent.processKeyBindings(Unknown Source) 
    at javax.swing.JComponent.processKeyEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source) 
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source) 
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source) 
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source) 
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$200(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

我setValueAt方法的錯誤是這樣的:

Vector<Paziente> content; 

    @Override 
    public void setValueAt(Object value, int rowIndex, int columnIndex) 
    { 
     Paziente row = this.content.elementAt(rowIndex); 
     String strValue = (String)value; 
     int IDValue = (int)value; 
     if(columnIndex == 0) 
     { 
      row.setID(IDValue); 
     } 
     else if(columnIndex == 1) 
     { 
      row.setNome(strValue); 
     } 
     else if(columnIndex == 2) 
     { 
      row.setCognome(strValue); 
     } 
     fireTableCellUpdated(rowIndex, columnIndex); 
    } 

你能再幫我一下嗎? :)我對這個功能越來越瘋狂,我知道對許多人來說,沒有什麼困難,但對我來說,這是...感謝你的回答,祝你有美好的一天! :d

更新:我已經解決了這個問題,此代碼: [CODE] @Override 公共無效setValueAt(對象值,INT的rowIndex,INT columnIndex) {字符串安勤=(字符串)值; String formatoIstruzione =「UPDATE PAZIENTI SET NOME =」+「'」+ aValue +「'」+「WHERE ID =」+ rowIndex +「;」; Database.esegui(formatoIstruzione.toUpperCase()); Pianificatore.aggiornaTabelle(); }} [/ CODE]

換句話說,我啓動SQL指令更新表,然後我刷新它的視圖,它幾乎都會顯示新值。謝謝大家的幫助和你給我的靈感,我們已經得到了它,終於! * _ *

回答

2

我在您的TableModel中看不到setValueAt()的任何實施。你怎麼能期望這些事情發生沒有它?嘗試實施該方法,看看你是否做得更好。

更新:

你必須要在行149 ADISysTableModel空引用:

at adisys.server.strumenti.ADISysTableModel.setValueAt(ADISysTableModel.java:149) 

打開文本編輯器,進入該行號,並檢查對象引用。在調試器中運行以查看哪一個爲空。這將很容易解決。

看到這一行嗎?

Vector<Paziente> content; 

這將引用設置爲null,因爲你沒叫新將其設置爲一個新的參考。

其更改爲這和NPE消失:

Vector<Paziente> content = new Vector<Paziente>(); 

這可能不是你想要的,但它不會是空的。

我認爲你應該處理底層表模型,而不是這個隨機向量。你知道你的代碼,但我認爲這也不會讓你開心。

我強烈建議你把事情簡單化,並經過一個JTable的教程中,你嘗試你真正的問題之前:

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

+0

+1'AbstractTableModel'實現是空的;另請參閱此相關的[示例](http://stackoverflow.com/a/12352838/230513)。 – trashgod

+0

編輯:我已經實現了setValueAt方法,檢查它! ;) –

+0

我看不到代碼來更新數據庫。如果您期望這種行爲,您將不得不對其進行編碼。 – duffymo