Problem with making Window loading

As I was using the javax.swing library I made a Window, that should paint something if the correct button is pressed. The problem is that it is only painted if I close the window I made. How can I Prevent the program from pausing when the method
JOptionPane.showOptionDialog(…);
is called?
This code will have the same effect:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
background(255);
JButton jb=new JButton("paint");
jb.addActionListener(new ActionListener() {
  void actionPerformed(ActionEvent e) {
    background(0);
  }
}
);
JPanel jp=new JPanel();
jp.add(jb);
JOptionPane.showOptionDialog(
              null, 
              jp,
              "", 
              JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, 
              null, null, null);

1 Like

Hello NumericPrime
You could try using multiple JFrame windows. One example that works (although not the most elegant version of approaching the problem) would be:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

void setup(){
  window w= new window();
}

class window{
JButton jb;
JFrame jf;
JFrame main;

  window(){
    jb=new JButton("paint");
    jf=new JFrame();
    main= new JFrame();
    main.setSize(200,200);
    main.getContentPane().setBackground(new Color(255,255,255));
    main.setVisible(true);
    
    jf.setSize(100,100);
    jf.getContentPane().add(jb);
    jf.setVisible(true);
  
       jb.addActionListener(new ActionListener() {
        
        void actionPerformed(ActionEvent e) {
          main.getContentPane().setBackground(new Color(0,0,0));
          main.setVisible(true);
        }
      });
    }
}
1 Like

This is what JOptionPane’s reference says about it:
Docs.Oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/JOptionPane.html

All dialogs are modal. Each showXxxDialog method blocks the caller until the user’s interaction is complete.

If you don’t wanna halt the execution of the sketch’s “Animation Thread” you’re gonna need to call those modal methods on another Thread:

BtW, I’ve got these 2 input “libraries” w/ JOptionPane-based utility methods:

1 Like

Is there a way to use the main window as a JFrame?

I think you would need to define different threads (as described above) to do that