CP5 TAB to next textField

Hello World...I am by no means a JAVA developer but I am pretty stuck here and would love some guidance...I have scoured this forum and the internets looking for a solution.

I am using CP5 to develop a desktop app...I have 6 text fields I would like to use the TAB key to cycle through and clear the text at the same time:

        void keyPressed() { 
          if (keyCode == TAB) { 
        cp5.get(Textfield.class, "text1").clear();

Here are what my textfields look like text1-text6:

  txt1 = cp5.addTextfield("text1")
      .setColorCursor(0xff686868)
        .setValue("00")
          .setPosition(192, 191)
            .setSize(42, 18)
              .setFocus(true)
              .setAutoClear(false)
                .setInputFilter(ControlP5.FLOAT)
                .setId(1);

I am surprised this hasn't been asked before, any help would be greatly appreciated!

THANKS!

Answers

  • edited June 2014

    I dunno how to use that library yet. But I've noticed that your fields are named something like "text" + index, right?
    Make a variable called something like txtFieldIdx. Inside keyPressed() do something like:

    static final int TXT_FIELDS = 6;
    int txtFieldIdx = 1;
    
    void keyPressed() {
      final int k = keyCode;
    
      if (k == TAB) {
        if (++txtFieldIdx > TXT_FIELDS)   txtFieldIdx = 1;
        cp5.get(Textfield.class, "text" + txtFieldIdx).clear();
      }
    }
    

    As mentioned, dunno whether it's gonna work or not! Good luck! %%-

  • Brilliant! That works great..how can I have it .setFocus for each of the fields as well when I tab to it?

    I am adding a setFocus line but it keeps them all focused:

            void keyPressed() {
              final int k = keyCode;
    
              if (k == TAB) {
                if (++txtFieldIdx > TXT_FIELDS)   txtFieldIdx = 1;
                cp5.get(Textfield.class, "text" + txtFieldIdx).clear();
             cp5.get(Textfield.class, "text" + txtFieldIdx).setFocus(true); //new line
              }
            }
    

    Thanks for the quick response!

  • Answer ✓

    Just a guess, but perhaps you should turn off the focus from the previous Textfield? :-??

    // forum.processing.org/two/discussion/5549/cp5-tab-to-next-textfield
    
    import controlP5.*;
    ControlP5 cp5;
    
    static final int TXT_FIELDS = 6;
    int txtFieldIdx = 1;
    
    void keyPressed() {
      final int k = keyCode;
    
      if (k == TAB) {
        cp5.get(Textfield.class, "text" + txtFieldIdx).setFocus(false);
        if (++txtFieldIdx > TXT_FIELDS)   txtFieldIdx = 1;
        cp5.get(Textfield.class, "text" + txtFieldIdx).setFocus(true).clear();
      }
    }
    
  • Thanks..that works perfectly. I'm sure others will find this most helpful!

Sign In or Register to comment.