We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have seen a number of demos claiming to integrate PApplet and Swing. For example:
http://wiki.processing.org/w/Swing_JSliders
However this code does not seem to work for me. I cannot even create a JFrame with a button that pops up a PApplet. Is this something that no longer works, since the latest demo I can find is dated 2010, or is there a way to make it work? I would appreciate a pointer to any code example that you know works on Processing 2.1
Answers
You cannot use Java Swing components (e.g. JButton, JSlider etc) directly inside a Processing sketch but it is possible to create multiple windows each with its own embedded PApplet, see this post.
Processing and Java Swing use very different event handling models so they are not easy to integrate. If you want to use multiple windows with their own GUIs you might consider using the G4P library and the GUI Builder tool both can be installed from the Processing Menus.
If you will look at the above link (and there are quite a number like it) there are claims that people have been able to put a PApplet as a component in a JFrame. Of course, I can't get it to work.
I would settle for two separate windows, one with the Swing code, and the other with the PApplet. In any case, neither worked.
In case I wasn't being clear, this is not a sketch. This is using the Processing libraries in Eclipse.
OK still not quite clear exactly what you want but I created the class
MySwingApp
shown below in Eclipse.If you run it as an Application then it will open a very small window (JFrame) with a single button (JButton).
If you then click on the button it will create a new window (JFrame) which has an embedded PApplet object and a button (JButton) and when you click on the button the 5 circles are redrawn in new positions.
Notice that I have used noLoop() and redraw() to control what is drawn on the PApplet, this is needed to avoid conflict between the event handling between Swing and Processing.
Thanks, this was great. Now I see what I had to do to stop processing from fighting with swing.