G4P: Create objects dynamically

edited July 2014 in Library Questions

Hello! One more question about GUI4Processing. Is there a way to create few objects during main cycle? For example to create 3 independent radiobuttons after the button was pressed?

Tagged:

Answers

  • Although this is possible it is better to create them at startup and make them invisible with setVisible(false) and then make them visible whin they are needed.

    If you really want to dynamically create controls then the sketch below show you how. It creates and displays a single button and when the button is clicked it creates 3 radio-buttons and 3 check-boxes.

    import g4p_controls.*;
    
    GButton button1; 
    GToggleGroup togGroup1; 
    GOption radioBtn1; 
    GOption radioBtn2; 
    GOption radioBtn3; 
    GCheckbox checkbox1; 
    GCheckbox checkbox2; 
    GCheckbox checkbox3; 
    
    
    public void setup(){
      size(480, 320, JAVA2D);
      createGUI();
    
    }
    
    public void draw(){
      background(230);
    }
    
    public void createGUI(){
      G4P.messagesEnabled(false);
      G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
      G4P.setCursor(ARROW);
      if(frame != null)
        frame.setTitle("Sketch Window");
      button1 = new GButton(this, 10, 10, 80, 30);
      button1.setText("Face text");
      button1.addEventHandler(this, "button1_click1");
    }
    
    public void button1_click1(GButton source, GEvent event) {
      togGroup1 = new GToggleGroup();
      radioBtn1 = new GOption(this, 120, 20, 120, 20);
      radioBtn1.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      radioBtn1.setText("Radio Button 1");
      radioBtn1.setOpaque(false);
      radioBtn1.addEventHandler(this, "radioBtn1_clicked");
      radioBtn2 = new GOption(this, 120, 40, 120, 20);
      radioBtn2.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      radioBtn2.setText("Radio Button 2");
      radioBtn2.setOpaque(false);
      radioBtn2.addEventHandler(this, "radioBtn2_clicked");
      radioBtn3 = new GOption(this, 120, 60, 120, 20);
      radioBtn3.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      radioBtn3.setText("Radio Button 3");
      radioBtn3.setOpaque(false);
      radioBtn3.addEventHandler(this, "radioBtn3_clicked");
      togGroup1.addControl(radioBtn1);
      radioBtn1.setSelected(true);
      togGroup1.addControl(radioBtn2);
      togGroup1.addControl(radioBtn3);
      checkbox1 = new GCheckbox(this, 240, 20, 120, 20);
      checkbox1.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      checkbox1.setText("Checkbox 1");
      checkbox1.setOpaque(false);
      checkbox1.addEventHandler(this, "checkbox1_clicked");
      checkbox2 = new GCheckbox(this, 240, 40, 120, 20);
      checkbox2.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      checkbox2.setText("Checkbox 2");
      checkbox2.setOpaque(false);
      checkbox2.addEventHandler(this, "checkbox2_clicked");
      checkbox3 = new GCheckbox(this, 240, 60, 120, 20);
      checkbox3.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      checkbox3.setText("Checkbox 3");
      checkbox3.setOpaque(false);
      checkbox3.addEventHandler(this, "checkbox3_clicked");
    } 
    
    public void radioBtn1_clicked(GOption source, GEvent event) {
      println("radioBtn1 - GOption event occured " + System.currentTimeMillis()%10000000 );
    }
    
    public void radioBtn2_clicked(GOption source, GEvent event) {
      println("radioBtn2 - GOption event occured " + System.currentTimeMillis()%10000000 );
    }
    
    public void radioBtn3_clicked(GOption source, GEvent event) {
      println("radioBtn3 - GOption event occured " + System.currentTimeMillis()%10000000 );
    }
    
    public void checkbox1_clicked(GCheckbox source, GEvent event) {
      println("checkbox1 - GCheckbox event occured " + System.currentTimeMillis()%10000000 );
    }
    
    public void checkbox2_clicked(GCheckbox source, GEvent event) {
      println("checkbox2 - GCheckbox event occured " + System.currentTimeMillis()%10000000 );
    }
    
    public void checkbox3_clicked(GCheckbox source, GEvent event) {
      println("checkbox3 - GCheckbox event occured " + System.currentTimeMillis()%10000000 );
    } 
    
  • No, this is not the thing I talk about, though it could be used if somebody wants dynamically place objects. You create object dynamically, but you declare it and its eventlistener statically. That means you know how many objects you'll need. I don't. In my program I read data(connected to previous topic, so I can say "from UART") and depending on how many IDs I receive in messages I need to create radiobuttons for them. Of course I could use object arrays, but It would solve only first part of problem, I'd still have to declare eventlisteners. The most rude solution is to create 10 radiobuttons, make them invisible and hope, that I'll never get more than 10 IDs :)

  • Answer ✓

    The eventhandler has to exist because it must be compiled before the sketch can be run.

    This code demonstrates what you want

    import g4p_controls.*;
    
    GButton button1;
    
    public void setup() {
      size(480, 320, JAVA2D);
      createGUI();
    }
    
    public void draw() {
      background(230);
    }
    
    public void handleToggleControlEvents(GToggleControl option, GEvent event) { 
      println(option.tag + " " + option.tagNo);
    }
    
    public void createGUI() {
      G4P.messagesEnabled(false);  
      G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
      G4P.setCursor(ARROW);
      if (frame != null)
        frame.setTitle("Sketch Window");
      button1 = new GButton(this, 10, 10, 80, 30);
      button1.setText("Face text");
      button1.addEventHandler(this, "button1_click1");
    }
    
    public void button1_click1(GButton source, GEvent event) {
      GToggleGroup togGroup1 = new GToggleGroup();
      GOption radioBtn1 = new GOption(this, 120, 20, 120, 20);
      radioBtn1.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      radioBtn1.setText("Radio Button 1");
      radioBtn1.setOpaque(false);
      radioBtn1.tag = "Radio button";
      radioBtn1.tagNo = 1;
      GOption radioBtn2 = new GOption(this, 120, 40, 120, 20);
      radioBtn2.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      radioBtn2.setText("Radio Button 2");
      radioBtn2.setOpaque(false);
      radioBtn2.tag = "Radio button";
      radioBtn2.tagNo = 2;
      GOption radioBtn3 = new GOption(this, 120, 60, 120, 20);
      radioBtn3.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      radioBtn3.setText("Radio Button 3");
      radioBtn3.setOpaque(false);
      radioBtn3.tag = "Radio button";
      radioBtn3.tagNo = 3;
      togGroup1.addControl(radioBtn1);
      radioBtn1.setSelected(true);
      togGroup1.addControl(radioBtn2);
      togGroup1.addControl(radioBtn3);
      GCheckbox checkbox1 = new GCheckbox(this, 240, 20, 120, 20);
      checkbox1.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      checkbox1.setText("Checkbox 1");
      checkbox1.setOpaque(false);
      checkbox1.tag = "Checkbox";
      checkbox1.tagNo = 1;  
      GCheckbox checkbox2 = new GCheckbox(this, 240, 40, 120, 20);
      checkbox2.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      checkbox2.setText("Checkbox 2");
      checkbox2.setOpaque(false);
      checkbox2.tag = "Checkbox";
      checkbox2.tagNo = 2;  
      GCheckbox checkbox3 = new GCheckbox(this, 240, 60, 120, 20);
      checkbox3.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
      checkbox3.setText("Checkbox 3");
      checkbox3.setOpaque(false);
      checkbox3.tag = "Checkbox";
      checkbox3.tagNo = 3;
    }
    
Sign In or Register to comment.