简体   繁体   中英

How to record when a JTable cell was edited

I am trying to update a JPanel based on values in a JTable so I want to know how I can record when the JTable was edited so I can update the JPanel I am using.

I Tried using a TableModelListener but the code in the listener only runs when the Frame is resized so I am stuck.

This is my code for the TableModelListener:

tableauTroncon.addTableModelListener(new TableModelListener() {
    @Override
    public void tableChanged(TableModelEvent e) {
        System.out.println("A cell was edited");
        EtapeUpdater.majEtape(etape, tableauTroncon.getData()); // updates the data used by the JPanel
        affichageEtape = new EtapePanel(etape); //Updates the JPanel
        affichageEtape.repaint(); //Repaints the JPanel
    }
});`

EDIT: I am refering to a manual update of a cell from the user. Also tableauTroncon is an instance of my custom TableModel I don't know if this can help but there is the code of my custom TableModel :

public class EtapeTableModel extends AbstractTableModel {
private List<Troncon> data;
private String[] titre; //Title of the columns

public EtapeTableModel(List<Troncon> data, String[] titre){
    this.data = data;
    this.titre = titre;
}

public String getColumnName(int col){
    return this.titre[col];
}

public int getColumnCount(){
    return this.titre.length;
}

public int getRowCount(){
    return this.data.size();
}

public Object getValueAt(int row, int col){
    Troncon troncon = data.get(row);
    switch(col)
    {
        case 0:
            return row;
        case 1:
            return troncon.getLongueur(); 
        case 2:
            return troncon.getDenivele();
        case 3:
            return troncon.isPave();
        case 4:
            return new JButton();
        default:
            throw new IllegalArgumentException();

    }
}

public void setValueAt(Object value, int row, int col){
    switch(col)
    {
        case 1:
            data.get(row).setLongueur( (double) value);
            break;
        case 2:
            data.get(row).setDenivele((double) value);
            break;
        case 3:
            data.get(row).setPave((boolean) value);
            break;
        case 4:
            break;

        default:
            throw new IllegalArgumentException();
    }

    //EtapeUpdater.majEtape(,data); 
}

public boolean isCellEditable(int row, int col)
{
    return true;
}

public Class getColumnClass(int col){
    switch(col)
    {
        case 0: return Integer.class;
        case 1:
        case 2:
            return Double.class;
        case 3: return Boolean.class;
        case 4: return JButton.class;
        default: throw new IllegalArgumentException();

    }

}

This might help.

myTable.getModel().addTableModelListener(new TableModelListener()
{
 @Override
 public void tableChanged(TableModelEvent e) 
 {
 // access the values of the model and save them to the file here
}
});

See this post

Stackoverflow post - JTable

Javacs- JTable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM