3

I have this JTable of 3 columns and of variable rows. The user can edit the value of any of the cells (because the cells are selectable) and hit enter. Once s/he presses "SAVE", I then get every cell's value, and combine them into a single string, with each cell's value separated by a pipe ( | ). eg ( Col1|Col1|Col1|Col2|Col2|Col2| ) So far, so good.

The thing is, although this string conversion method works, when the user changes the value of the cells in the table, the code still gets the OLD value of the cells of the JTable. Follow me so far? I'm not sure how I can remedy this. Here's the code below, just in case it is required. (I realize my StringBuffer is something I should learn to move away from)

int rowNumber = inventoryData.getModel().getRowCount();
StringBuffer compiledData = new StringBuffer();
Object currentData = null;
for(int i=0;i<rowNumber;i++){
    if(inventoryData.getModel().getValueAt(i, 0) != null) currentData = inventoryData.getModel().getValueAt(i, 0).toString();
    compiledData.append(currentData.toString()+"|");
    if(inventoryData.getModel().getValueAt(i, 1) != null) currentData = inventoryData.getModel().getValueAt(i, 1).toString();
    compiledData.append(currentData.toString()+"|");
    if(inventoryData.getModel().getValueAt(i, 2) != null) currentData = inventoryData.getModel().getValueAt(i, 2).toString();
    compiledData.append(currentData.toString()+"|");
    currentData = "";
}
String repPipesRemoved = compiledData.toString().replaceAll("([|])\\1+", "");

StringBuffer tempBuffer = new StringBuffer(repPipesRemoved);
tempBuffer.append("|");
String finalString = tempBuffer.toString();

Thanks for reading - got any ideas?

4

1 回答 1

2

你可以试试这个答案

table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
于 2012-08-22T14:17:20.550 回答