gpt4 book ai didi

JavaFX setCellValueFactory

转载 作者:行者123 更新时间:2023-11-30 06:09:18 24 4
gpt4 key购买 nike

我知道,这个问题已经被问过数百次了,我花了 3 个小时阅读了所有这些问题。我正在使用 JavaFX 编写一个小应用程序(我之前已经这样做过......)。除了将内容放入我的专栏之外,一切正常:

@FXML public void initialize() {
exerciseColumn.setCellValueFactory(new PropertyValueFactory<Exercise, String>("name"));
}

这是我的初始化方法:

public StringProperty nameProperty() {
return name;
}

是“StringProperty name”的属性 getter 。真正有趣的是,该代码是我不久前制作的另一个项目的精确副本(当然,我对所有内容进行了两次和三次检查),我今天再次编译了它,并且运行良好。我读了一半 JavaFX 文档来寻找解决方案,但它没有任何意义。如果您认为需要更多代码来帮助我,那么我当然可以提供。

编辑:具有相同问题的另一个示例(至少我希望如此)

主要:

package test;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import test.model.Exercise;
import test.view.Controller;

public class Main extends Application {

private Stage primaryStage;
private AnchorPane pane;

private ObservableList<Exercise> activeSession = FXCollections.observableArrayList();

public Main() {
activeSession.add(new Exercise("ednhzrd"));
}

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Test");

try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/view.fxml"));
pane = (AnchorPane) loader.load();
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();

Controller controller = new Controller();
controller.setMainApp(this);
} catch(Exception e) {
e.printStackTrace();
}
}
}

Controller :

package test.view;

import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import test.Main;
import test.model.Exercise;

public class Controller {
@FXML private TableView<Exercise> exerciseTable;
@FXML private TableColumn<Exercise, String> exerciseColumn;

private Main main;

public Controller() {}

@FXML
public void initialize() {
exerciseColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
}

public void setMainApp(Main main) {
this.main = main;
}
}

练习:

enter codepackage test.model;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Exercise {

private StringProperty name;

public Exercise(String name) {
this.name = new SimpleStringProperty(name);
}

public StringProperty nameProperty() {
return name;
}
}

fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane prefHeight="400.0" prefWidth="209.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.view.Controller">
<children>
<TableView fx:id="exerciseTable" layoutX="200.0" layoutY="100.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="exerciseColumn" prefWidth="75.0" text="C1" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
</AnchorPane>

最佳答案

您的cellValueFactory没有问题。这里唯一的问题是你从来没有在表中放入任何项目。如果修改 Controller 以向表中添加测试项:

package test.view;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import test.Main;
import test.model.Exercise;

public class Controller {
@FXML
private TableView<Exercise> exerciseTable;
@FXML
private TableColumn<Exercise, String> exerciseColumn;

private ObservableList<Exercise> activeSession = FXCollections.observableArrayList();

private Main main;

public Controller() {
}

@FXML
public void initialize() {
exerciseColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
activeSession.add(new Exercise("hrrykane"));
exerciseTable.setItems(activeSession);
}

public void setMainApp(Main main) {
this.main = main;
}
}

然后你就看到了想要的效果:

enter image description here

关于JavaFX setCellValueFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50612023/

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