We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi there,
Have constructed a bit of code that creates say 5 text fields along with corresponding submit buttons.
Have managed this with For Loops. Is there a more elegant way to do this?
I feel my code can be improved as it would be very cumbersome to keep copy-pasting and changing the relative numbers especially for the submit() function. The overall code would take up too much space.
Can the submit function reside within a for loop as well?
Would love to hear some ideas so that I can take it forward. Thanks!
Here is the code I'm using:
import controlP5.*;
ControlP5 cp5;
// Should we do an array for this initialization?
// I don't want to type out say 30 textValues. What is the more elegant way to represent this?
// Arrays.fill()??
String textValue_1 = "";
String textValue_2 = "";
String textValue_3 = "";
String textValue_4 = "";
String textValue_5 = "";
void setup() {
size(800, 1000);
cp5 = new ControlP5(this);
for (int i = 1; i < 6; ++i) {
cp5.addTextfield("textValue" + "_" + i)
.setPosition(20, 80*i)
.setSize(200, 40)
.setFont(createFont("arial", 20))
.setCaptionLabel("Enter Text for field " + i)
.setAutoClear(false)
;
cp5.addButton("submit"+ "_" + i)
.setPosition(240, 80*i)
.setSize(80, 40)
.setCaptionLabel("submit")
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
;
}
}
void draw() {
background(0);
}
// The function for submit... can this be done in a for loop?
// I may want a large number of text fields
// and it can get tedious typing it all out every time I change the number
public void submit_1() {
cp5.get(Textfield.class, "textValue_1").submit();
}
public void submit_2() {
cp5.get(Textfield.class, "textValue_2").submit();
}
public void submit_3() {
cp5.get(Textfield.class, "textValue_3").submit();
}
public void submit_4() {
cp5.get(Textfield.class, "textValue_4").submit();
}
public void submit_5() {
cp5.get(Textfield.class, "textValue_5").submit();
}
void controlEvent(ControlEvent theEvent) {
if (theEvent.isAssignableFrom(Textfield.class)) {
println("controlEvent: accessing a string from controller '"
+theEvent.getName()+"': "
+theEvent.getStringValue()
);
}
}
Answers
Hey, a for-loop looks like a good way to simplify multiple similiar tasks. Another one is using arrays. But in this case it could be enough to work with IDs.
I made some modifications to your code, maybe this helps:
Thanks Benja... this really helps in understanding the use of Arrays as well as the concept of ids. But i am a little lost when it comes to buttons performing different functions?
Submit and Clear buttons:
Was going through and decided to add another set of buttons that would perform the 'clear' function. However, these buttons would have the same ids and don't perform the right function of clearing the text in the text field.
I tried using 'j' instead of 'i' in an additional for loop only for the clear buttons but obviously the ids all start from 0 again and they overlap. So that fails.
The current modified version shown below does not perform the 'clear' function. How can I set another range of ids for the 'clear' buttons? Thanks for all the help!
:)
Well, if it gets more complex, IDs might not work in the way i used them for that example. There was only button associated to each textfield.
We could of course use names for the controllers like "submit_1" etc. and extract type and index from that string. But i thnk i prefer using arrays to store the controllers and use thy IDs to define the type of button, makes more sense to me.
Here is an example of waht i mean:
As a next step i would recommend to create a class for such a group of controllers, makes things much more organized. A good introduction to this topic is here: https://forum.processing.org/two/discussion/8081/from-several-arrays-to-classes
Thanks Benja, you have explained this very well and am able to understand completely. Thanks so much for all your help. I'm still learning and hopefully will be able to work out the classes to make this code better. But for now this is awesome! :-) :-) :-) Cheers!