Как я могу заполнить JavaFX TableView, используя Jython? - PullRequest
0 голосов
/ 18 ноября 2018

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: я использую проект библиотеки javafxjython из GitHub (https://github.com/f-lima/javafx-jython) для загрузки и создания всех компонентов внутри FXML в мой класс контроллера.

...