FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="335.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" stylesheets="@style.css">
<center>
<VBox BorderPane.alignment="CENTER">
<children>
<ScrollPane>
<content>
<VBox>
<children>
<TableView fx:id="tblProduct" prefHeight="270.0" prefWidth="335.0">
<columns>
<TableColumn fx:id="colProductAmount" prefWidth="64.0" text="Amount" />
<TableColumn fx:id="colProductId" prefWidth="64.0" text="ID" />
<TableColumn fx:id="colProductDescription" prefWidth="192.0" text="Description" />
</columns>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</TableView>
</children>
</VBox>
</content>
</ScrollPane>
</children>
</VBox>
</center>
</BorderPane>
控制器:
from javafxjython import (EventHandler, FXMLLoader)
from javafx.scene import Scene
from javafx.scene.layout import BorderPane
from javafx.beans.property import (SimpleIntegerProperty, SimpleStringProperty)
from javafx.collections import (FXCollections, ObservableList)
from javafx.scene.control.cell import PropertyValueFactory
from javafx.fxml import Initializable
class ProductReport():
def __init__(self, amount, id, description):
self.amount = SimpleIntegerProperty(amount)
self.id = SimpleIntegerProperty(id)
self.description = SimpleStringProperty(description)
def getAmount(self):
return self.amount.get()
def setAmount(self, value):
self.amount = SimpleIntegerProperty(value)
def amountProperty(self):
return self.amount
def getId(self):
return self.id.get()
def setId(self, value):
self.id = SimpleIntegerProperty(value)
def idProperty(self):
return self.id
def getDescription(self):
return self.description.get()
def setDescription(self, value):
self.description = SimpleStringProperty(value)
def amountDescription(self):
return self.description
class ProductController(FXMLLoader, BorderPane, Initializable):
# second class must, be the same as root in FXML file
def __init__(self):
super(ProductController, self).__init__("view/product.fxml") # FXML file to load
self.colProductAmount.setCellValueFactory(PropertyValueFactory("amount"))
self.colProductId.setCellValueFactory(PropertyValueFactory("id"))
self.colProductDescription.setCellValueFactory(PropertyValueFactory("description"))
data = FXCollections.observableArrayList()
data.add(ProductReport(1, 33, "A"))
data.add(ProductReport(1, 66, "B"))
data.add(ProductReport(1, 99, "C"))
self.tblProduct.setItems(data)
def initialize(self, location, resources):
pass
主类:
from javafxjython import (Application, EventHandler, FXMLLoader)
from javafx.scene import Scene
from controllers import ProductController
class Main(Application):
def __init__(self):
self.title = "JavaFX Test"
def start(self, stage):
stage.setTitle(self.title)
stage.setScene(Scene(ProductController(stage), 335, 400))
stage.show()
if __name__ == "__main__":
Application.launch(Main)
唯一的问题是:产品列表未显示在表格上。
该表进行渲染,就像它有要显示的数据,但什么也没有显示。
有人知道哪里出了错吗?
请帮帮我。
PS:我正在使用来自GitHub(https://github.com/f-lima/javafx-jython)的javafxjython库项目将FXML内部的所有组件加载并创建到我的控制器类中。