Just wondering if anyone knows how to set the cancel button of an input dialog message to perform an action in Java.
I specifically want it to end the program, but I’m not sure how I would specifically get the button to do something.
Any ideas?
Just wondering if anyone knows how to set the cancel button of an input dialog message to perform an action in Java.
I specifically want it to end the program, but I’m not sure how I would specifically get the button to do something.
Any ideas?
What are you using? Typically there is a DestroyWindow() or CloseWindow() or exit() call you can make.
Just BlueJ
I’m not sure how I would put that method in there though
JavaFX?
Okay so it looks like there’s some code like this:
JOptionPane.OK_CANCEL_OPTION);
but I don’t know how to put it in, and I don’t know how I would add what would happen
Its been a while since I messed around in Java, but a quick search brought this up.
Object[] options = {"Yes, please", "No way!"};
int n = JOptionPane.showOptionDialog(frame,
"Would you like green eggs and ham?",
"A Silly Question",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if (n == JOptionPane.YES_OPTION) {
} else if (n == JOptionPane.NO_OPTION) {
} else {
}
Looks like you just need to get the return value from your dialog, see what it is, and do something.
I think I found an example in my book on page 711.
Okay. Weird. It looks like it will only accept and integer. trying to do a String will not work. I have to figure out what to do.
Nevermind. Fuck this. I can’t even get a basic program to work. It’s JOptionPane too. Can’t even get a damn cancel option to work.
Is it a ‘showInputDialog’? since I think that is the only one that returns a string.
basically would be something like…
String strResponse = JOptionPane.showInputDialog("Enter something: ");
if (strResponse == null) {
// strResponse is null so Cancel was pressed, close program.
}
else {
// Ok was pressed, check / do something with strResponse value.
}
I have been away from the GUI side of Java for a while (I hate programming GUIs), but if your button is tied to a listener, then you should be doing something based on the event onClick() or something like that. Maybe I am showing my age here as I know that there are new helper things that were introduce in JDK 7, 8, and 9 but I have not built GUI stuff since 6.
Yeah it’s showInputDialog
I tried that and it didn’t work.
How exactly is it not working? Could you post the chunk of code for what you are doing, I might get a better idea of what is going on.
/**
* imports
*/
import javax.swing.JOptionPane;
//import java.util.Scanner;
public class Main2
{
public static void main (String [] args)
{
//keyboard entry
//Scanner kE = new Scanner(System.in);
//available password characters
String aPC =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`-=[]\\;',./!@#$%^&*()~_+{}|:\"<>?";
//password length
int pL = 0;
//keyboard input (this is for the GUI/JOptionPane)
String kI = "";
do
{
kI = JOptionPane.showInputDialog(null, "Enter the password length. ");
try
{
pL = Integer.parseInt(kI);
if (pL <= 0)
{
JOptionPane.showMessageDialog(null, "Enter a positive integer. ",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Enter a positive integer. ",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
while (pL <= 0);
/**
* This is the user's entry of random numbers set as an array with a maxmium but no known numbers yet
*/
int [] uERA = new int [pL];
for (int cV1 = 0; cV1 < uERA.length; cV1++)
{
int c = cV1 + 1;
kI = JOptionPane.showInputDialog(null, "Enter random number " + c);
//do
try
{
pL = Integer.parseInt(kI);
if (pL < 1)
{
JOptionPane.showMessageDialog(null, "Enter a positive integer. ",
"Error", JOptionPane.ERROR_MESSAGE);
/**
* Stops the incrementing/continuing of the current loop to try and get the answer
*/
cV1--;
}
if (pL > 93)
{
JOptionPane.showMessageDialog(null, "Enter a positive integer, one to 93. ",
"Error", JOptionPane.ERROR_MESSAGE);
cV1--;
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Enter a positive integer. ",
"Error", JOptionPane.ERROR_MESSAGE);
cV1--;
}
//while ()
}
//final corresponding password
String fCP = "Jmiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii";
/**
* Prints the string which is their password.
*
* JOptionPane does NOT allow you to copy the password regardless
*/
JOptionPane.showMessageDialog(null, "Your corresponding password is " + fCP, "Information", JOptionPane.INFORMATION_MESSAGE);
}
}
I added the following after each of the showInputDialog
and before each of the try
blocks,
if (kI == null) { System.exit (0); }
It closes when pressing cancel
, and just keeps asking if to enter information when pressing ok
with no data.
Thank you!
I’m surprised my book does not teach this.
Most of the basics are just… either pick them up from auto-generated code or die dumb.
Same.
I feel like I’m dying dumb at this point. I’m just now trying to get a confirmation joptionpane message on whether they are sure or not they want to quit and I can’t get it to work as a non error message to appear.
I think I should start with a deliberate dedicated separate window with dedicated buttons for easier and more control. I think it just makes more sense at this point.