So this is my first time working with the JOptionPane and I was wondering if someone could help explain how I could make both of my buttons do certain actions? For all intent and purposes have it just print out "Hi". Here is my code. So far it only prints out "Hi" if I click the "Uhh...." button but I want it to do the same when I click the "w00t!!" button as well. I know it has something to do with the parameter "JOptionPane.YES_NO_OPTION" but I'm not sure what exactly I have to do with it. Thanks for the help in advance!
Object[] options = {"Uhh....", "w00t!!"};
int selection = winnerPopup.showOptionDialog(null,
"You got within 8 steps of the goal! You win!!",
"Congratulations!", JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE, null,
options, options[0]);
if(selection == JOptionPane.YES_NO_OPTION)
{
System.out.println("Hi");
}
From the javadocs,
When one of the showXxxDialog methods returns an integer, the
possible values are:
YES_OPTION
NO_OPTION
CANCEL_OPTION
OK_OPTION
CLOSED_OPTION
So, your code should look something like,
if(selection == JOptionPane.YES_OPTION){
System.out.println("Hi");
}
else if(selection == JOptionPane.NO_OPTION){
System.out.println("wOOt!!");
}
But regardless, this logic is a bit bizarre so I would probably just roll my own dialog.
In JOPtionPane class there are some constants that represent the values of the buttons.
/** Return value from class method if YES is chosen. */
public static final int YES_OPTION = 0;
/** Return value from class method if NO is chosen. */
public static final int NO_OPTION = 1;
/** Return value from class method if CANCEL is chosen. */
public static final int CANCEL_OPTION = 2;
You changed the names of the buttons, and consequently, your first button "Uhh" has the value 0, and its button "w00t!" assumed a value of 1.
So, you can use this:
if(selection == JOptionPane.YES_OPTION)
{
System.out.println("Hi");
}
else if(selection == JOptionPane.NO_OPTION){
// do stuff
}
or maybe could be better use the swicht/case function:
switch (selection )
{
case 0:
{
break;
}
case 1:
{
break;
}
default:
{
break;
}
}
int selection = 0;
JOptionPane.showOptionDialog(null,
"You got within 8 steps of the goal! You win!!",
"Congratulations!", JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE, null,
options, options[0]);
if(selection == JOptionPane.YES_NO_OPTION)
{
System.out.println("Hi");
}
Related
I have made a game in Java that allows you to create and play your own worlds. While creating your world there is a wall entity that you can place. This wall entity can slide, but in order to slide it needs the direction (horizontal/Vertical), distance, and speed. Currently I am using a few JOptionPane to get those values everytime you place one of those blocks. My question is how can I get those values from the user without using a JOptionPane? (If you need any code please let me know).
This is what I have:
private void placeWall() {
int slidable = JOptionPane.showConfirmDialog(null, "Does This Block Slide?", "Slide", JOptionPane.YES_NO_OPTION);
if (slidable == 0) {
String[] options = { "Horizontal", "Vertical"};
int direction = JOptionPane.showOptionDialog(null, "Choose Direction", "Direction", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, 0);
int speed = getSpeed();
int distance = getDistance();
if (direction == 0)
wallSlidables.add(new SlideHorizontal(speed, distance));
else
wallSlidables.add(new SlideVertical(speed, distance));
}
}
Source Code: GitHub
I want to capture the ok button event when the "OK" button is pressed in on a JOptionPane. I then want to display a jframe. I've found numerous tutorials and videos on capturing all sorts of events except for the JOptionPane. The Java docs are not much help for a newbie. Hoping someone can help. I have the following.
JOptionPane.showMessageDialog(frame,
"Press OK to get a frame");
How do I implement the listener to capture the OK pressed event.
private class Listener implements ActionListener {
public void
actionPerformed(ActionEvent e) {
}
}
There's no need to capture it -- code flow will return immediately post the JOptionPane display line. If you want to know if OK vs cancel vs delete the window was pressed, then use a different JOptionPane -- use the JOptionPane.showConfirmDialog(...), and capture the result returned from this method call.
String text = "Press OK to get a frame";
String title = "Show Frame";
int optionType = JOptionPane.OK_CANCEL_OPTION;
int result = JOptionPane.showConfirmDialog(null, text, title, optionType);
if (result == JOptionPane.OK_OPTION) {
//...
}
You can't do it with the showMessageDialog method. You have to use the showConfirmDialog method instead. This will return you a value on which you can determine the button that was pressed:
int result = JOptionPane.showConfirmDialog(frame, "Press OK to get a frame");
if (result == JOptionPane.YES_OPTION) {
// Yes button was pressed
} else if (result == JOptionPane.NO_OPTION) {
// No button was pressed
}
To get the OK Button you need to use OK_CANCEL_OPTION:
int result = JOptionPane.showConfirmDialog(frame, "Press OK to get a frame",
"Title", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
// OK button was pressd
} else if (result == JOptionPane.CANCEL_OPTION) {
// Cancel button was pressed
}
I have created a JDialog box to display some warning massage to the users. When the user gets the warning massage and if he clicks on yes button the application do some work and if the user selects no then he remains on the same place.
I am getting 0 value for yes and 1 value for no. But the JDialog box giving same result for both yes and no button i.e, application is getting close for both inputs. But what i wanted is if the user selects yes then the application do some thing and if he selects no then nothing happen(the UI window remain open).
public void warningMassage(String Text) {
int n = JOptionPane.showConfirmDialog(frame, Text, "Warning", JOptionPane.YES_NO_OPTION);
System.out.println(n);
if(n == 0){
System.exit(0);
System.out.println(JOptionPane.YES_OPTION);
} else {
frame.setVisible(true);
}
}//warningMassage
Use the below code
public void warningMassage(String yourText){
int n = JOptionPane.showConfirmDialog(
frame,
yourText,
"Warning",
JOptionPane.YES_NO_OPTION);
System.out.println(n);
if(n == JOptionPane.YES_OPTION){
frame.setVisible(true);
}
else{
System.exit(0);
System.out.println(JOptionPane.YES_OPTION);
}
}//warningMassage
A YES returns 0 and a NO returns 1
Documentation for the JDialog Box
Use
if(n==JOptionPane.YES_OPTION)
//yes pressed
else
//no pressed
JDialog box is not giving same result for both yes and no inputs
. if you choose No the frame does not close untill you press
on terminal or you explicitly set
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
As said by #Cool Guy I also prefer to use
if(n==JOptionPane.YES_OPTION)
//yes pressed
else
//no pressed
but currently at this time your problem is not this .Do Post your Main Class
JOptionPane.showConfirmDialog() with JOptionPane.YES_NO_OPTION can return:
JOptionPane.YES_OPTION: public static final int YES_OPTION = 0;
JOptionPane.NO_OPTION: public static final int NO_OPTION = 1;
You should use those const in your check:
if (JOptionPane.showConfirmDialog(...) == JOptionPane.YES_OPTION) {
// do something
}
However, a small hint: if you want to display a warning message, consider to use this:
JOptionPane.showConfirmDialog(
parentComponent,
message,
title,
optionType,
messageType
);
where messageType is an integer designating the kind of message this is; primarily used to determine the icon from the pluggable Look and Feel: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE.
Then, in your case:
JOptionPane.showConfirmDialog(
null,
"message",
"TITLE",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE// <--- !!
);
I'm looking for a solution for my problem. I created a class that customize a TextField and I'm using TextChangeListener to change the value of field. Every number that user enter the change(format) is taken. Works, but the change is very slow I want it to be faster.
Here's how I'm doing.
public class CpfField extends TextField implements TextChangeListener{
private final StringBuilder CPF = new StringBuilder();
public CpfField(){
super("CPF");
setImmediate(true);
setMaxLength(14);
addTextChangeListener(this);
}
//change(format) values
#Override
public void textChange(TextChangeEvent event) {
if(!event.getText().trim().isEmpty()){
if(event.getText().length() == 3){
CPF.append(event.getText());
CPF.insert(3,".");
}else if(event.getText().length() == 7){
CPF.setLength(0);
CPF.append(event.getText());
CPF.insert(7,".");
}else if(event.getText().length() == 11){
CPF.setLength(0);
CPF.append(event.getText());
CPF.insert(11,"-");
}else{
CPF.setLength(0);
CPF.append(event.getText());
}
}else{
CPF.setLength(0);
setValue("");
}
setValue(CPF.toString());
}
}
How to change values faster ?
Setting the TextChangeEventMode to EAGER would result to trigger the event after each key is pressed. That seems often a bit too fast and too much overhead.
A TextChangeEvent is triggered when there is a pause in editing the text. The length of the pause can be modified with setInputEventTimeout().
Try to set an appropriate InputEventTimeout.
Hint:
If a ValueChangeEvent would occur before the timeout period, a
TextChangeEvent is triggered before it, on the condition that the text
content has changed since the previous TextChangeEvent.
Take a look here: https://vaadin.com/book/-/page/components.textfield.html under 5.8.4. Text Change Events. By default text change event mode is LAZY. What you want to do is probably:
setTextChangeEventMode(TextChangeEventMode.EAGER);
My JOptionPane code is as follows:
selectedSiteName = JOptionPane.showInputDialog("Enter the name of the new site:");
This renders out an input with a textbox and an OK and Cancel button. I need to detect if Cancel was clicked.
Cheers.
Check if selectedSiteName == null . This will be the case if the user clicks Cancel or closes the dialog.
Read the JOptionPane API and follow the link to the Swing turorial on "How to Use Dialogs" for a working example.
if(selectedSiteName == JOptionPane.CANCEL_OPTION)
{
}
should work.
JOptionPane extends JComponent.
Methods of JOptionPane
1) .showMessageDialog(); // VOID :-(
2) .showInputDialog(); // return STRING :-)
3) .showConfirmDialog(); // return int :-)
-> and more...
Example:
void myMethod() {
JDialog jd = new JDialog();
jd.setDefaultCloseOperation(1);
JOptionPane jop = new JOptionPane();
int val = jop.showConfirmDialog(jd, "Hello");
if(val == 0) jop.showMessageDialog(null, "Success", "INFO", jop.INFORMATION_MESSAGE);
System.out.println(val);
jd.add(jop);
}
Helpful link:
- Why does JOptionPane.getValue() continue to return uninitializedValue
- https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html