2012-04-16 6 views
0

JTextPaneを使用していて、受け取ったテキスト結果をStringBufferから整列させたいとします。結果を実行するスレッドは、要求されたすべての結果を含むStringを返し、後で別のスレッドでJTextPaneにデータを配置します。JtextPaneまたはStringBufferのテキストを整列する方法

結果(文字列)を取得するためのコードは次のようになります。

明らか
info.append("\n\nResult:\n"); 

    for (TResult result : results) 
     info.append(result.getID()); 

    info.append("\n"); 

    for (TResult result : results) 
     info.append(result.getApprovalDateStr+"    "); //the space is used for the alignment 

    info.append("\n"); 

    for (TResult result : results) 
     info.append(result.getState+","+result.getCity()+"     "); 

、画面上の結果は矛盾している状態/都市の長さに依存します。それをきちんと整列させるために何を使用すべきかを指摘することはできますか?これは、JTextPaneのStringBuffer以降で行うことができます。

ありがとうございました

以下は、希望する結果のスクリーンショットです。

enter image description here

+1

これは関係ありませんが、私はStringBuffer' ''オーバーSteingBuilder'を好むだろう... – fireshadow52

+2

'JTextPane'は、RTF&HTMLとしてフォーマットのドキュメントをサポートしていますが。これをHTMLテーブルとしてフォーマットすることができます。 –

答えて

3

コール

textPane.setContentType("text/html"); 

やHTMLテーブルを使用します。

info.append("<html><table>"); 

for (TResult result : results) 
    info.append("<tr><td>"+result.getID()+"</td><td>"+result.getApprovalDateStr+"</td><td>"+result.getState+","+result.getCity()+"</td></tr>"); 

info.append("</table></html>"); 

//then 
textPane.setText(info.toString()); 
+0

素晴らしいです、ありがとう! – adhg

1

ヘルパー関数を定義します。

private void log(StringBuilder buffer, String data, int numberOfSpaces) { 
    buffer.append(String.format("%" + numberOfSpaces + "s", data)); 
} 

その後:

for (TResult result : results) 
    log(info, result.getID(), 30); 

info.append("\n"); 

for (TResult result : results) 
    log(info, result.getApprovalDateStr, 30); 

info.append("\n"); 

for (TResult result : results) 
    log(info, result.getState+","+result.getCity(), 30); 
+0

これはトリックでしたが、将来のメンテナンスに柔軟性があるため、Eliasソリューションを使用することにしました。 +1。ありがとう! – adhg

関連する問題