私は Swing 開発者向けのライブラリを作成中です。進行中の作業です。しかし、私はあなたが探していることをしました。このパッケージのクラスを見てください。
http://code.google.com/p/swingobjects/source/browse/#git%2FSwingObjects%2Fsrc%2Forg%2Festhete%2Fswingobjects%2Fview%2Ftable
これを使用する方法の例については、このクラスを見てください - 行番号を見てください - 70-85
http://code.google.com/p/swingobjects/source/browse/SwingObjects/src/test/CompTest.java
私はまだドキュメントを書いていません。ただし、何もフォローしていない場合は、ここにコメントしてください。
更新 - コード サンプル
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import org.aesthete.swingobjects.annotations.Column;
import org.aesthete.swingobjects.view.table.RowDataBean;
import org.aesthete.swingobjects.view.table.SwingObjTable;
import org.jdesktop.swingx.JXFrame;
public class TableDemo {
public static void main(String[] args) {
try {
//For this demo the Framework need not be initialised.. If you plan on using the entire framework, then
//its best you initialise it before working on anything...
// SwingObjectsInit.init("/swingobjects.properties", "/error.properties");
//Here's the data to show on the table
final List<Row> rows = new ArrayList<Row>();
rows.add(new Row("Data 1", "Data 2", "Yes", true));
rows.add(new Row("Data 3", "Data 4", "No", false));
//Create the swing table as below.. Provide the Row.class to say that the data in the rows
// will be from this class
final SwingObjTable<Row> table = new SwingObjTable<Row>(Row.class);
table.setData(rows);
table.setVisibleRowCount(4);
//Make any column into a combo box by calling the below method.
//A column can be automatically made into a checkbox, by defining your property in the Row class as a boolean
table.makeColumnsIntoComboBox(new String[] { "Yes", "No" }, 2);
//Initialise the frame and show it on the screen
final JXFrame frame = new JXFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new JScrollPane(table));
frame.pack();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public static class Row extends RowDataBean{
@Column(index=0,name="Column 1",editable=true)
private String column1;
@Column(index=1,name="Column 2",editable=true)
private String column2;
@Column(index=2,name="Column 3",editable=true)
private String column3;
@Column(index=3,name="Column 4",editable=true)
private boolean column4;
public Row(String column1, String column2, String column3, boolean column4) {
super();
this.column1 = column1;
this.column2 = column2;
this.column3 = column3;
this.column4 = column4;
}
public String getColumn1() {
return column1;
}
public void setColumn1(String column1) {
this.column1 = column1;
}
public String getColumn2() {
return column2;
}
public void setColumn2(String column2) {
this.column2 = column2;
}
public String getColumn3() {
return column3;
}
public void setColumn3(String column3) {
this.column3 = column3;
}
public boolean getColumn4() {
return column4;
}
public void setColumn4(boolean column4) {
this.column4 = column4;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((column1 == null) ? 0 : column1.hashCode());
result = prime * result + ((column2 == null) ? 0 : column2.hashCode());
result = prime * result + ((column3 == null) ? 0 : column3.hashCode());
result = prime * result + (column4 ? 1231 : 1237);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Row other = (Row) obj;
if (column1 == null) {
if (other.column1 != null)
return false;
} else if (!column1.equals(other.column1))
return false;
if (column2 == null) {
if (other.column2 != null)
return false;
} else if (!column2.equals(other.column2))
return false;
if (column3 == null) {
if (other.column3 != null)
return false;
} else if (!column3.equals(other.column3))
return false;
if (column4 != other.column4)
return false;
return true;
}
}
}
更新: コメントのクエリへの回答
1) テーブルは任意のコンポーネントに挿入できますよね? (JPanel、JScrollPaneなど)、
はい。テーブルは、JTable を拡張する JXTable (swingx) を拡張します。したがって、任意のコンポーネント内に配置できます。ただし、理想的には JScrollPane に配置します。
2) 列名を制御できますか? (私のアプリは複数の言語にローカライズされています)
これをありがとう。フレームワークを作り始めたとき、ローカライズについては考えていませんでした。あなたは私を正しい方向に向けました。これを実現するために、フレームワークをすばやく変更することができました。実際には簡単です:
l10n を利用するには - コーディングを開始する前に、必ず以下の行で Swing オブジェクト フレームワークを初期化してください。Locale を指定しない場合、デフォルトの Locale が適用されます。
SwingObjectsInit.init("swingobjects", "application",new Locale("fr", "FR"));
2 セットのプロパティ ファイルが必要です。
1) swingobjects - これは、swingobjects フレームワークのデフォルトを提供します。このファイルは、私のコード ベースで既に提供されています。ファイルをコピーしてクラスパスの場所に貼り付けるだけです。
2) アプリケーション - アプリケーションの GUI テキスト/メッセージを配置する必要がある場所です。必要なロケールごとに新しいアプリケーション プロパティ ファイルを作成する必要があります。
そして最後に、言う代わりに
@Column(index=0,name="Column 1",editable=true)
private String column1;
以下を使用する必要があります。
@Column(index=0,key="test.column1",editable=true)
private String column1;
名前をキーに変更します。これにより、リソース バンドルが読み込まれ、ハード コーディングされた列名ではなくプロパティ test.column1 が検索されます。
3) SwingObjTable には hashCode と equals を実装する必要がありますか?
Swing Objects Framework をそのまま使用する場合は、equals と hashcode が必要です。Swing Objects Framework を使用すると、モデル クラスの Bean に GUI 要素のデータを設定できます - Read as in MVC。フレームワークは、Bean の値が変更された場合に GUI を自動的に更新します。Bean の値が実際に変更されたかどうかを確認するには、equals メソッドを呼び出す必要があります。したがって、それをオーバーライドする必要があります。Eclipse にこれを生成させることができます。または、フレームワークで何も使用しない場合は、そこから super.equals() を呼び出すだけです。
git リポジトリをチェックアウト/クローンするだけで、TableDemo の例にアクセスできるようになります。
git clone https://writetosethu@code.google.com/p/swingobjects/