@FXML private javafx.scene.control.TableView<User> userTableView;
@FXML private TableColumn<User, Image> isActiveColumn;
@FXML private TableColumn<User, String> userNameColumn;
@FXML private TableColumn<User, String> taskColumn;
@FXML private TableColumn<User, String> firstNameColumn;
@FXML private TableColumn<User, String> lastNameColumn;
@FXML private TableColumn<User, String> editDeleteColumn;
public static ObservableList<User> data;
public void initialize() throws SQLException {
try {
data = FXCollections.observableArrayList();
addUsers();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void clickAddUser() throws Exception{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/addUser.fxml"));
Parent root = fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
}
//tablewiev'e kullanici eklenmesi
public void addUsers() throws SQLException {
try {
List<User> userList = Main.dbManager.getUserList();
if(userList.size() != 0){
for (int i=0; i<userList.size(); i++){
User aUser = userList.get(i);
data.add(aUser);
}
}
isActiveColumn.setMinWidth(200);
isActiveColumn.setCellFactory(new Callback<TableColumn<User, Image>, TableCell<User, Image>>() {
public TableCell<User, Image> call(TableColumn<User, Image> param) {
final ImageView imageView = new ImageView();
imageView.setFitHeight(50);
imageView.setFitWidth(50);
TableCell<User, Image> cell = new TableCell<User, Image>(){
@Override
protected void updateItem(Image item, boolean empty) {
if(item != null)
imageView.setImage(new Image("images/if_User-green_85406.png"));
}
};
cell.setGraphic(imageView);
return cell;
}
});
isActiveColumn.setCellValueFactory(new PropertyValueFactory<User, Image>("image"));
userNameColumn.setMinWidth(200);
userNameColumn.setCellValueFactory(new PropertyValueFactory<User, String>("aUsername"){
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<User, String> param) {
return new ReadOnlyObjectWrapper(param.getValue().getUsername());
}
});
taskColumn.setMinWidth(200);
taskColumn.setCellValueFactory(new PropertyValueFactory<User, String>("aTask"){
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<User, String> param) {
return new ReadOnlyObjectWrapper(param.getValue().getTask());
}
});
firstNameColumn.setMinWidth(200);
firstNameColumn.setCellValueFactory(new PropertyValueFactory<User, String>("aFirstname"){
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<User, String> param) {
return new ReadOnlyObjectWrapper(param.getValue().getFirstname());
}
});
lastNameColumn.setMinWidth(200);
lastNameColumn.setCellValueFactory(new PropertyValueFactory<User, String>("aLastname"){
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<User, String> param) {
return new ReadOnlyObjectWrapper(param.getValue().getLastname());
}
});
editDeleteColumn.setMinWidth(200);
editDeleteColumn.setCellValueFactory(new PropertyValueFactory<User, String>("editDeleteCol"));
userTableView.setItems(data);
} catch (SQLException e) {
e.printStackTrace();
}
}
public ObservableList<User> getData(){
return data;
}
}
如果是主动或被动,我想添加图像。我添加主动或被动状态布尔值。我不知道它会是怎样但在此之前我尝试添加静态图像从不调用updateItem方法。你能帮忙解决这个问题吗?
答案 0 :(得分:1)
假设你的User
类定义了一个布尔属性active
,即
public class User {
private final BooleanProperty active = new SimpleBooleanProperty(false);
public BooleanProperty activeProperty() {
return active ;
}
public final boolean isActive() {
return activeProperty().get();
}
public final void setActive(boolean active) {
activeProperty().set(active);
}
// other properties...
}
然后您应该将isActiveCol
设为TableColumn<User, Boolean>
并将其设置为:
@FXML private TableColumn<User, Boolean> isActiveColumn;
// ...
public void addUsers() {
// ...
isActiveColumn.setCellValueFactory(new PropertyValueFactory<>("active"));
isActiveColumn.setCellFactory(tc -> {
final Image activeImage = new Image(...);
final Image passiveImage = new Image(...);
TableCell<User, Boolean> cell = new TableCell<User, Boolean>() {
private ImageView imageView = new ImageView();
@Override
protected void updateItem(Boolean active, boolean empty) {
super.updateItem(active, empty);
if (empty) {
setGraphic(null);
} else {
if (active) {
imageView.setImage(activeImage);
} else {
imageView.setImage(passiveImage);
}
setGraphic(imageView);
}
}
};
return cell ;
});
}