如果在 FXML 中指定了控制器,则无法将数据添加到表视图。如果未指定控制器,则无法绑定"onAction"



我正在学习JavaFX FXML。为了向TableView添加数据,我将控制器添加到POJO类中的FXMLLoader

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/views/my_view.fxml"));
fxmlLoader.setController(controller);
try {
VBox myView = fxmlLoader.load();    
controller.getInboxTable().setItems(getMyDisplayedItems());
//...

FXML文件的根目录有这样的定义:

<VBox fx:id="rootVBox" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">

因此,我无法在FXML文件中指定控制器。

当我在同一类中定义按钮时,我无法指定onMouseClicked因为"没有为顶级元素指定控制器"。

因此,我可以用数据填充TableView或附加操作处理程序,但不能同时填充两者。将SortedList附加到表视图并在FXML中指定onAction的正确方法是什么?

如果要使用 FXML,请尝试在那里管理视图层的所有代码。您可以通过fx:controller标记在 FXML 文件中添加控制器。查看 Oracle Docs 中的使用 FXML 创建地址簿。下面的大部分代码都来自该教程。 另一件事是,使用这种分配控制器的方式应该可以解决"没有为顶级元素指定控制器"的问题。我添加了一个按钮,导致表视图的数据被打乱。

因此,假设您的项目文件夹中有名为sample的文件夹中的所有文件:

主.java:

package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
@Override
public void start(final Stage stage) {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(Main.class.getResource("/sample/sample.fxml"));
try {
Parent root;
root = fxmlLoader.load();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Application.launch(args);
}
}

sample.fxml:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<VBox fx:id="rootVBox"
xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.Controller">
<Button text="Shuffle Data" onAction="#shuffleDataButtonClicked"/>
<TableView fx:id="tableView">
<columns>
<TableColumn text="First Name">
<cellValueFactory><PropertyValueFactory property="firstName" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Last Name">
<cellValueFactory><PropertyValueFactory property="lastName" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Email Address">
<cellValueFactory><PropertyValueFactory property="email" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</VBox>

控制器.java:

package sample;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;
import java.util.Collections;
import java.util.Vector;
public class Controller {
@FXML
private TableView<Person> tableView;
@FXML
private void initialize() {
tableView.getItems().addAll(getSomePersonData());
}
private Vector<Person> getSomePersonData() {
Person jacobSmith = new Person("Jacob", "Smith", "jacob.smith@example.com");
Person isabellaJohnson = new Person("Isabella", "Johnson", "isabella.johnson@example.com");
Person ethanWilliams = new Person("Ethan", "Williams", "ethan.williams@example.com");
Person emmaJones = new Person("Emma", "Jones", "emma.jones@example.com");
Person michaelBrown = new Person("Michael", "Brown", "michael.brown@example.com");
Vector<Person> people = new Vector<>();
people.add(jacobSmith);
people.add(isabellaJohnson);
people.add(ethanWilliams);
people.add(emmaJones);
people.add(michaelBrown);
return people;
}
@FXML
private void shuffleDataButtonClicked() {
Collections.shuffle(tableView.getItems());
}
}

人.java

package sample;
import javafx.beans.property.SimpleStringProperty;
public class Person {
private final SimpleStringProperty firstName = new SimpleStringProperty("");
private final SimpleStringProperty lastName = new SimpleStringProperty("");
private final SimpleStringProperty email = new SimpleStringProperty("");
public Person() {
this("", "", "");
}
public Person(String firstName, String lastName, String email) {
setFirstName(firstName);
setLastName(lastName);
setEmail(email);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String fName) {
lastName.set(fName);
}
public String getEmail() {
return email.get();
}
public void setEmail(String fName) {
email.set(fName);
}
}

最新更新