Loading...
Logo
Processing Forum
One last question...
 
I have a G4P GUI that uses a button on the main window to mute and unmute a Text-To-Speech routine.
There is a secondary window which holds the text field I type into.
 
The problem is that when I click on the mute button, the TTS gets muted, but focus is lost on the secondary window.
I tried setting focus in the following code but it still does not bring focus back to the text field.
The text field needs me to click within it to get focus back...  is there a way to have it refocused without having to move the pointer back into the window???
When I open the secondary window for the first time it is already focused within the text field so I can start typing immediately...
 the problem is when I go back to the primary window to mute...
 
 
TTS mute button:
Copy code
  1. public void tts_click1(GButton source, GEvent event) { //_CODE_:tts:896734:
    if (ttsOnOffFlag==0){  txf1.setFocus(true); ttsOnOffFlag=1; txf1.setFocus(true);
    }else {
      ttsOnOffFlag=0;
      txf1.setFocus(true);
     txf1.setFocus(true);
    }
    }
 
 
Secondary Window turned on by another button:
 
Copy code
  1. public void type_click1(GButton source, GEvent event) { //_CODE_:type:205440:
      calibPNL.setVisible(false);
      remotePNL.setVisible(false);
      typePNL.setVisible(true);
      blankPNL.setVisible(false);
    if (typeOnOffFlag==1){
    window1.close();
    typeOnOffFlag=0;
    }else{typeOnOffFlag=1;
      window1 = new GWindow(this, "Window title", 62, 708, 1858,60, true, JAVA2D);
      window1.setActionOnClose(G4P.CLOSE_WINDOW);
      window1.addDrawHandler(this, "win_draw2");
      txf1 = new GTextField(window1.papplet, 5,5, 1848, 50, G4P.SCROLLBARS_NONE);
      txf1.setFont(new Font("Dialog", Font.PLAIN, 40));
      txf1.setFocus(true);
      txf1.setOpaque(false);
      txf1.addEventHandler(this, "txf1_change1");
    }
    }
 

Replies(1)

When you click on the mute button you make that window active (i.e. the focus for all window events) to give the textfield focus then you need to make the window that contains it active.

Since GWindow inherits from java.awt.Frame which in turn inherits from java.awt.Window then methods in these classes are available for us andafter a bit of looking I found the toFront() method which appears to do the job - try the code below

When you click on the mute button then the textfield gets focus. The setFocus will highlight all the text so a keypress will replace it. If you want to append then press the right arrow key.
 
Copy code
  1. // Need G4P library
  2. import g4p_controls.*;

  3. public void setup(){
  4.   size(480, 320, JAVA2D);
  5.   createGUI();
  6.   customGUI();
  7.   // Place your setup code here
  8.  
  9. }

  10. public void draw(){
  11.   background(230);
  12.  
  13. }
  14. // Use this method to add additional statements
  15. // to customise the GUI controls
  16. public void customGUI(){
  17. }

  18.  void myBtnEvents(GButton button) { //_CODE_:button1:12356:
  19.      // It is safe to enter your event code here 
  20.  } //_CODE_:button1:12356:

  21. public void button1_click1(GButton source, GEvent event) { //_CODE_:button1:872060:
  22.   window1.toFront();
  23.   textfield1.setFocus(true);
  24. } //_CODE_:button1:872060:

  25. synchronized public void win_draw1(GWinApplet appc, GWinData data) { //_CODE_:window1:493117:
  26.   appc.background(230);
  27. } //_CODE_:window1:493117:

  28. // Create all the GUI controls.
  29. // autogenerated do not edit
  30. public void createGUI(){
  31.   G4P.messagesEnabled(false);
  32.   G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
  33.   G4P.setCursor(ARROW);
  34.   if(frame != null)
  35.     frame.setTitle("Sketch Window");
  36.   button1 = new GButton(this, 66, 41, 80, 30);
  37.   button1.setText("Face text");
  38.   button1.addEventHandler(this, "button1_click1");
  39.   window1 = new GWindow(this, "Window title", 0, 0, 240, 120, false, JAVA2D);
  40.   window1.addDrawHandler(this, "win_draw1");
  41.   textfield1 = new GTextField(window1.papplet, 40, 45, 160, 30, G4P.SCROLLBARS_NONE);
  42.   textfield1.setOpaque(true);
  43. }

  44. // Variable declarations
  45. // autogenerated do not edit
  46. GButton button1;
  47. GWindow window1;
  48. GTextField textfield1;