jython / jython

Python for the Java Platform
https://www.jython.org
Other
1.21k stars 189 forks source link

How I can fill a JavaFX TableView using Jython? #17

Open f-lima opened 5 years ago

f-lima commented 5 years ago

I do not know if it is a issue or if something is missing at the code, but TableView is not showing the data that I pass. Follow the code:

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>

Controller:

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

Main class:

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)

The only problem with it is: the product list is not appearing at table.

The table renderizes like it has data to show but nothing is showed.

Someone knows where is the mistake?

Please, help me.

PS: I am using javafxjython library project from GitHub (https://github.com/f-lima/javafx-jython) to load and create all components inside FXML into my controller class.

jeff5 commented 5 years ago

The jython-users list would be a better place to post this. Here you address only a handful of folk developing Jython itself.

https://sourceforge.net/p/jython/mailman/jython-users/

If your problem stems from an issue with Jython that you can demonstrate in isolation, then this is the right place.

f-lima commented 5 years ago

Thanks. I will post there.

f-lima commented 5 years ago

I did the same thing in Java and it is working, then the problem is inside Jython. I just did a copy and paste of python code and adjusted the syntax to Java and I can see the results.

jeff5 commented 4 years ago

Originally raised as https://github.com/jythontools/jython issue 127.