We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › [Pretty much solved] Swing in P5
Page Index Toggle Pages: 1
[Pretty much solved] Swing in P5 (Read 3250 times)
[Pretty much solved] Swing in P5
Sep 4th, 2009, 12:11am
 
I managed to learn how to make a new JFrame based (of course) on the Java Swing Toolkit from inside a Processing sketch...

What I do not know yet (because I have not reached to that point) is if the JFrame and the P5 sketch share the same variables I may create in the begining of the sketch, before the setup().

There is another thing going on in the program:

It contains three dummy buttons, created as the Java docs say they should be created, but no matter what method I use to put them in the JFrame, the last of them I add to the JFrame takes all the space within it and leaves the other two hidden until I click where they are suposed to be. This problem is not in P5 because I isolated the JFrame code, put it in Eclipse, hit run, and the same happened... So I assume the error is in the code not in the dev environment.

So here are my three questions:

1 - Will the JFrame and the frame of the sketch share the same set of global variables when I implement an event listener for the JFrame's events? (This is to be able to control things in the skecth display from the JFrame control panel of course...)

2 - What am I doing wrong in the coding of the JFrame interface, why does the last controller takes all the space available?

3 - And, more importantly: Is this approach (Swing within P5) a good way into implementing a lightweight-separate-window control interface for my program? I read around that this is not the best way to do so. But I just proved I can make the Interface from within P5, so I don't know if this is a valid trustable way for what I aim at...

I used ControlP5 but it eats some amounts of processor that I consider better to be spent in the main sketch's graphics display rather than the control interface...

Here is my dummy test code for the app.

[[[ EDIT: THE FOLLOWING EXAMPLE CODE SUCKS, THE SOLUTION IS IN THE FIFTH ANSWER TO THIS THREAD ]]]

Quote:
import javax.swing.*;

public class swingUIDev extends PApplet {

  void setup() {  
    size(300,300);
    createAndShowGUI();
    frame.setTitle("Swing UI Test");
  }

  public void draw() {
    background(0);
    stroke(255);
    strokeWeight(10);
    // just to make sure things happen in the main window
    line(random(width),random(height),random(width),random(height));
  }

  static public void main(String[] args) {
    PApplet.main(new String[] {
      "swingUIDev"                         }
    );
  }

  static public void createAndShowGUI() {
    JFrame controlPanel = new JFrame("sw");
    controlPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    controlPanel.setSize(200,300);
    controlPanel.setLocation(100,100);
    JMenuBar menuBar = new JMenuBar();
    JMenu head = new JMenu("File");
    JMenu subMenu = new JMenu("sum menu");
    JMenuItem subMenuItem1 = new JMenuItem("menu item1");
    JMenuItem subMenuItem2 = new JMenuItem("menu item2");
    
    controlPanel.setJMenuBar(menuBar);
    menuBar.add(head);
    head.add(subMenu);
    head.add(subMenuItem1);
    subMenu.add(subMenuItem2);
    
    JButton button1 = new JButton("button 1");
    button1.setSize(80,20);
    button1.setLocation(20,20);

    JButton button2 = new JButton("button 2");
    button2.setSize(80,20);
    button2.setLocation(50,50);

    JButton button3 = new JButton("button 3");
    button3.setSize(80,20);
    button3.setLocation(10,10);
    //    use this lines to add the buttons directly to
    //      the Control Panel's Content Pane
    //    controlPanel.getContentPane().add(button1);
    //    controlPanel.getContentPane().add(button2);
    //    controlPanel.getContentPane().add(button3);

    GroupLayout layout = new GroupLayout(controlPanel);
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);
    
    layout.setVerticalGroup(
      layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        .addComponent(button1)
        .addComponent(button2)
        .addComponent(button3)
    );
    
    // use the following line to wrap up the
    //   controlPanel window.
    // controlPanel.pack();
    controlPanel.setVisible(true);
  }
}



thank you very much for any help Smiley
Re: Swing in P5
Reply #1 - Sep 4th, 2009, 1:15am
 
I haven't tried to mix Processing and Swing, partly because of the warning I report in Problem with dialog box.
There was a library allowing to use AWT components in a sketch (SpringGUI), but it was removed from the list of libraries because it is reported as incompatible with Processing 1.0.
Likewise, Swing & Processing using a different threading system, you might hit issues.
Note there are alternative libraries of GUI, although I don't know if they are as complete as controlP5.
Re: Swing in P5
Reply #2 - Sep 4th, 2009, 11:35am
 
Yes I read that topic, but I have to tell that I used the same approach in APL 0.0.0.5 and it was working fine. the only thing was that right before the JOptionPane there was a noLoop() command, so the sketch stopped while the popup was open. After the JOptionPane there was a loop() command, so when the opoup was shut the sketch began working again.

Anyway, I'm not using Swing stuff inside the sketch itself, I justinitalize it in the setup() and then use the draw() as usual. But, since I have not implemented an event listener, because I'm trying to fix the button bad coding problem, I still do not know if the Swing UI and the sketch will share the same set of variables (but by the way I constructed it I supose they do).

Another thing I'm trying too, is to have a normal java app programmed in Eclipse, in it's main() it calls the processing sketch in an adjacent .java file, which, I think, is a much better approach. But with this technique the question remains: will the Eclipse java app, calling the sketch, and the sketch, share the same set of variables? if not, how can I do so?
Re: Swing in P5
Reply #3 - Sep 4th, 2009, 12:33pm
 
Ah, the noLoop() trick is nice!
I am probably too dense tonight, but I don't see why you can't share data between your class using Swing and the Processing side.
Re: Swing in P5
Reply #4 - Sep 4th, 2009, 2:47pm
 
Using that mode of a Swing app calling a P5 sketch I think I solved the variable access problem.

Since the P5 sketch is in another class you tell the Swing app to access the public variables. If your P5 class is called, say, "mySketch", and it has a int variable called "number", then in the Swing app you tell it to access the number variable in the sketch like this: mySketch.number = 23; or something like that, those accesses should be placed in the Swing components event handling code, I think. I have not tested this in P5, because the Swing elements are inside the setup() on the other hand The propposed approach is being executed in eclipse and NetBeans in which it works perfectly.

And about the GUI problems, I chose to build the gui using NetBeans' Built-in GUI Builder... It's kind of a lazy solution but the GUI looks perfect Smiley

So I think I solved things up. But I still do not know if there is a more sophisticated way to this Tongue

If you know about something better than this approach, posting it will be very welcome. Smiley
[This is the answer]: Swing in P5
Reply #5 - Sep 4th, 2009, 5:33pm
 
I finally made it!

It is in deed posible to create a Swing GUI from inside Processing!
It takes quite a while if you do not know all methods for GUI interaction but it is possible. I created this GUI in NetBeans' GUI Builder (lame solution I know... ) But then I managed to adapt it to the P5 context.

The code is bigger than supported by the forum so you can catch it here:

http://0p0media.piensalibre.org/swingUIDev.zip

The zip contains the project folder with a single pde file. Just open it in P5 and run. The result is almost the same sketch in the begining of this thread above, but this time the GUI loads a little slider that controls the position of the first point of the random line drawed on screen Smiley

I also made a NetBeans working example, but with the other approach of telling the Swing App to access the sketch's inner variables by declaring the class and then the variable in this way: Class.variable = value;

So I answered the questions about shared variables and the incorrect GUI building code. I do believe though There's no problem with making the Swing UI in this fashion. The app executed just well and also exported perfectly, the exported app also ran perfectly...

So what do you guys think of this? Smiley

I'd like to get some feedback on other platforms diferent from Windows, Swing is supposed to be cross platform, but I do not know if this approach applies to all three Processing's supported platforms.
Re: [Pretty much solved] Swing in P5
Reply #6 - Sep 9th, 2009, 6:49pm
 
Wow, thank you very much for this, it is going to be very useful for a project I'm working on. I can confirm this works in OS X (10.6).
Re: [Pretty much solved] Swing in P5
Reply #7 - Sep 10th, 2009, 1:16am
 
Excellent! Thanks! Cheesy
Re: [Pretty much solved] Swing in P5
Reply #8 - Sep 10th, 2009, 1:50am
 
Sounds neat, but it seems to depend on GroupLayout (Java 6).  In 1.5 I get an error at:

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

Cannot find a class or type named "javax.swing.GroupLayout"

But it seems it can be grafted into 1.5, so I downloaded swing-layout-1.0.1.jar here:

http://www.findjar.com/jar/net.java.dev.swing-layout/jars/swing-layout-1.0.1.jar...

renamed it "swing.jar" and dropped it in ~/sketchbook/libraries/swing/library/swing.jar

Edit: correct import seems to be:
import org.jdesktop.layout.*;
(forgot about the "import library" menu item :P)
but I'm getting the same error, so it doesn't seem to be trying the GroupLayout from that .jar...

update: replaced all instances of javax.swing.GroupUpdate to reflect the GroupUpdate in org.jdesktop.layout, but it seems to be missing many of the attributes that are being set.  Confusing.

--Ben
Re: [Pretty much solved] Swing in P5
Reply #9 - Sep 10th, 2009, 8:04pm
 
I've managed to fit in a JTextField and that has been working quite well.

I was wondering if anyone else had tried adding in additional classes? I've been getting a "Cannot parse error text: File /var/folders/7W/7Wqt2DqtEqOfBRl5zbfK6++++TI/-Tmp-/build3654922029925722397.tmp/S
ymbol.java is missing, where Symbol is the name of the file I'm trying to add in a new tab.

Additionally if I add a class outside swingUIDev I get the same error.

I'm guessing this has something to do with the fact that PApplet is being extended and something strange is happening to the paths, or where it's looking for libraries, when it goes to compile. Does anyone have any ideas? Perhaps switching over to eclipse is necessary?
Re: [Pretty much solved] Swing in P5
Reply #10 - Sep 11th, 2009, 11:19pm
 
@R
I had no trouble adding new classes to swingUIDev... I do not know what could be the problem...

But just in case:

Do not run this example in Java mode. if you wrote your own main() method, erase it, don't use it. It is not necessary. if you wrote the class swingUIDev beginig with.

Code:
public class swingUIDev extends PApplet {
}


Did I write it in Java mode?  Huh I'm sorry, nevermind, erase that java mode code Smiley

If you added a new class in .java format be careful to add the import statement before the class definition.

Code:
import processing.core.PApplet; 



I do not recommend the use of Java mode in processing, really, it is not necessary, unless you are working in Eclipse or other IDE, or need a super duper advanced Java feature.


@BenHem
How about using Java 6? Tongue
Re: [Pretty much solved] Swing in P5
Reply #11 - Sep 12th, 2009, 2:40am
 
0p0 wrote on Sep 11th, 2009, 11:19pm:
How about using Java 6 Tongue
Not an option for lot of Mac users yet.
Re: [Pretty much solved] Swing in P5
Reply #12 - Sep 13th, 2009, 2:22am
 
Well... I think we'll have to wait till the day...
Re: [Pretty much solved] Swing in P5
Reply #13 - Nov 19th, 2009, 7:45am
 
0p0, I tried to download your file but the url is dead.
Page Index Toggle Pages: 1