如果在 FXML 中指定了控制器,则无法向 TableView 添加数据。如果未指定控制器,则无法绑定 `onAction`

斯捷潘

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

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到 TableView 并在 FXML 中指定 onAction的正确方法是什么

普热米斯瓦夫·塔蒙

如果要使用 FXML,请尝试在那里管理所有 View 层的代码。您可以通过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);
  }
}

示例.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", "[email protected]");
    Person isabellaJohnson = new Person("Isabella", "Johnson", "[email protected]");
    Person ethanWilliams = new Person("Ethan", "Williams", "[email protected]");
    Person emmaJones = new Person("Emma", "Jones", "[email protected]");
    Person michaelBrown = new Person("Michael", "Brown", "[email protected]");

    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);
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章