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 & HelpIntegration › Processing and Swing
Page Index Toggle Pages: 1
Processing and Swing (Read 1876 times)
Processing and Swing
Apr 30th, 2006, 1:07am
 
I have been able to use Swing components in a JFrame but not in the PApplet. Can I use Swing sliders, scollers, buttons etc ... in PApplet? If so how?

If not, can I write with processing and output to a JFrame?

Thanks,

4dplane
Re: Processing and Swing
Reply #1 - May 1st, 2006, 1:35pm
 
this has been covered many times. type "swing papplet" in the search box and you should find it.
Re: Processing and Swing
Reply #2 - May 2nd, 2006, 1:55am
 
Thanks Fry - I always search before I post, but I could not find a clean answer so I asked the question again. Next time I will post on the same thread.

I have had some succsess with a swing slider but its a bit buggy.

Code:
import javax.swing.JSlider;
import processing.core.PApplet;

public class test4 extends PApplet{
private static final long serialVersionUID = 1L;
public void setup() {
size(400,300);
JSlider slider = new JSlider();
slider.setMinorTickSpacing(5);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.setToolTipText("The value is 50");
add(slider);
print(slider.getValue());
}
}


note*
You have to resize the window to bring the slider into view.
Running in opengl renders a black screen.
Re: Processing and Swing
Reply #3 - May 2nd, 2006, 2:49am
 
you can't use swing (or awt) components inside a papplet. you have to make a separate java project that embeds papplet as a component into a swing layout. this is covered in my comments on these posts:

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1143760874
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1131438463
Re: Processing and Swing
Reply #4 - May 4th, 2006, 5:33am
 
Has anyone ever made this work? I have been trying to no avail!

Here is what I have so far...

Code:
import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;

public class test3 extends JFrame {
pillar p1;
public static void main(String[] args) {
new test3();
}
public test3() {
super("Using JFrame");
WindowUtilities.setNativeLookAndFeel();
addWindowListener(new ExitListener());
Container content = getContentPane();

p1 = new pillar();
content.add(p1, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}


The pillar class that is called:

Code:
import processing.core.PApplet;

public class pillar extends PApplet{
public pillar(){
//size(1280, 900);
}
}


This will render without errors but as soon as I open up size (1280, 900) I get this error:

Exception in thread "main" java.lang.NullPointerException at processing.core.PApplet.updateSize(PApplet.java:912)

at processing.core.PApplet.size(PApplet.java:862)

at processing.core.PApplet.size(PApplet.java:792)

at processing.core.PApplet.size(PApplet.java:774)

at pillar.<init>(pillar.java:5)

at test3.<init>(test3.java:26)

at test3.main(test3.java:12)

I have tried as many variations of this code as I can think of and nothing works - can someone show me how this should be done?

Thanks,

4dplane
Re: Processing and Swing
Reply #5 - May 4th, 2006, 5:00pm
 
call p1.init() to initialize the applet before calling pack() or any of that stuff.
Re: Processing and Swing
Reply #6 - May 4th, 2006, 10:54pm
 
Fantastic - thanks Fry!

Now can this also be done with OPENGL? When I run the code without OPENGL it works every time but when I add OPENGL in the setup() the code executes without errors but there is no visible jframe; there is however, a thread created in my task manager. To add more complexity, sometimes a jframe is produced and runs perfectly.

Here is what I have:

Code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JSlider;

public class Frame extends JFrame {
pillar p1;
public static void main(String[] args) {

new Frame();
}
public Frame() {
super("Using JSlider");
WindowUtilities.setNativeLookAndFeel();
addWindowListener(new ExitListener());
Container content = getContentPane();
content.setBackground(Color.white);

p1 = new pillar();
p1.init();
content.add(p1, BorderLayout.SOUTH);

JSlider slider1 = new JSlider();
slider1.setBorder(BorderFactory.createTitledBorder("JSlider without Tick Marks"));
content.add(slider1, BorderLayout.NORTH);

pack();
setVisible(true);
}
}


Code:

import processing.core.PApplet;

public class pillar extends PApplet{
public void setup(){
size(60,40, OPENGL);
}
public void draw() {
//fill(120,1,120);
//rect(20,20,20,20);
float x = mouseX - width/2;
background(255);
fill(0, 102, 153, 26);
for(int i=0; i<height; i+=10) {
for(int j=0; j<width; j+=10) {
beginShape(QUADS);
vertex(x+j, i, 0);
vertex(x+j, i+5, 0);
vertex(j-x, i+5, -400);
vertex(j-x, i, -400);
endShape();
}
}
}
}


thanks,

4dplane
Re: Processing and Swing
Reply #7 - May 5th, 2006, 2:42pm
 
hm, it should work as i've seen it used elsewhere.. are you not getting any error messages in the console? the "works sometimes" thing means that it's a threading issue, that the ui update thread and the animation thread may or may not be working in sync with one another.
Re: Processing and Swing
Reply #8 - May 8th, 2006, 5:44pm
 
I have been working with this for a few days now and every once and awhile I get this error when I am trying to render the script with OPENGL:

Frame at localhost:2477 (may be out of synch) was unable to replace the running code with the code in the workspace.
Reason:
Hot code replaced failed – Scheme changed not implemented

Any thoughts on what this means? If there is a synch problem how do I go about fixing it? Could this be a machine specific problem?

Thanks,

4dplane
Re: Processing and Swing
Reply #9 - Oct 28th, 2006, 9:27pm
 
AFAIK, if you're launching your app from an IDE like eclipse and then see something wrong, go to your code and modify it without stopping the app and then save your modif in your IDE, the IDE can try to swap your class code in the JVM without stopping it. That's what they call "hot code replace".

It just doesn't always work to do that... for many reasons...

But it's not a problem that can occur during normal runtime once your app gets deployed and you don't try to hot swap code.

Amaury
Re: Processing and Swing
Reply #10 - Nov 1st, 2006, 8:13pm
 
thanks for posting the example.

however that's a gross mischaracterization of my position on this. i am not worried about "making this way of working more well known" and can't imagine i've ever said anything as such. this mode of operation is documented here:

http://dev.processing.org/reference/core/javadoc/processing/core/PApplet.html

and as i said above, i use this all the time for my own work, so it's not as though i somehow wish to hide it or prevent others from using it that way.

the only reason i don't spend more time talking about it, or contributing still more time to the "integration" board is because i'm already supporting a massive project, i don't need to complicate things further by supporting anyone who wants to program in java. i have limits, cut me some slack.
Re: Processing and Swing
Reply #11 - Nov 1st, 2006, 9:13pm
 
deleted my posts, not keen to get into a lengthy discussion, i'll think twice before contributing discussion points in future, i think you may have taken as a criticism something that was intended to be a positive well-meaning suggestion; but i do think this area is powerful enough to warrant more than a mention in the javadoc.

here's a blog with a beginning to end example of embedding processing in a swing application: http://meshsystem.net/default.asp?Display=20

('supporting anyone who wants to program in java' - that's hardly what i was suggesting, i just think a brief tutorial on doing this would help what seems to be quite a common enquiry, your response was somewhat fractious)
Page Index Toggle Pages: 1