這是我用JTableBinding實現的類。列寬設置很好。
public class TMZeit extends JTable {
private static final long serialVersionUID = 8639962215280391425L;
private List<Zeit> list;
private ResourceBundle bundle;
private Object[][] columns;
public TMZeit(ResourceBundle bundle) {
this.list = ObservableCollections.observableList(new ArrayList<Zeit>());
this.bundle = bundle;
this._initComponents();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void _initComponents() {
this._initColumns();
JTableBinding<Zeit, List<Zeit>, JTable> jTableBinding = SwingBindings
.createJTableBinding(AutoBinding.UpdateStrategy.READ,
this.list, this);
for (int i = 0; i < this.columns.length; ++i) {
ELProperty<Zeit, String> property = ELProperty.create(String
.format("${%s}", columns[i][1]));
ColumnBinding columnBinding = jTableBinding
.addColumnBinding(property);
columnBinding.setColumnClass((Class<?>) this.columns[i][2]);
columnBinding.setColumnName((String) this.columns[i][0]);
columnBinding.setEditable(false);
}
jTableBinding.bind();
for (int i = 0; i < this.columns.length; ++i) {
this.getColumnModel().getColumn(i)
.setPreferredWidth((Integer) this.columns[i][3]);
}
}
private void _initColumns() {
this.columns = new Object[][] {
{ this.bundle.getString("table.column.serno.tooltip"), "serno",
Integer.class, 30 },
{ this.bundle.getString("table.column.project.tooltip"),
"project", String.class, 230 },
{ this.bundle.getString("table.column.activity.tooltip"),
"activity", String.class, 230 },
{ this.bundle.getString("table.column.time.tooltip"), "time",
Integer.class, 50 } };
}
...
}
對不起,格式不正確。
您將使用您的自定義列寬創建表結構,然後相應的對象將綁定jtablebinding嘗試 – Sathiyaraj
它不起作用。看起來JTableBinding在JTable中看不到更改,即使我在綁定之前和之後創建它們。 :( – user3468526
'我注意到JtableBinding沒有一個TableColumnModel' - 我從來沒有使用過JTableBinding,但看起來你仍然需要一個JTable,所以你改變了由JTable創建的TableColumnModel的TableColumns,這必須在你之後完成已設置JTable的TableModel – camickr