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 › textfield and buttons from controlp5
Page Index Toggle Pages: 1
textfield and buttons from controlp5 (Read 1306 times)
textfield and buttons from controlp5
Aug 23rd, 2009, 2:30pm
 
Hi,
i have used the ControlP5 library to create a textfield and buttons in my sketch and would like to translate the the text from the textfield into different languages depending on which button is pressed. In the controlEvent() method i am writing code for the different events,
but don't seem to be able to pass the textinput to the buttons. I looked at the documentation and previous posts on this library but still can't figure it out. Can anybody help on how to do this? Thanks




import controlP5.*;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
ControlP5 controlP5;
public int myColorBackground = 200;
Textfield myTextfield;


void setup()
{
 size(450,350);
 frameRate(25);
 background(myColorBackground);
 controlP5 = new ControlP5(this);

 controlP5.Button fre  = controlP5.addButton("FRE",20,360,40,20,20);
 fre.setColorBackground(0);
 fre.setId(1);
 fre.setColorActive(60);
 
 controlP5.Button ger  = controlP5.addButton("GER",0,400,40,20,20);
 ger.setColorBackground(0);
 ger.setId(2);
 ger.setColorActive(60);
 
 myTextfield = controlP5.addTextfield("texts",100,300,200,20);
 myTextfield.setFocus(true);
}

void draw() {
}


void controlEvent(ControlEvent theEvent)
{
 boolean textfieldeventhappened = false;
 println("got a control event from controller with id "+theEvent.controller().id());

//textfield
if( theEvent.controller().id() == -1)
{
 String lines = (myTextfield.getText());
 textfieldeventhappened = true;
   println(textfieldeventhappened);
}

//button
else if(( theEvent.controller().id() == 1) && ( textfieldeventhappened == true))
   {
     String inputtext = (myTextfield.getText());
     translateThis( inputtext);
   }
   
//button    
else if(( theEvent.controller().id() == 2) && ( textfieldeventhappened == true))
   {
         String language = "GERMAN";
         println(language);
   }  
}


void translateThis(String inputtext)
{
try
  {
    String translatedText = Translate.translate(inputtext, Language.ENGLISH, Language.FRENCH);
    println(translatedText);
  }
  catch (Exception ex)
  {
    ex.printStackTrace();
  }
}
Re: textfield and buttons from controlp5
Reply #1 - Aug 24th, 2009, 6:49am
 
hi,
since a controlEvent is called for each event individually, the variable textfieldeventhappened in your case will never be true for both of the buttons since you are always setting its value back to false when controlEvent is called. you could make textfieldeventhappened global, by taking its declaration out of the controlEvent function - the translation (calling translateThis inside controlEvent) wouldnt work for the first textfield-input though, only after you use return in the textfield for the first time. Why is that? textfieldeventhappened is false, controlEvent will only be called by a textfield when using a return. the first time you use return will set textfieldeventhappened to true. now, whenever you press one of the buttons, textfieldeventhappened will be true and whatever you have assigned button 1 and 2 to do, will be executed. maybe you dont need variable textfieldeventhappened at all?
Re: textfield and buttons from controlp5
Reply #2 - Aug 24th, 2009, 6:54am
 
Well the reason none of your methods are working is because right at the beginning of the controlEvent() method, you are assigning false to textfieldeventhappened. So whenever an event happens, textfieldeventhappened will always be false, no matter what, and none of your conditionals will be met.

I've found that when using controlP5, it's much easier to create custom methods for each event, rather than having a cluttered controlEvent() method. I'm not sure if you know how to do this already, but it may help. Here's how you would do it:

Code:

//you created a textfield called myTextfield
//you can set up a custom method like this:

public void myTextfield(String _theText){
    translateThis(_theText);
}

So that method above would fire every time that text field was submitted. So then to submit the text field, you just create a custom method for your button to submit the text field. Like this:

Code:

public void ger(int theValue){
    String language = "GERMAN";
    myTextfield.submit();
}

The submit() button is automatically assigned to your text field, so you can call that anywhere, and it will trigger your text field to submit itself, which will then trigger the method that we created above automatically.

I hope this helps. I struggled a lot with ControlP5 as well when I was first starting out, and this helped me organize my sketch a lot better and keep track of everything without a ton of conditionals stacking up in my controlEvent() method.
Re: textfield and buttons from controlp5
Reply #3 - Sep 4th, 2009, 1:54pm
 
hi, thanks for your advice, I have tried, but am still not getting my head around how this works. I tried to use custom method for each event, but still don't understand how to feed the text from the textfield and the info from the button into one method. I am trying to make the buttons determine into which language i am translating my input text.  I am really struggling to understand this.


public void texts(String theText){
   
 println(theText);
 translateThis(theText);
 
}

public void GER(int theValue){

    String lang = "GERMAN";
    println(lang);
    texts.submit();
}


public void FRE(int theValue)
{

    String lang = "FRENCH";
    println(lang);
    texts.submit();
}



void translateThis(String theText, String lang)
{
try
 {
   String translatedText = Translate.translate(theText, Language.ENGLISH, Language.lang);
   println(translatedText);
 }
 catch (Exception ex)
 {
   ex.printStackTrace();
 }
}
Page Index Toggle Pages: 1