We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi I only started months ago using Processing. I used the library ControlP5 to create my GUI. I modified the example provided by the library called ControlP5Frames in /controllers. I want to display another window when I pressed a button in the main window. My problem is, error _ConcurrentModificationException_ occurs when I close the second window.
Below is my code:
`import java.awt.*;
import java.awt.event.*;
import controlP5.*;
private ControlP5 cp5;
ControlFrame cfConfig;
String actualColors[] = {"Red","Green","Blue","Yellow"};
String arrangement[] = {"Corners","All Top","Right Side","Left Side"};
int selectedColors[] = new int[4];
int selectedArrangement = 0;
int removeMe;
void initMainGUI(){
cp5.addButton("config")
.setLabel("Configuration")
.setPosition(100,400)
.setSize(200,19)
;
cp5.addButton("homePosition")
.setLabel("Home Position")
.setPosition(400,400)
.setSize(200,19)
;
cp5.addButton("systemStart")
.setLabel("Start Sorting")
.setPosition(700,400)
.setSize(200,19)
;
}
void config(){
cfConfig = addControlFrame( "Configuration", 720, 600, 20, 50, color( 100 ), 0);
}
void setup(){
size(1000, 500);
cp5 = new ControlP5(this);
initMainGUI();
}
void draw(){
}
ControlFrame addControlFrame(String theName, int theWidth, int theHeight, int theX, int theY, int theColor ,int type) {
final Frame f = new Frame( theName );
final ControlFrame p = new ControlFrame( this, theWidth, theHeight, theColor, type);
f.add( p );
p.init();
f.setTitle(theName);
f.setSize( p.w, p.h );
f.setLocation( theX, theY );
f.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent we) {
p.dispose();
f.dispose();
}
}
);
f.setResizable( false );
f.setVisible( true );
// sleep a little bit to allow p to call setup.
// otherwise a nullpointerexception might be caused.
try {
Thread.sleep( 100 );
}
catch(Exception e) {
}
return p;
}
class ControlFrame extends PApplet {
int w, h;
int bg;
int windowType;
PApplet parent;
ControlP5 cp5;
public ControlFrame(PApplet _parent, int _w, int _h, int theColor,int type) {
super();
parent = _parent;
w=_w;
h=_h;
bg = theColor;
windowType=type;
}
public void setup() {
size(w, h);
cp5 = new ControlP5(this);
if(windowType==0){
initConfig();
}
}
public void initConfig(){
cp5.addScrollableList("prioOne")
.setLabel(" ")
.setPosition(100, 60)
.setSize(100, 100)
.setBarHeight(20)
.setItemHeight(20)
.addItems(actualColors)
.setOpen(false)
;
cp5.addTextlabel("labelOnce")
.setText("Priority One")
.setFont(createFont("Calibri",12))
.setPosition(20,60)
.setColorValue(#000000)
;
cp5.addTextlabel("labelTwo")
.setText("Priority Two")
.setFont(createFont("Calibri",12))
.setPosition(20,90)
.setColorValue(#000000)
;
cp5.addTextlabel("labelThree")
.setText("Priority Three")
.setFont(createFont("Calibri",12))
.setPosition(20,120)
.setColorValue(#000000)
;
cp5.addTextlabel("labelFour")
.setText("Priority Four")
.setFont(createFont("Calibri",12))
.setPosition(20,150)
.setColorValue(#000000)
;
cp5.addTextlabel("labelFive")
.setText("Arrangement")
.setFont(createFont("Calibri",12))
.setPosition(20,180)
.setColorValue(#000000)
;
cp5.addTextlabel("labePOH")
.setText("Priority One H")
.setFont(createFont("Calibri",12))
.setPosition(20,250)
.setColorValue(#000000)
;
cp5.addTextlabel("labePTH")
.setText("Priority Two H")
.setFont(createFont("Calibri",12))
.setPosition(20,310)
.setColorValue(#000000)
;
cp5.addTextlabel("labePThH")
.setText("Priority Three H")
.setFont(createFont("Calibri",12))
.setPosition(20,370)
.setColorValue(#000000)
;
cp5.addTextlabel("labePFH")
.setText("Priority Four H")
.setFont(createFont("Calibri",12))
.setPosition(20,430)
.setColorValue(#000000)
;
cp5.addTextlabel("labeSmtH")
.setText("Gripper H")
.setFont(createFont("Calibri",12))
.setPosition(20,490)
.setColorValue(#000000)
;
}
boolean updateGui=false;
int nextState=0;
void updatingGui(){
if(nextState==1){
cp5.addScrollableList("prioTwo")
.setLabel(" ")
.setPosition(100, 90)
.setSize(100, 100)
.setBarHeight(20)
.setItemHeight(20)
.addItems(actualColors)
.setOpen(false)
.bringToFront();
;
}else if(nextState==2){
cp5.addScrollableList("prioThree")
.setLabel(" ")
.setPosition(100, 120)
.setSize(100, 100)
.setBarHeight(20)
.setItemHeight(20)
.addItems(actualColors)
.setOpen(false)
.bringToFront();
;
}else if(nextState==3){
cp5.addScrollableList("prioFour")
.setLabel(" ")
.setPosition(100, 150)
.setSize(100, 100)
.setBarHeight(20)
.setItemHeight(20)
.addItems(actualColors)
.setOpen(false)
.bringToFront();
;
}else if(nextState==4){
cp5.addScrollableList("prioFive")
.setLabel(" ")
.setPosition(100, 180)
.setSize(100, 100)
.setBarHeight(20)
.setItemHeight(20)
.addItems(arrangement)
.setOpen(false)
.bringToFront();
;
}else if(nextState==5){
cp5.addSlider("oneH")
.setPosition(20,270)
.setSize(200,20)
.setRange(0,255)
.setValue(128)
.getCaptionLabel().align(ControlP5.TOP, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
;
cp5.addSlider("twoH")
.setPosition(20,330)
.setSize(200,20)
.setRange(0,255)
.setValue(128)
.getCaptionLabel().align(ControlP5.TOP, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
;
cp5.addSlider("threeH")
.setPosition(20,390)
.setSize(200,20)
.setRange(0,255)
.setValue(128)
.getCaptionLabel().align(ControlP5.TOP, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
;
cp5.addSlider("fourH")
.setPosition(20,450)
.setSize(200,20)
.setRange(0,255)
.setValue(128)
.getCaptionLabel().align(ControlP5.TOP, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
;
cp5.addSlider("fiveH")
.setPosition(20,510)
.setSize(200,20)
.setRange(0,255)
.setValue(128)
.getCaptionLabel().align(ControlP5.TOP, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
;
}
updateGui=false;
}
boolean prioOne=true;
void prioOne(int n){
println("Priority One: " + actualColors[n]);
selectedColors[0]=n;
if(prioOne){
updateGui=true;
nextState=1;
prioOne=false;
}
}
boolean prioTwo=true;
void prioTwo(int n){
println("Priority Two: " + actualColors[n]);
selectedColors[1]=n;
removeMe=n;
if(prioTwo){
updateGui=true;
nextState=2;
prioTwo=false;
}
}
boolean prioThree=true;
void prioThree(int n){
println("Priority Three: " + actualColors[n]);
selectedColors[2]=n;
removeMe=n;
if(prioThree){
updateGui=true;
nextState=3;
prioThree=false;
}
}
boolean prioFour=true;
void prioFour(int n){
println("Priority Four: " + actualColors[n]);
selectedColors[3]=n;
removeMe=n;
if(prioFour){
updateGui=true;
nextState=4;
prioFour=false;
}
}
boolean prioFive=true;
void prioFive(int n){
println("Arrangement: "+arrangement[n]);
selectedArrangement=n;
removeMe=n;
if(prioFive){
updateGui=true;
nextState=5;
prioFive=false;
}
}
void oneH(int theColor) {
println("Priority One H: "+theColor);
}
void twoH(int theColor) {
println("Priority Two H: "+theColor);
}
void threeH(int theColor) {
println("Priority Three H: "+theColor);
}
void fourH(int theColor) {
println("Priority Four H: "+theColor);
}
void fiveH(int theColor) {
println("Gripper H: "+theColor);
}
public void draw(){
background(bg);
if(updateGui){
updatingGui();
}
}
}
`
Answers
Go back and edit your post so the code is correctly formatted for the forum. Read
How to format code
https://GitHub.com/sojamo/controlp5/pull/72
@quark sorry this is my first time posting in the forum. Thanks!
@GoToLoop Hi! I saw that thread and read it several times. But I can't seem to know how to apply it in my code.
Disclaimer: Never used ControlP5 for myself. Very rookie here! 3:-O
Now that your code is formatted, I can see it's very complex b/c it's got 2 PApplet. And each 1 got its own ControlP5.
However, that Frame approach to create the 2nd PApplet isn't standard for Processing! And it isn't even compatible w/ Processing 3! :-SS
I'd suggest you to use runSketch() in order to instantiate your 2nd PApplet: :-B
A more specific forum link example: O:-)
https://forum.Processing.org/two/discussion/16457/size-method-for-intial-window-not-working-when-more-windows-are-are-added#Item_2
@GoToLoop Hi again! Thank you for responding! I've seen several users that you also helped. :)
One of which is this thread: https://forum.processing.org/two/discussion/10937/#Comment_43019
I tried running Multiple Nested Applets (v2.0) and I loved it! Maybe I'll just reprogram my code and use yours as a pattern if you don't mind?
However, how would I to prevent the "main" sketch from closing when I close the "generated" sketches? Because one of my objectives is to generate a sketch when I press a button in the main sketch.
Much appreciated!
https://forum.Processing.org/two/discussion/17310/intercept-window-closure-in-p2d-renderer-processing-3#Item_5