While weâre on the subject of EDT for Swing components, do you have any insight as to why this doesnât run? Silently fails; no error messages in Thonny. There is no py5.
import jpype
import jpype.imports
jpype.startJVM()
import java
import javax
from javax.swing import *
def buildWnd():
print("Got to here.")
frame = JFrame("Hello World Swing")
print("Never gets to here.")
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setBounds(100,100,400,400)
frame.setVisible(True)
print("Done.")
SwingUtilities.invokeLater(buildWnd)
Addendum:
I think it has to have two threads: a main one and EDT for Swing (but I still donât see a window):
import jpype
import jpype.imports
jpype.startJVM()
import javax
from javax.swing import *
def buildWnd():
print("buildWnd called.")
def setup():
print("Got to here.")
frame = javax.swing.JFrame("Swing Demo")
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE)
frame.setBounds(100,100,400,400)
frame.setVisible(True)
print("frame =",frame)
print("Done.")
javax.swing.SwingUtilities.invokeLater(buildWnd)
if __name__ == '__main__':
setup()
Console output:
Got to here.
buildWnd called.
frame = javax.swing.JFrame[frame0,100,100,400x400,layout=java.awt.BorderLayout,title=Swing Demo,resizable,normal,defaultCloseOperation=EXIT_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,28,400x372,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
Done.
New Console Output: EDT called later after indenting SwingUtilities call
Got to here.
frame = javax.swing.JFrame[frame0,100,100,400x400,layout=java.awt.BorderLayout,title=Swing Demo,resizable,normal,defaultCloseOperation=EXIT_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,28,400x372,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
Done.
>>> buildWnd called.
Addendum 2:
AppHelper is your friend. This runs on a Mac in Thonny editor. @GoToLoop Can you run this on your Windows system?
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()
Output:
Console output:
buildWnd called.
frame in buildWnd = 2024-06-05 21:42:40.868 Python[40240:1751568] WARNING: Secure coding is not enabled for restorable state! Enable secure coding by implementing NSApplicationDelegate.applicationSupportsSecureRestorableState: and returning YES.
javax.swing.JFrame[frame0,100,100,400x400,invalid,hidden,layout=java.awt.BorderLayout,title=Swing EDT,resizable,normal,defaultCloseOperation=EXIT_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
btn = javax.swing.JButton[,30,60,100x24,alignmentX=0.0,alignmentY=0.5,border=com.apple.laf.AquaButtonBorder$Dynamic@457e5465,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Button,defaultCapable=true]
EDT = True
Done in buildWnd.