簡體   English   中英

如何記錄何時編輯JTable單元格

[英]How to record when a JTable cell was edited

我試圖基於JTable中的值更新JPanel,所以我想知道如何記錄編輯JTable的時間,以便可以更新正在使用的JPanel。

我使用TableModelListener進行了嘗試,但是偵聽器中的代碼僅在調整Frame的大小時才運行,因此我陷入了困境。

這是我的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
    }
});`

編輯:我指的是來自用戶的單元格的手動更新。 另外tableauTroncon是我的自定義TableModel的實例,我不知道這是否可以幫上忙,但是有我的自定義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();

    }

}

這可能會有所幫助。

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

看到這個帖子

Stackoverflow發布-JTable

Javacs JTable

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM