Replies: 4 comments 14 replies
-
How are you running this? What version of py5 are you using? |
Beta Was this translation helpful? Give feedback.
-
Finally found a workaround to import problematic JavaFX classes: import py5
# Class loader for problematic Java imports:
from java.lang import ClassLoader, String # type: ignore[reportMissingImports]
AppClassLoader = ClassLoader.getSystemClassLoader()
# Workaround for "from javafx.scene.control import Button":
ButtonConstructor = AppClassLoader.loadClass(
"javafx.scene.control.Button").getConstructor(String)
def setup():
py5.size(300, 200, py5.FX2D)
py5.window_title('JavaFX Button')
canvas = py5.get_surface().get_native()
pane = canvas.getParent()
pane_nodes = pane.getChildren()
btn = ButtonConstructor.newInstance('Push Me')
pane_nodes.add(btn)
py5.run_sketch() I had to resort to reflection to get JavaFX Button's constructor, so it can be instantiated later. |
Beta Was this translation helpful? Give feedback.
-
Another observation: An exception only seems to be thrown with actual controls in javafx.scene.control. Consider the following code; anything which is an actual control fails while dialog, menu, event classes get by: from javafx.scene.control import Menu
from javafx.scene.control import Dialog
from javafx.scene.control import SortEvent
from javafx.scene.control import CheckMenuItem
from javafx.scene.control import CheckBoxTreeItem
# fails
# from javafx.scene.control import ProgressIndicator
# fails
# from javafx.scene.control import Label
# fails
# from javafx.scene.control import Cell
#fails
# from javafx.scene.control import Control
#fails
# from javafx.scene.control import ListView
# fails
# from javafx.scene.control import PasswordField
#fails
# from javafx.scene.control import MenuButton
#fails
# from javafx.scene.control import ScrollBar
#fails
# from javafx.scene.control import Slider
#fails
# from javafx.scene.control import Spinner
#fails
# from javafx.scene.control import ChoiceBox
#fails
# from javafx.scene.control import Button |
Beta Was this translation helpful? Give feedback.
-
Another workaround (until JPype can be fixed, assuming that is even possible) is to call the imports from a separate function after the renderer and window has been setup. Only downside that I see is that they all have to be 'global', otherwise it's no more code but does require a new app code structure. def loadImports():
global Button,Slider,Spinner,Label, VBox, Font, FontWeight
from javafx.scene.control import Button, Label, Slider, Spinner
from javafx.scene.text import Font, FontWeight
from javafx.scene.layout import VBox
def setup():
size(500,200,FX2D)
window_title('JavaFX Controls in Default Window')
canvas = get_surface().get_native()
root = canvas.getParent()
loadImports()
btn = Button('Push Me')
slider = Slider(0,100,25)
label = Label("These controls all came from javafx.scene.control")
font = Font.font("Menlo", 15)
label.setFont(font)
spinner = Spinner(0,100,25)
spinner.setPrefSize(70, 25)
vbox = VBox()
vbox.setSpacing(30)
vbox.getChildren().addAll(label,btn,slider,spinner)
root.getChildren().add(vbox) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Any idea why this syntax works for some things and not for others?
Error message:

Beta Was this translation helpful? Give feedback.
All reactions