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