Io voglio semplicemente che se modifico la cella 0,0 devo inserire il nuovo valore nella tabella, solo che appunto il table model che mi è stato fornito (non l'ho scritto io) lavora su un result set e non su una semplice tabella, ed essendo la prima volta che lavoro sia sulle tabelle che in java in generale non so come fare ciò visto che ogni tutorial visto su internet si rifà al vettore.
PS: la funzione che hai detto tu non so se è errata o meno però non avendolo scritto io quel codice, mi è stato solo passato, e non avendomi mai dato problemi fino ad ora non penso sia quello il problema...
PPS: ho modificato il codice in questo
codice:
package adisys.server.strumenti;
import java.sql.*;
import java.util.Formatter;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import adisys.server.boundary.EditorPatologie;
import adisys.server.boundary.EditorPazienti;
import adisys.server.data.Database;
import adisys.server.data.Patologie;
import adisys.server.data.Pazienti;
import adisys.server.entity.Patologia;
import adisys.server.entity.Paziente;
/**
* @author Francesco
*
*/
public class ADISysTableModel extends AbstractTableModel implements TableModel {
ResultSet dati;
Vector<Paziente> content = new Vector<Paziente>();
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)
{
String tableName = "";
try {
tableName = dati.getMetaData().getTableName(1);
} catch (SQLException e) {
// Eccezione
e.printStackTrace();
}
if(tableName.equals("PAZIENTI"))
return true;
else
return false;
}
@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
* N.B. L'indice della prima colonna è 0, l'ultimo è numeroColonne-1.
* @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;
}
@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
System.out.println("COL:" + columnIndex);
if(columnIndex == 1)
{
row.setNome(strValue);
}
else if(columnIndex == 2)
{
row.setCognome(strValue);
}
*/
try{
dati.updateObject(columnIndex, value);
}catch (SQLException e) {
// Eccezione
e.printStackTrace();}
fireTableCellUpdated(rowIndex, columnIndex);
}
}
Ma adesso non mi da più il NULLPOINTEREXCEPTION come errore ma questo
codice:
java.sql.SQLException: attempt to assign to non-updatable column
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.notUpdatableColumn(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.checkUpdatable(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.startUpdate(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.updateObject(Unknown Source)
at adisys.server.strumenti.ADISysTableModel.setValueAt(ADISysTableModel.java:168)
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.plaf.basic.BasicTableUI$Handler.mousePressed(Unknown Source)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(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)
Caused by: org.hsqldb.HsqlException: attempt to assign to non-updatable column
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
... 45 more
In pratica appeno clicco fuori dalla cella mi resetta il valore a quello che c'era, non mi fa la modifica, però almeno non si blocca come faceva prima...