How to disable closing button

Hi there
I my recent project i need to create multiple windows( which i have already done), the probleme is when i close a window all the create windows also close.

Does anyone know in processing 4 how to stop the windows from closing when i click the closing button, or even better just close the window that the button was pressed?

imagem

Thanks!! :smiley:

Does anyone know in processing 4 how to stop the windows from closing when i click the closing button, or even better just close the window that the button was pressed?

To avoid this situation one approach is to use all Swing JFrame windows and suppress the default window provided by Processing by not showing it. It gets more complicated if you want to use draw(); then you might have to use the default window.

The following code shows three independent JFrame windows:

import javax.swing.*;

void setup() {
  surface.setVisible(false); // Don't show default window
  
  JFrame wnd1 = new JFrame("Window One");
  wnd1.setBounds(100,100,300,350);
  wnd1.setVisible(true); 
  
  JFrame wnd2 = new JFrame("Window Two");
  wnd2.setBounds(300,200,300,350);
  wnd2.setVisible(true); 
  
  JFrame wnd3 = new JFrame("Window Three");
  wnd3.setBounds(500,300,300,350);
  wnd3.setVisible(true); 
  
}

void draw(){
  // Use default canvas from default wnd if you need this for graphics
}

Hello
Thanks for awsering
When i do that the windows crashes, or in other words when i run:
surface.setVisible(false);
Then window miinmizes and frezzes

Do you know why?

Processing has a default window which you can’t get rid of; its presence and size is controlled with a call to ‘size()’. If you don’t use size() it will create the window anyway and it will be about 100 x 100 in size. That window is always created, but you don’t have to look at it. We can do that by using surface.setVisible(false) which tells the runtime to not show the window; it’s still created, but you shouldn’t see it except for a split second. If you look closely you will see a ‘flash’ as it is created and hidden. There should not be a crash or freeze of your program when this happens. If the app is crashing or freezing then there is some other problem with your code. We would be happy to look at the source code if you want to post a minimal example of the problem. If you use the JFrame approach then you can’t use draw() and have to use other Swing components to provide window content. Which approach you use will depend on what you are trying to do. The bottom line is that if you use the default window that Processing provides and you close it first then your entire app shuts down. If you don’t want this to happen then you have to find some other way to create windows besides using the default window. This is possible with Swing as I have shown or you could possibly use JavaFx windows.

Addendum:

It just occurred to me that another option would be to use a default window which has no close button; this is called an ‘undecorated’ window. You can still use draw() as shown with the demo below, but the only way you can close the window is by using ‘command Q’ (macOS), which will close your app.

import java.awt.*;

Frame frame;

void setup() {
  size(400, 400);
  frame = ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
  frame.removeNotify();
  frame.setUndecorated(true);
  frame.setVisible(true);
  frame.setLocation(100, 100);
  surface.setResizable(true);
}

void draw() {
  circle(100, 100, 50);
}

Undecorated Window (no close button):

2 Likes

Hi @wonderways,

you just need to override the exit function in your child window. This will just close the current window but not the main window. If you close the main window all children will be closed …

Cheers
— mnse

example

ArrayList<Child> children;
void setup() {
	size(500,500);  
}
void draw() {
  background(0);
}
void mouseReleased() {
  if (children == null)
    children = new ArrayList<>(); 
  children.add(new Child());
}

class Child extends PApplet {
  public Child() {
    super();
    PApplet.runSketch(new String[]{this.getClass().getName()}, this);    
  }
  
  public void settings() {
    size(400, 400);
  }
  public void setup() {
    surface.setTitle("Child sketch");
  }
  public void draw() {
    background(0);
  } 
  // override exit
  public void exit() {
  }
}
2 Likes