[英]Add String row to TableView in JAVA FX
我找不到将简单的“字符串”行添加到tableView的方法。 实际上,我可以添加一行,但是其内容不可见 ...
这是我的代码:
@FXML
private TableView<String> table;
@FXML
private TableColumn<String, String> table2;
public ObservableList<String> getLanes()
{
ObservableList<String> lanes=FXCollections.observableArrayList();
lanes.add("TEST");
return lanes;
}
然后:
table.setItems(getLanes()); //Not working
和
table.getItems().add("TEST"); //Not working
但是没有成功。 我阅读了 该内容以及其他文档,但是它并没有帮助我以这种简单的方式进行操作。
编辑:
添加此行解决了我的问题:
table2.setCellValueFactory(param -> new ReadOnlyStringWrapper(param.getValue()));
这是一个简单的应用程序,我们试图将单个值加载到TableView列中。 它还显示了如何在表列上设置cellValueFactory()
。
tableColumn.setCellValueFactory(param -> new ReadOnlyStringWrapper(param.getValue()));
MCVE
import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
TableView<String> tableView = new TableView<>();
TableColumn<String, String> tableColumn = new TableColumn<>("Name");
tableColumn.setCellValueFactory(param -> new ReadOnlyStringWrapper(param.getValue()));
tableView.getColumns().add(tableColumn);
ObservableList<String> items = FXCollections.observableArrayList("Itachi");
tableView.setItems(items);
VBox root = new VBox(tableView);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 300, 275);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.