-
Anybody know how to fix this; it's their code: import jpype
import jpype.imports
jpype.startJVM()
import java
import javax
from javax.swing import *
def createAndShowGUI():
print("Got to here.")
frame = JFrame("HelloWorldSwing")
print("Never gets to here.")
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setBounds(100, 100, 400, 400)
label = JLabel("Hello World")
frame.getContentPane().add(label)
frame.pack()
frame.setVisible(True)
# Start an event loop thread to handling gui events
@jpype.JImplements(java.lang.Runnable)
class Launch:
@jpype.JOverride
def run(self):
createAndShowGUI()
javax.swing.SwingUtilities.invokeLater(Launch()) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 11 replies
-
Cheers @vsquared ! I'm the one who insisted you should try to learn how to make the javax GUI stuff work with "pure jpype", and I really think finding out a way of making this work is important, but I'm afraid this is not the best discussion forum for you to find help with this issue! Here, you are the most knowledgeable person on |
Beta Was this translation helpful? Give feedback.
-
AppHelper is the key to running this on a Mac: import jpype
import jpype.imports
jpype.startJVM()
import java
import javax
from javax.swing import *
from PyObjCTools import AppHelper
def buildWnd():
print("buildWnd called.")
print("frame in buildWnd =",frame)
btn = javax.swing.JButton("Button")
btn.setBounds(30,60,100,24)
frame.add(btn)
frame.setVisible(True)
print("btn =",btn)
print("EDT =",javax.swing.SwingUtilities.isEventDispatchThread())
print("Done in buildWnd.")
def main():
global frame
frame = javax.swing.JFrame("Swing EDT")
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE)
frame.setBounds(100,100,400,400)
frame.setLayout(None)
javax.swing.SwingUtilities.invokeLater(buildWnd)
AppHelper.runEventLoop()
main() Addendum: import jpype
import jpype.imports
jpype.startJVM()
import java
import javax
from javax.swing import *
from Cocoa import NSApp
def createAndShowGUI():
frame = JFrame("HelloWorldSwing")
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
label = JLabel("Hello World")
frame.getContentPane().add(label)
frame.pack()
frame.setVisible(True)
javax.swing.SwingUtilities.invokeLater(createAndShowGUI)
NSApp.run() The key to running this on a Mac is the last line of code which comes from the Cocoa framework, made possible by the PyObjc plugin to Thonny. Addendum2: import jpype
import jpype.imports
jpype.startJVM()
import java
import javax
import platform
from javax.swing import *
if(platform.system() == "Darwin"):
from Cocoa import NSApp
def createAndShowGUI():
frame = JFrame("HelloWorldSwing")
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
label = JLabel("Hello World")
frame.getContentPane().add(label)
frame.pack()
frame.setVisible(True)
javax.swing.SwingUtilities.invokeLater(createAndShowGUI)
print(platform.system())
if(platform.system() == "Darwin"):
NSApp.run() |
Beta Was this translation helpful? Give feedback.
-
@villares This is the only other demo that needs testing. I think it works cross-platform, but the author of JPype is not happy with it because he wants to start the main event loop up at the top and that is just not the 'Apple way' in my experience. |
Beta Was this translation helpful? Give feedback.
AppHelper is the key to running this on a Mac: