Can I use IF statements with a message dialog in java? - java

So, I've been stumped on this piece of code for about a week now, I want to have the code sending an error message when the user chooses 'No' or 'Cancel' however I get an error which tells me that NO and CANCEL are not variables. Does anyone have any suggestions as to how I can overcome this problem?
int mc = JOptionPane.QUESTION_MESSAGE;
int bc = JOptionPane.YES_NO_CANCEL_OPTION;
int ch = JOptionPane.showConfirmDialog (null, "Select:", "Title", bc, mc);
if (bc == NO)
{
JOptionPane.showInputDialog("Sorry, you cannot continue without agreeing to the rules.");
}
else if (bc == CANCEL)
{
JOptionPane.showInputDialog("Sorry, you cannot continue without agreeing to the rules.");
}
else
{
JOptionPane.showInputDialog("Thank you, you may continue!");
}

This is all explained in the JOptionPane Javadoc.
In the code that you've given, the button that you've clicked is identified by the return value from showConfirmDialog, which you've assigned to ch, not bc. In your case, the logic should be
if (ch == JOptionPane.NO_OPTION) {
...
}
else if (ch == JOptionPane.CANCEL_OPTION) {
...
}
else {
...
}

Related

Do -While loops: Java

Good Day Everyone,
I've been having a problem with this bit of code for a while now. I'm making a little game and the gist of this little code fragment is that I want to purposefully lock the persons into this pop-up box until they answer yes. However I continuously receive the error message saying that 'while expected'. I've been thinking for about a day now but I cannot figure a way out to answer this question, can someone help me please?
Kind Regards,
Zeno
int choice = JOptionPane.showConfirmDialog(null, "Will you fight?",
"warning", JOptionPane.YES_NO_OPTION);
do {
/ other code /
}
while (choice
!= 0) {
choice = JOptionPane.showConfirmDialog(null, "Will you fight?",
"warning", JOptionPane.YES_NO_OPTION);
}
}
while (ehealth > 0) {
}
}
}
This is a very simple syntax issue, refer to https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html for formatting.
So it might look more like this:
boolean loopedOnce=false;
do {
//execute *other* code once
//and continues to loop through until choice!=0
if(loopedOnce) getFightingChoice(); //Method for JPane code
loopedOnce=true;
} while(choice!=0);
And then continue on in your program with
while(ehealth>0) {
//more code
}

Using JOptionPane.YES_NO_OPTIONS

Here's my problem, I'm trying to get this bit of code to work so that in my GUI, when I click on yes, the product is added (that bit of code is still to be developed) and the addproductwindow closes, but when no is clicked the JOptionPane closes, but the add product window remains open.
(the System.out.print(" no ");) was me just testing what came out from the inputs.
#Override
public void actionPerformed(ActionEvent e) {
int dialogButton = JOptionPane.YES_NO_OPTION;
JOptionPane.showConfirmDialog (null, "Do you want to add Product: ","Confirmation",dialogButton);
if (dialogButton == 1){
System.out.println(" no ");
} else {
addProductWindow.dispose();
}
}
When using JOptionPane.showConfirmDialog (...) you need to check which button was clicked by the user.
The basic code is:
int result = JOptionPane.showConfirmDialog (...);
if (result == JOptionPane.YES_OPTION)
// do something
Read the section from the Swing tutorial on How to Use Dialogs for more information and working examples.
if (dialogButton == 1)
Don't use "magic numbers". Nobody knows what "1" means. The API will have variable for you do use that are more descriptive.
For addProductWindow to close, you have to call addProductWindow.dispose() method in the if block too.
#Override
public void actionPerformed(ActionEvent e) {
int dialogButton = JOptionPane.YES_NO_OPTION;
JOptionPane.showConfirmDialog(null, "Do you want to add Product: ", "Confirmation", dialogButton);
if (dialogButton == JOptionPane.YES_OPTION) {
addProductWindow.dispose(); // you forgot this
} else {
System.out.println(" no ");
addProductWindow.dispose();
}
}

AND operator is not working for onKeyUp method

Hi, I wish to know why the AND operator is not working here? Even though without operator it working fine what is wrong while including it?
else if (keyCode == KeyEvent.KEYCODE_CTRL_LEFT && keyCode == KeyEvent.KEYCODE_S) {
int visibility=new_Sell_order_cart.getVisibility();
if(visibility==View.VISIBLE)
{
openCart();
}
}
If its not possible how jQuery developer Achive this
if (e.keyCode == 65 && e.ctrlKey) {
alert('ctrl A');
}
});
keyCode cannot be two keys at once. It can be either CTRL_LEFT or KEYCODE_S.
This statement returns false because either one will be true but not both.
(Its like a==3 && a==4, a cannot be 3 and 4 at the same time)
keyCode == KeyEvent.KEYCODE_CTRL_LEFT && keyCode == KeyEvent.KEYCODE_S
Probably you are looking for OR (||) operator:
else if (keyCode == KeyEvent.KEYCODE_CTRL_LEFT || keyCode == KeyEvent.KEYCODE_S) {
EDIT:
If you want to check for two keypress. Here is the solution suggested in this post.
One way would be to keep track yourself of what keys are currently
down.
When you get a keyPressed event, add the new key to the list; when you
get a keyReleased event, remove the key from the list.
Then in your game loop, you can do actions based on what's in the list
of keys.
I got one Solution , I know it is not up-to mark but it full-fill my requirement .
else if (isCTRLisPressed(17) && keyCode == KeyEvent.KEYCODE_S) {
int visibility=new_Sell_order_cart.getVisibility();
if(visibility==View.VISIBLE)
{
openCart();
}
}
and
public boolean isCTRLisPressed(int i){
if(i==17){
System.out.println(" key code of ctrl"+i);
return true;
}
else {
System.out.println("key code in else "+i);
return false;
}
}

Returning from the top of the code in Java,How?

is there any other way to return from the top of the code?
I already tried using Scanner and it worked but now I want to use YES_NO_OPTION in JOption but i dont have any idea to return from the top of the code using this method.
This is the last part of the program:
int selectedOption = JOptionPane.showConfirmDialog(null,"Continue?","Choose",JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.YES_OPTION) {
//What should i put here inside the bracket?
}
if (selectedOption == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null,"Thank you for using");
}
and What should i put in the top of the code to read my Command inside the Bracket?
any help will be appriciated.
It seems like you want a loop:
int selectedOption = JOptionPane.YES_OPTION; // by default
while (selectedOption == JOptionPane.YES_OPTION) {
// do your stuff
selectedOption =
JOptionPane.showConfirmDialog(null,"Continue?","Choose",JOptionPane.YES_NO_OPTION);
}

More Trouble with 'do while' loops, Java

How do I get my program to prompt the user for different choices until they decide to purposefully exit the program? Right now I tried a do-while loop with a boolean called 'exit,' trying to say that while exit is false keep prompting the user to choose what to do, though all it does is ask them once and after it has done what the user wanted, the application stops.
Here is my code:
boolean exit = false;
do {
int options = 0;
do {
options = Integer.parseInt(JOptionPane.showInputDialog(null,
"Would you like to:"
+ "\n(Enter number value of option you would like to choose.)\n"
+ "\n1. See your recommendations. \n2. See top rated books."
+ "\n3. See random books of the day. \n4. Exit"));
} while (logIn < 1 || logIn > 4);
if (options == 1) {
recommend.displayRecommendations(custIndex);
} else if (options == 2) {
calculations.displayTopTen();
} else if (options == 3) {
calculations.displayRandomBooks();
} else if (options == 4) {
exit = true;
System.exit(0);
}
} while (exit = false);
Any help would be appreciated.
this be your problem:
while (exit = false);
Look very carefully...
You are checking a logIn variable but the correct one should be options. Change:
while (logIn < 1 || logIn > 4)
to:
while (options < 1 || options > 4)
In addition, the while (exit = false) (attempted assignment) must be while (exit == false) (comparison) or, even better, while (!exit)
And, lastly, though it doesn't matter that much, it's a bit superfluous to set exit = true immediately before calling System.exit().

Categories