G4P - button to open new window

edited March 2014 in Library Questions

Hi, I would like to create a button, when pressed on, will create a new window and disable the button. When the window is closed, the button will be enabled again. How can I do that? This is my code:

void handleButtonEvents(GButton button, GEvent event) 
{

  if (event == GEvent.CLICKED) 
  {
      createWindows();

  }// if
}

void createWindows() 
{
  window = new GWindow(this, "Help", 500, 50, 477, 538, false, JAVA2D);
  window.setBackground(help);
  window.setActionOnClose(GWindow.CLOSE_WINDOW);

}// createWindow
Tagged:

Answers

  • Answer ✓

    Like this

    import g4p_controls.*;
    
    GButton btnMakeWindow;
    GWindow window;
    
    void setup() {
      size(200, 200);
      btnMakeWindow = new GButton(this, 10, 20, 140, 20, "Make Window");
    }
    
    void draw(){
      background(255);
    }
    
    void handleButtonEvents(GButton button, GEvent event) {
      if (button == btnMakeWindow && event == GEvent.CLICKED) {
        createWindows();
        btnMakeWindow.setEnabled(false);
      }// if
    }
    
    void createWindows() {
      println("Making Window");
      window = new GWindow(this, "Help", 500, 50, 477, 538, false, JAVA2D);
      window.addOnCloseHandler(this, "windowClosing"); 
      window.setActionOnClose(GWindow.CLOSE_WINDOW);
    }// createWindow
    
    public void windowClosing(GWindow w){
      println("Window closing");
      btnMakeWindow.setEnabled(true);
    }
    
  • If i want to put a text in these new window, how can i do?

  • You need to register a draw method fro the new window. This behaves kust like the main draw() method but you have to prefix all the calls to Processing methods - see below import g4p_controls.*;

    GButton btnMakeWindow;
    GWindow window;
    
    void setup() {
      size(200, 200);
      btnMakeWindow = new GButton(this, 10, 20, 140, 20, "Make Window");
    }
    
    void draw(){
      background(255);
    }
    
    void handleButtonEvents(GButton button, GEvent event) {
      if (button == btnMakeWindow && event == GEvent.CLICKED) {
        createWindows();
        btnMakeWindow.setEnabled(false);
      }// if
    }
    
    synchronized public void window_draw(GWinApplet appc, GWinData data) { 
      appc.background(230); // Notice the REQUIRED prefix appc.
      appc.fill(0);
      appc.text("Hello World", 10,30);
    } 
    
    void createWindows() {
      println("Making Window");
      window = new GWindow(this, "Help", 500, 50, 477, 538, false, JAVA2D);
      window.addDrawHandler(this, "window_draw");
      window.addOnCloseHandler(this, "windowClosing");
      window.setActionOnClose(GWindow.CLOSE_WINDOW);
    }// createWindow
    
    public void windowClosing(GWindow w){
      println("Window closing");
      btnMakeWindow.setEnabled(true);
    }
    
  • ok, i want to put these code in the new window, i on't know how to do it. String title = "MICROHERBS: Software to help you to identify herbal drug"; String Name = ""; void setup() { size(760, 300); textAlign(LEFT, LEFT); textSize(25); fill(0); } void draw() { background(255); text (title,10,0, width, height); text("Name: "+Name, 10, 140, width, height); } void keyPressed() { if (keyCode == BACKSPACE) { if (Name.length() > 0) { Name = Name.substring(0, Name.length()-1); } } else if (keyCode == DELETE) { Name = ""; } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) { Name = Name + key; } }

  • Please format your code for this forum : highlight the code and press Ctrl+K

  • edited December 2014

    ok, I want to put these code in the new window and I don't know how to do it.

    String title = "MICROHERBS: Software to help you to identify herbal drug";
    String Name = ""; 
    void setup() {
      size(760, 300);
      textAlign(LEFT, LEFT);
      textSize(25);
      fill(0);
    } 
    void draw() {
      background(255);
      text (title,10,0, width, height);
      text("Name: "+Name, 10, 140, width, height);
    } 
    void keyPressed() {
      if (keyCode == BACKSPACE) {
        if (Name.length() > 0) {
          Name = Name.substring(0, Name.length()-1);
        }
      } else if (keyCode == DELETE) {
        Name = "";
      } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) {
        Name = Name + key;
      }
    }
    
  • edited December 2014

    Please format your code for this forum see here if you don't know how to do it.

    On this occassion I have formatted the code for you but please find out how to do this yourself.

  • I have change the variable Name to name because we only capitalise the first letter if it is the name of a class.

    I have also modified the example to show how to use key event handling n GWindows.

    import g4p_controls.*;
    
    GButton btnMakeWindow;
    GWindow window;
    
    String title = "MICROHERBS: Software to help you to identify herbal drug";
    String name = ""; 
    
    void setup() {
      size(200, 200);
      btnMakeWindow = new GButton(this, 10, 20, 140, 20, "Make Window");
    }
    
    void draw() {
      background(255);
    }
    
    void handleButtonEvents(GButton button, GEvent event) {
      if (button == btnMakeWindow && event == GEvent.CLICKED) {
        createWindows();
        btnMakeWindow.setEnabled(false);
      }
    }
    
    // This method is the draw method for the window
    synchronized public void window_draw(GWinApplet appc, GWinData data) {
      appc.background(255); // Notice the REQUIRED prefix appc.
      appc.text (title, 10, 0, appc.width, appc.height);
      appc.text("Name: "+ name, 20, 140, appc.width, appc.height);
    }
    
    // This method is the handling key eventsfor the window
    synchronized public void win_key(GWinApplet appc, GWinData data, KeyEvent kevent) {
      if (kevent.getAction() == KeyEvent.PRESS) {
        println("window - key event " + millis());
        if (appc.keyCode == BACKSPACE) {
          if (name.length() > 0) {
            name = name.substring(0, name.length()-1);
          }
        } else if (appc.keyCode == DELETE) {
          name = "";
        } else if (appc.keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) {
          name = name + appc.key;
        }
      }
    }
    
    void createWindows() {
      println("Making Window");
      window = new GWindow(this, "Help", 500, 50, 760, 300, false, JAVA2D);
      window.addDrawHandler(this, "window_draw");
      window.addKeyHandler(this, "win_key");
      window.addOnCloseHandler(this, "windowClosing");
      window.setActionOnClose(GWindow.CLOSE_WINDOW);
      // Place here any code that would normally be in 
      // setup for this window except for the size which
      // is used above when creating the window.
      window.papplet.textAlign(LEFT, LEFT);
      window.papplet.textSize(25);
      window.papplet.fill(0);
    }// createWindow
    
    public void windowClosing(GWindow w) {
      println("Window closing");
      btnMakeWindow.setEnabled(true);
    }
    
  • hello, I tried the code (the first one to open a new window) but tells me: ClassNoFoundException: processing.core.PGraphicsJava2d

    this is not the first time, also when I tried to run examples from p4g often happens. can you please help me? thank you for any suggestions

Sign In or Register to comment.