Swing/PropertyTable のバックアップの現在との差分(No.8)
- category: swing folder: PropertyTable title: JTableでプロパティ一覧表を作成する tags: [JTable, TableCellRenderer, TableCellEditor, JColorChooser] author: aterai pubdate: 2014-01-06T00:25:50+09:00 description: JTableの行ごとにクラスに応じたセルエディタなどを適用することで、プロパティ一覧表を作成します。 image:
概要
JTable
の行ごとにクラスに応じたセルエディタなどを適用することで、プロパティ一覧表を作成します。
Screenshot
Advertisement
サンプルコード
String[] columnNames = {"Type", "Value"};
Object[][] data = {
{"String", "text" },
{"Date", new Date() },
{"Integer", 12 },
{"Double", 3.45 },
{"Boolean", Boolean.TRUE},
{"Color", Color.RED }
};
JTable table = new JTable(data, columnNames) {
private Class editingClass;
private Class getClassAt(int row, int column) {
int mc = convertColumnIndexToModel(column);
int mr = convertRowIndexToModel(row);
return getModel().getValueAt(mr, mc).getClass();
}
#spanadd
#spanend
@Override public TableCellRenderer getCellRenderer(int row, int column) {
//editingClass = null;
// editingClass = null;
if (convertColumnIndexToModel(column) == 1) {
//System.out.println("getCellRenderer");
// System.out.println("getCellRenderer");
return getDefaultRenderer(getClassAt(row, column));
} else {
return super.getCellRenderer(row, column);
}
}
#spanadd
#spanend
@Override public TableCellEditor getCellEditor(int row, int column) {
if (convertColumnIndexToModel(column) == 1) {
//System.out.println("getCellEditor");
// System.out.println("getCellEditor");
editingClass = getClassAt(row, column);
return getDefaultEditor(editingClass);
} else {
editingClass = null;
return super.getCellEditor(row, column);
}
}
#spanadd
#spanend
// https://stackoverflow.com/questions/1464691/property-list-gui-component-in-swing
// This method is also invoked by the editor when the value in the editor
// component is saved in the TableModel. The class was saved when the
// editor was invoked so the proper class can be created.
@Override public Class getColumnClass(int column) {
//return editingClass != null ? editingClass : super.getColumnClass(column);
// return editingClass != null ? editingClass : super.getColumnClass(column);
if (convertColumnIndexToModel(column) == 1) {
//System.out.println("getColumnClass");
// System.out.println("getColumnClass");
return editingClass;
} else {
return super.getColumnClass(column);
}
}
};
table.setAutoCreateRowSorter(true);
table.setDefaultRenderer(Color.class, new ColorRenderer());
#spandel
table.setDefaultEditor(Color.class, new ColorEditor());
#spanend
#spandel
table.setDefaultEditor(Date.class, new DateEditor());
#spanend
#spanadd
table.setDefaultEditor(Color.class, new ColorEditor());
#spanend
#spanadd
table.setDefaultEditor(Date.class, new DateEditor());
#spanend
View in GitHub: Java, Kotlin解説
上記のサンプルでは、JTable#getCellRenderer(...)
、JTable#getCellEditor(...)
をオーバーライドして、実際のモデル値からクラスを取得し、そのクラスに応じて行ごとに使用するセルレンダラー、セルエディタを変更しています。
上記のサンプルでは、JTable#getCellRenderer(...)
、JTable#getCellEditor(...)
をオーバーライドして実際のモデル値からクラスを取得し、そのクラスに応じて行ごとに使用するセルレンダラー、セルエディタを変更しています。
- セルレンダラー
-
String
,Integer
,Double
,Date
,Boolean
クラスは、それぞれデフォルトのDefaultTableCellRenderer
,JTable$NumberRenderer
,JTable$DoubleRenderer
,JTable$BooleanRenderer
を使用 -
Color
クラスは、table.setDefaultRenderer(Color.class, new ColorRenderer());
で、自作のColorRenderer
を使用 -
String
クラスとDate
クラスはデフォルトのDefaultTableCellRenderer
を使用 -
Integer
クラスとDouble
クラスはデフォルトのJTable$NumberRenderer
を使用 -
Color
クラスはアイコンで色を表示するセルレンダラーを作成してTable#setDefaultRenderer(Color.class, new ColorRenderer())
で設定
-
- セルエディタ
-
String
,Integer
とDouble
,Boolean
クラスは、それぞれデフォルトのJTable$GenericEditor
,JTable$NumberEditor
,JTable$BooleanEditor
を使用 -
Date
,Color
クラスは、JTable#setDefaultEditor(Class, TableCellEditor)
で、それぞれ対応するセルエディタを用意して使用 -
String
クラスはデフォルトのJTable$GenericEditor
を使用 -
Integer
クラスとDouble
クラスはデフォルトのJTable$NumberEditor
を使用 -
Boolean
クラスはデフォルトのJTable$BooleanEditor
を使用 -
Date
クラスはJSpinner
でセルエディタを作成してJTable#setDefaultEditor(Class, TableCellEditor)
で設定
-
-
Color
クラスはJColorChooser
を開くJButton
でセルエディタを作成してJTable#setDefaultEditor(Class, TableCellEditor)
で設定
-
JTable#getColumnClass(int)
メソッドの引数は列のみなので、1
列目の場合は、JTable#getCellEditor(...)
で取得したクラスを返すようにオーバーライド -
JTable#getColumnClass(int)
メソッドの引数は列のみのため、1
列目の場合はJTable#getCellEditor(...)
で取得したクラスを返すようにオーバーライド
参考リンク
- java - Property list GUI component in Swing - Stack Overflow
- CellEditorをJSpinnerにして日付を変更
- TableDialogEditDemo
- TableDialogEditDemo - Using Swing Components: Examples (The Java™ Tutorials > Creating a GUI with Swing > Using Swing Components)