[英]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.