From: Charlie G. <cha...@gm...> - 2007-12-27 23:21:51
|
On Dec 27, 2007 12:38 PM, Eric Johnson <er...@eb...> wrote: > 1) Everything I read about Swing stresses that it is single threaded and > that almost all code dealing with Swing components must be on the event > dispatch thread. Even the code that initialises the GUI should execute > on this thread. Most of the examples in the Swing Tutorial on the Sun > website seem to use the SwingUtilities.invokeLater method to achieve > this. [I think Sun changed this around the release of Java 1.5] > However, none of the Swing examples on the Wiki do and so it seems to me > that in this case it is incorrectly done on the initial thread. > > Is there some special property of Jython that renders this unnecessary? > More likely, I think we should update the examples. > Or have I misunderstood the situation? No, Jython isn't special in this regard, so updating the examples seems like a good idea to me. > I have attempted to translate two examples from the Swing Tutorial which use > SwingWorker threads (from Java 1.6). It'd probably be good to point out https://swingworker.dev.java.net/ in the wiki entry for people that aren't running Java 6 yet. > I would welcome any comments to improve their style. > After incorporating any changes and people think it is a good idea I will > add them to the Wiki. In both examples, you're mixing tabs and spaces for indentation which makes Guido cry. I'd suggest converting everything to spaces. For these to serve as examples, it'd be good for them to import full Java classes rather than Java packages so they can run with the package scanner disabled. That just means doing "from java.lang import Runnable" instead of "import java.lang as lang" and so on for the rest of the Java imports. True and False only appeared in Python 2.3 so they're not available in the released version of Jython. As sad as it is, it'd be good to use 1 and 0 or to assign True and False if they don't exist at the start of the scripts. In Flipper.py, Flipper is a class, so you can pass Flipper directly to Runnable rather than using "lambda: Flipper()". Rather than using a class for FlipPair, I'd just pass a two element tuple of (heads, total) to publish in doInBackground. That'll be turned into an Object[] and so you can just access heads as element 0 and total as element 1 in process. If you're going to use the bean shortcuts(which seems like a good thing to me), I'd use them consistently. So frame.setContentPane in createAndShowGui should become a contentPane= in the JFrame's constructor, frame.setVisible(True) should be frame.visible = True, and so on. Other than that, they look good to me. > 2) There is a try block around the code in the doInBackground method in > both examples. If I don't put this exception handler in the code then > exceptions seems to be ignored. In particular if I change the code > from util.Random() to lang.Random() [which doesn't exist] then the > code runs but an unhanded exception is raised and this is not reported > to the console. > Is this expected? > Is there a way to run the script so that these exceptions are raised > as they are on the main thread? This is a property of SwingWorker, not Jython. When doInBackground raises an exception, the worker stops and stores the exception. If you call get on the worker after that, it'll raise an ExecutionException which you can use to get the exception that doInBackground threw by calling getCause. Charlie |