sách gpt4 ai đã đi

java - 有没有办法根据正在编辑的行和列来更改 ComboBoxTableCell 的内容?

In lại 作者:太空宇宙 更新时间:2023-11-04 09:26:33 28 4
mua khóa gpt4 Nike

我使用的 JavaFX TableView 有 5 列(列的数据类型):任务 ID(int)、任务名称(字符串)、任务描述(字符串)、状态(字符串)和备注(字符串)。现在,我正在从具有相同列的数据库加载数据,我想做的是,当用户单击软件中的任务状态列时,列单元格更改为组合框(字符串),但组合框中显示的状态列表应该根据用户尝试编辑的所选项目(也称为任务的状态)而有所不同

我尝试在列的 OnEdit 中创建一个新的 cellFactory,并且还尝试重写更新项方法并使用 boolean 变量设置是否将图形设置为组合框

 myStatusColumn.setOnEditStart(new EventHandler<>>() {
@Ghi đè
public void handle(TableColumn.CellEditEvent event) {
thử {
dataBeingEdited = true;
Task task = myTasksTable.getSelectionModel().getSelectedItem();
statuses = Login.dbHandler.getStatuses(task.getTaskTypeID());
myStatusColumn.setCellFactory(new Callback<>, TableCell>() {
@Ghi đè
public TableCell call(TableColumn param) {
return new TableCell(){
@Ghi đè
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if(empty)
setGraphic(null);
khác
{
if(dataBeingEdited)
{
ComboBox comboBox = new ComboBox<>(statuses);
setGraphic(comboBox);
}
}
}
};
}
});
} catch (SQLException e) {
e.printStackTrace();
}

}
});

当我双击状态列时,我希望输出是一个组合框,但我没有看到组合框,当我得到时

1 Câu trả lời

到目前为止,我还没有找到直接使用 ComboBoxTableCell 类的方法,但我所做的是使用 CellFactory 重新设计了我自己的类。我所做的是在返回 CellFactory 中创建的 Cell 之前,我设置了一个 onMouseClickedListner 来检查双击,当用户双击时,我将从表中获取选定的项目,并将图形设置为一个组合框,其值取决于所选的行和单击的列然后我设置一个 onKeyPressedListener 它将更改所选的项目,然后刷新表并更新数据库

myStatusColumn.setCellFactory(new Callback<>, TableCell>() {
@Ghi đè
public TableCell call(TableColumn param) {
ComboBox box = new ComboBox<>();
TableCell cell = new TableCell() {

@Ghi đè
protected void updateItem(String item, boolean empty) {
if (empty)
setGraphic(null);
else {
setEditable(false);
setText(item);
}
}
};
cell.setOnMouseClicked(new EventHandler() {
@Ghi đè
public void handle(MouseEvent event) {
if(event.getButton().equals(MouseButton.PRIMARY))
{
Task task = myTasksTable.getSelectionModel().getSelectedItem();
if(task!=null)
box.setItems(FXCollections.observableArrayList(task.getStatuses()));
cell.setEditable(true);
}
if(event.getClickCount()==2 && cell.isEditable() ) {
box.getSelectionModel().select(0);
cell.setText(null);
cell.setGraphic(box);
}

}
});

cell.setOnKeyPressed(new EventHandler() {
@Ghi đè
public void handle(KeyEvent event) {
if(event.getCode().equals(KeyCode.ENTER))
{
thử {
TaskLog taskLog = (TaskLog) myTasksTable.getSelectionModel().getSelectedItem();
if(taskLog != null) {
taskLog.setStatues(box.getSelectionModel().getSelectedItem());
taskLog.setStatuesID(Login.dbHandler.getStatusID(taskLog.getStatues()));
System.out.println(taskLog.getStatues());
Login.dbHandler.addNewTaskLog(taskLog);
cell.setEditable(false);
myTasksTable.refresh();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
return cell;
}
});

关于java - 有没有办法根据正在编辑的行和列来更改 ComboBoxTableCell 的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57632092/

28 4 0
太空宇宙
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com