gpt4 book ai didi

Javafx - 场景生成器 - 如何获取表的选定值

转载 作者:行者123 更新时间:2023-12-01 13:13:37 25 4
gpt4 key购买 nike

我对 Java FX 和场景构建器还很陌生。

我有一个 TableView ,其中填充了来 self 的数据库的数据,我已经开始工作了,这是代码:

public class GridPaneController implements Initializable {

// Table
@FXML
TableView<Box> table;
@FXML
TableColumn<Box, Integer> boxId;
@FXML
TableColumn<Box, String> name;
@FXML
TableColumn<Box, Integer> groesse;
@FXML
TableColumn<Box, String> einstreu;
@FXML
TableColumn<Box, Boolean> fenster;
@FXML
TableColumn<Box, String> standort;
@FXML
TableColumn<Box, Integer> tagessatz;

ObservableList<Box> data = FXCollections.observableArrayList();

@Override
public void initialize(URL location, ResourceBundle resources) {
boxId.setCellValueFactory(new PropertyValueFactory<Box, Integer>("boxId"));
name.setCellValueFactory(new PropertyValueFactory<Box, String>("name"));
groesse.setCellValueFactory(new PropertyValueFactory<Box, Integer>("groesse"));
einstreu.setCellValueFactory(new PropertyValueFactory<Box, String>("einstreu"));
fenster.setCellValueFactory(new PropertyValueFactory<Box, Boolean>("fenster"));
standort.setCellValueFactory(new PropertyValueFactory<Box, String>("standort"));
tagessatz.setCellValueFactory(new PropertyValueFactory<Box, Integer>("tagessatz"));

List<Box> boxen = serv.zeigeAlleBoxen();
for( Box b : boxen ) {
data.add(b);
}
table.setItems(data);
}

}

我现在想做的事情:

我的场景中有一个“详细信息”部分,它应该显示此表中当前选定的表条目的详细信息。我不知道如何将当前选定的表条目的值获取到我的代码中。

为了更清楚一点:在我的详细信息部分,我有两个标签:

@FXML
String label_name;

@FXML
String label_tagessatz;

当我单击特定的表条目时,label_namelabel_tagessatz 应更新为单击的表条目的值。我假设我应该定义一个在 TableView 的 onclick 事件上调用的方法,但是如何获取所选表条目的值?

我想出了更好的方法来解决这个问题:我必须将标签的文本属性绑定(bind)到选定的表元素。现在我仍然需要知道如何获得它。

最佳答案

table.getSelectionModel().selectedItemProperty().addListener(
(ObservableValue<? extends Box> ov, Box b, Box b1) -> {
label_name.textProperty().bind(b1.nameProperty());
});

您需要在 Box 类中有一个 nameProperty() 方法。

关于Javafx - 场景生成器 - 如何获取表的选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22638872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com