0

My JTable doesn't refresh after I change a cell value. I can double click on a cell and change it's value but when I press OK or I click out of the cell the value's reset to the previous one, it's not update the table. Here's my code for my custom table model, I don't know how to update the database, because the table is taken from a db, when I change the cell's value.

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;
    }


}

If you need something else, like the codes for the database or for the table editor just let me know ;) Thanks for your help :)

--EDIT-- Alright i've implemented the setValueAt method but when i click on the cell it gives me errors like this

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)

my setValueAt method is this:

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

Would you please help me again on this? :) I'm getting crazy about this function, i know that for many is nothing difficult but for me it is... Thanks for answers, have a nice day! :D

UPDATE: i have solved this problem with this code: [CODE] @Override public void setValueAt(Object value, int rowIndex, int columnIndex) {String aValue = (String)value; String formatoIstruzione= "UPDATE PAZIENTI SET NOME = " + "'" + aValue + "'" + " WHERE ID = " + rowIndex + ";"; Database.esegui(formatoIstruzione.toUpperCase()); Pianificatore.aggiornaTabelle(); } } [/CODE]

In other words i launch the sql instruction to update the table and then i refresh it's view so it's almost show the new value. Thank you everybody for the help and the inspiration you gave me, we've got it, finally!! *_*

Jimi
  • 1,605
  • 1
  • 16
  • 33

1 Answers1

2

I don't see any implementation for setValueAt() in your TableModel. How can you expect either of those things to happen without it? Try implementing that method and see if you do better.

Update:

You have a null reference at line 149 in ADISysTableModel:

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

Open a text editor, go to that line number, and inspect the object references. Run in a debugger to see which one is null. It'll be easy to fix.

See this line?

Vector<Paziente> content;

That sets the reference to null because you didn't call new to set it to a new reference.

Change it to this and the NPE goes away:

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

This might not be what you want, but it won't be null.

I think you should be dealing with the underlying table model, not this random vector. You know your code, but I don't think this is going to make you happy, either.

I'd strongly urge you to simplify things and go through a JTable tutorial before you attempt your real problem:

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

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • +1 The implementation in `AbstractTableModel` is empty; see also this related [example](http://stackoverflow.com/a/12352838/230513). – trashgod Aug 19 '13 at 14:59
  • EDIT: I have implemented the setValueAt method, check it up! ;) – Jimi Aug 20 '13 at 09:56
  • I see no code here to update a database. You'll have to code it if you expect that kind of behavior. – duffymo Aug 20 '13 at 11:20
  • the methods setID, setNome and setCognome updates the db, infact they're used for adding data in the table or when i click a button to edit all the line, but for some reason they don't work when i want to edit a single cell... – Jimi Aug 20 '13 at 14:02
  • They don't work because you keep thinking you coded them in, but you did not. There's no magic here. – duffymo Aug 20 '13 at 14:13
  • I saw with the debugger that the error is this: Paziente row = this.content.elementAt(rowIndex); So maybe will i'll not use the vector to take the element? Because he calls both the methods setValueAt from ADISys and from JTable, the second cause the errore becuase can't find anything, no value... – Jimi Aug 20 '13 at 15:50
  • this tells me that the content member variable is null. – duffymo Aug 20 '13 at 15:52
  • yes, but why? I mean, the value, rowIndex and columnIndex are taken properly, so i dont' understand what's wrong with it? – Jimi Aug 20 '13 at 16:13
  • You didn't initialize it to be non-null. Look where it's declared. You didn't call new to set it to a non-null reference. None of the rest of that stuff you cited matters if the reference is null. – duffymo Aug 20 '13 at 16:44
  • so content must take the value of the entire row of the table? – Jimi Aug 20 '13 at 16:53
  • I didn't say that. I said content cannot be null or you'll get a null pointer exception when you dereference it. – duffymo Aug 20 '13 at 16:54
  • so at how i will initialize that vector? becuase if i use the default getValueAt method to fill the vector i have tons of errors.... I'm sorry if i'm boring you with this but it's my first time i make a program in java so i don't know much about how it works and yes, my teacher is an asshole, he doesn't teach us anything about that... – Jimi Aug 20 '13 at 17:00
  • 1
    Your teacher's not an asshole. You need to be a better student. Spend some time with tutorials and figure it out. http://docs.oracle.com/javase/tutorial/uiswing/components/table.html – duffymo Aug 20 '13 at 17:02
  • no trust me, he's an asshole, in all his slides he use to teach us there's nothing about java in general, it's only uml... – Jimi Aug 20 '13 at 17:03
  • 1
    Maybe he's expecting students to fill in the blanks. I'm always suspicious of students bad mouthing teachers. More often than not it's the student with the problem. I know from personal experience. I cringe to recall what I said about one particular instructor I had. I was the problem, not him. – duffymo Aug 20 '13 at 17:07
  • ok but this is our first exam of java of ever, can he give us an easier project or perhaps make this point optionally? we have only 1 months to do that and this is only the first one! – Jimi Aug 20 '13 at 17:17
  • Go look at the tutorial. You'll find it easier if you stop complaining and make more effort to understand. – duffymo Aug 20 '13 at 17:19
  • mmh... from that example i see that the table and alos the table model are in one file, and the table is already written, but i have two different files, one for the table and one for the model, and the file where is the table make other things, 'cause the table is set into a frame, so i can't follow that example, i've tried to do it, put the same things where he wants, but it doesn't work :( – Jimi Aug 20 '13 at 17:32
  • ahahahahahahah nice tip, but i can't stop, i think i will let this point as the last one ;) – Jimi Aug 20 '13 at 19:50