3

3列と可変行のこのJTableがあります。ユーザーは任意のセルの値を編集して(セルは選択可能であるため)、Enterキーを押します。「SAVE」を押すと、すべてのセルの値を取得し、それらを1つの文字列に結合し、各セルの値をパイプ(|)で区切ります。例(Col1 | Col1 | Col1 | Col2 | Col2 | Col2 |)これまでのところ、とても良いです。

この文字列変換方法は機能しますが、ユーザーがテーブル内のセルの値を変更しても、コードはJTableのセルのOLD値を取得します。これまで私に従ってください?どうすればこれを改善できるかわかりません。必要な場合に備えて、以下のコードを示します。(私は自分のStringBufferが私が離れることを学ぶべきものであることを理解しています)

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

読んでくれてありがとう-何かアイデアはありますか?

4

1 に答える 1

2

この答えを試すことができます:

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