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:
Inputs.java
/**
* Keyboard Input Library
* Andrew Errity v0.2 (2015-Oct-01)
* GoToLoop v1.0.4 (2015-Oct-22)
*
* https://Forum.Processing.org/two/discussion/13175/
* do-whille-is-not-working-how-it-suppose-to#Item_12
*
* https://GitHub.com/aerrity/Inputs/blob/master/src/Inputs.java
* https://Gist.GitHub.com/GoToLoop/bba0c288aaeeb5ef9bb1
This file has been truncated. show original
Keyboard_Input_Library.pde
/**
* Keyboard Input Library
* Andrew Errity v0.2 (2015-Oct-01)
* GoToLoop v1.0.4 (2015-Oct-22)
*
* https://Forum.Processing.org/two/discussion/13175/
* do-whille-is-not-working-how-it-suppose-to#Item_12
*
* https://GitHub.com/aerrity/Inputs/blob/master/src/Inputs.java
* https://Gist.GitHub.com/GoToLoop/bba0c288aaeeb5ef9bb1
This file has been truncated. show original
NoRepeatIdInput.pde
/**
* No Repeat ID Input (v2.1.1)
* by GoToLoop (2015/Sep/15)
*
* Forum.Processing.org/two/discussion/12532/
* windowjs-cross-mode-alert-confirm-prompt-other-js-api-for-java#Item_2
*
* Forum.Processing.org/two/discussion/869/check-array-contents-with-arraylist
*
* Studio.ProcessingTogether.com/sp/pad/export/ro.9$Bjf6i21oXBw
This file has been truncated. show original
index.html
<script defer src=https://Unpkg.com/processing-js></script>
<canvas data-processing-sources=NoRepeatIdInput.pde></canvas>
window.java
/**
* Name: WindowJS
* Version: 1.2.2
* Language: Java 6
*
* Author: GoToLoop
* Date: 2015/Sep/15
* License: LGPL 2.1
*/
This file has been truncated. show original
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