解
您可以通过调用与所选行对应的模型对象上的getter来检索相关字段.
在下面的代码中,newValue.getId()调用是关键.
没有Java 8 lambdas:
final Label selected = new Label();
table.getSelectionModel().selectedItemProperty().addListener(
new ChangeListener() {
@Override
public void changed(
ObservableValue extends IdentifiedName> observable,
IdentifiedName oldValue,
IdentifiedName newValue
) {
if (newValue == null) {
selected.setText("");
return;
}
selected.setText("Selected Number: " + newValue.getId());
}
}
);
使用Java 8 lambda:
final Label selected = new Label();
table.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> {
if (newValue == null) {
selected.setText("");
return;
}
selected.setText("Selected Number: " + newValue.getId());
}
);
示例代码
import javafx.application.Application;
import javafx.collections.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class TableViewSample extends Application {
private TableView table = new TableView<>();
private final ObservableList data =
FXCollections.observableArrayList(
new IdentifiedName(3, "three"),
new IdentifiedName(4, "four"),
new IdentifiedName(7, "seven"),
new IdentifiedName(8, "eight"),
new IdentifiedName(9, "nineses")
);
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
TableColumn idColumn = new TableColumn<>("No");
idColumn.setMinWidth(100);
idColumn.setCellValueFactory(
new PropertyValueFactory<>("id")
);
TableColumn nameColumn = new TableColumn<>("Name");
nameColumn.setMinWidth(100);
nameColumn.setCellValueFactory(
new PropertyValueFactory<>("name")
);
table.setItems(data);
table.getColumns().setAll(idColumn, nameColumn);
table.setPrefHeight(180);
final Label selected = new Label();
table.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null) {
selected.setText("");
return;
}
selected.setText("Selected Number: " + newValue.getId());
});
final VBox layout = new VBox(10);
layout.setPadding(new Insets(10));
layout.getChildren().addAll(table, selected);
VBox.setVgrow(table, Priority.ALWAYS);
stage.setScene(new Scene(layout));
stage.show();
}
public static class IdentifiedName {
private final int id;
private final String name;
private IdentifiedName(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
}
其他问题的答案
check my updated question so I can’t use this?
因此,在您的更新中,您可以看到每行的数据类型是ObservableList< String>,而在我的回答中,类型是IdentifiedName.要使发布的解决方案适用于您的数据类型,更改是微不足道的. newValue.getId()的等价物将是newValue.get(0),以返回列表中所选行的第一个项目.
final Label selected = new Label();
table.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> {
if (newValue == null) {
selected.setText("");
return;
}
selected.setText("Selected Number: " + newValue.get(0));
}
);
or is it possible to use identifiedname class? then how?
是的,你可以,但你必须对数据库提取代码进行大量更改,以将创建的数据加载到IdentifiedName类而不是ObservableList< String>这样做会失去数据库加载代码的通用性质.
I implement your code into my project I got … java.lang.ClassCastException:
您需要为数据类型正确设置表和列的类型,而不是我为用例提供的示例类型.
替换这些类型:
TableView
TableColumn
有这些类型:
TableView>
TableColumn, String>
小建议
我建议通过阅读Java Generics Trail进行复习.JavaFX中的表在使用泛型时非常复杂,但在表代码中使用正确的泛型可以使编写更容易(只要你使用的是一个好的IDE,它就是善于在需要时猜测泛型).
您也可能希望提供minimal, complete, tested and readable example以及此类型的未来问题(并非所有问题).构建一个可以帮助您更快地解决您的问题.此外,确保您的代码具有一致的缩进使其更容易阅读.