【发布时间】:2016-10-14 16:20:54
【问题描述】:
由于缺少教程和以下问题的不完整示例,我决定写这个问题。如果这个问题的答案成为解决类似问题的有效示例,我将很高兴。
基于:JavaFX8 list bindings similar to xaml
任务
让我们使用 JavaFX 技术(使用 FXML 作为制作图形视图的技术的一部分)制作一个 GUI 应用程序,它显示 用户集合,例如,对于每个用户来说,他/她的汽车集合。让我们也使用 JavaFX 属性 和 bindig 机制来将模型(数据)与 GUI 同步。
实施
我首先为用户和汽车创建类。
User.java
package example;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class User {
public StringProperty firstName;
public StringProperty lastName;
private ObservableList<Car> cars;
public User(String firstName, String lastName, Car[] cars) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
this.cars = FXCollections.observableArrayList(cars);
}
public String getFirstName() {
return firstName.get();
}
public StringProperty firstNameProperty() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public String getLastName() {
return lastName.get();
}
public StringProperty lastNameProperty() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public ObservableList<Car> getCars() {
return cars;
}
}
Car.java
package example;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Car {
public StringProperty modelName;
public StringProperty manufacturer;
public Car(String modelName, String manufacturer) {
this.modelName = new SimpleStringProperty(modelName);
this.manufacturer = new SimpleStringProperty(manufacturer);
}
public String getModelName() {
return modelName.get();
}
public StringProperty modelNameProperty() {
return modelName;
}
public void setModelName(String modelName) {
this.modelName.set(modelName);
}
public String getManufacturer() {
return manufacturer.get();
}
public StringProperty manufacturerProperty() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer.set(manufacturer);
}
}
比我为 FXML GUI 视图 准备 Controller 以及用户样本集合。
Controller.java
package example;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class Controller {
private ObservableList<User> users = FXCollections.observableArrayList(
new User("John", "Smith", new Car[] {
new Car("LaFerrari", "Ferrari"),
new Car("FG X Falcon", "Ford")
}),
new User("Ariel", "England", new Car[] {
new Car("ATS", "Cadillac"),
new Car("Camaro", "Chevrolet"),
new Car("458 MM Speciale", "Ferrari")
}),
new User("Owen", "Finley", new Car[] {
new Car("Corsa", "Chevrolet"),
})
);
}
最后,我还包含了生成的Main.java。
package example;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Users -- example application");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
问题
挑战在于制作 FXML GUI 视图,该视图使用 JavaFX 绑定,并显示来自对应控制器的准备好的用户集合。可能使用 ListView。
我还想在 FXML 和 不在代码中 指定 ListView 项目 设计/外观 - 因为它是 GUI 设计的一部分。 .NET XAML ItemTemplate 的适当 JavaFX FXML 替代品。 ListCell呢?
这个伪代码的某种方式:
users.fxml
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Label?>
<GridPane fx:controller="example.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<ListView items="${users}">
<ListView.ItemTemplate>
<VBox>
<Label Text="${firstName}" />
<Label Text="${lastName}" Style="-fx-background-color: yellow" />
<ListView items="${cars}">
<ListView.ItemTemplate>
<HBox>
<Label Text="${manufacturer}" />
<Label Text=": " />
<Label Text="${modelName}" />
</HBox>
</ListView.ItemTemplate>
</ListView>
</VBox>
</ListView.ItemTemplate>
</ListView>
</GridPane>
【问题讨论】:
-
不幸的是,JavaFX 中没有像数据模板这样的东西。
-
“由于缺少教程和不完整的示例”。 FXML 的文档是here,基本上包含了您需要做的所有信息。此外,this site's documentation section 上还有许多其他示例。对于这个论坛,这似乎不像是on-topic 的问题。
-
@Clemens 你肯定设置了正确的属性并使用expression binding?
-
@James_D 实际上,我通过 Oracle 浏览了一些关于 ListView 设计项的示例,但这些示例使用代码。我认为“设计”应该在一个地方(FXML)解决,而不是与代码混合。所以,问题是关于 FXML 的绑定。
-
@James_D 我认为 expression_binding 不适合这个问题。