我有一个大问题.
我尝试在TableView中将TableColumn的内容居中.
我已经尝试过在网上找到的所有内容,但实际上没有任何效果!
那里有人遇到过同样的问题吗?任何解决方案?
希望能有所帮助!
编辑:
好吧,我能够使用以下代码来使静态单元格的内容居中:
tc_customer.setCellFactory(new Callback, TableCell>() {
@Override
public TableCell call(TableColumn p) {
TableCell tc = new TableCell();
tc.setAlignment(Pos.CENTER);
tc.setText("SOMETEXT");
return tc;
}
});
但是内容应该来自数据库,我真的不知道如何从我在TABLEVIEWNAME.setItems方法中使用的ObservableList对象获取数据…
我首先使用此代码:
tc_customer.setCellValueFactory(new PropertyValueFactory("Customer"));
但是无法集中显示该内容!
有人可以帮我吗?
编辑:
感谢这个好答案,我做到了!
代码如下:
tc_customer.setCellFactory(new Callback, TableCell>() {
@Override
public TableCell call(TableColumn p) {
TableCell tc = new TableCell(){
@Override
public void updateItem(String item, boolean empty) {
if (item != null){
setText(item);
}
}
};
tc.setAlignment(Pos.CENTER);
return tc;
}
});
tc_customer.setCellValueFactory(new PropertyValueFactory("Customer"));
谢谢!!