I need to have only one button in showConfirmDialog.
I tried this:
int response = JOptionPane.showConfirmDialog(null, "Time Entered Successfully",
"", JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE);
if (response == JOptionPane.CLOSED_OPTION || response == JOptionPane.OK_OPTION)
{
System.out.println("CLOSING>>>>>>");
}
But this shows dialog with Yes_No_option.
I want only OK button to be displayed there. Is it possible?
try using this, it creates only one button
JOptionPane.showMessageDialog(null, "Loading Complete...!!!");
I want only OK button to be displayed there. Is it possible?
Use showOptionDialog() method.
Object[] options = {"OK"};
int n = JOptionPane.showOptionDialog(frame,
"Message here ","Title",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
It's the JOptionPane.DEFAULT_OPTION
JOptionPane.showConfirmDialog(null,
"MESSAGE",
"TITLE",
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE);
Related
Is there any way to change the title of button OK in JOptionPane.showConfirmDialog for example i have a panel and i want to show it inside a JOptionPane.showConfirmDialog like this
MyPanel pan = new MyPanel();
int result = JOptionPane.showConfirmDialog(null, venteFam,
"Title of the panel", JOptionPane.OK_CANCEL_OPTION);
This show me this :
So what i need is to change OK and cancel title to another title, is that possible?
Thank you.
You can use JOptionPane.showOptionDialog, which lets you supply your own texts as an array instead of JOptionPane.showConfirmDialog :
JOptionPane.showOptionDialog(null,
"Do you like this answer?",
"Feedback",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[]{"Yes I do", "No I don't"},
"default");
I need to display an input dialog box and enter the player's name.
If the player clicked the ok button and the value of the input dialog is either a number or null an error should be displayed. If the player clicks the ok button of the error message dialog box, the input box will appear again.
I don't know if my code is wrong. I forgot how to code in java because of my web programming subject.
int[] player = new int[1];
for(int a =0; a<player.length; a++){
String input = JOptionPane.showInputDialog("Enter your Name:",JOptionPane.OK_CANCEL_OPTION);
try {
if(!input.matches("[a-zA-Z]+")){
JOptionPane.showMessageDialog(null,"Use Letters only", "Warning", JOptionPane.OK_OPTION);
} else {
input = String.valueOf(player[a]);
category c = new category();
this.dispose();
c.show();
}
}
catch(Exception e){
};
If you do the loop right, you'll need only one dialog:
String message = "Enter Your Name:";
String playerName = null;
do {
playerName =
JOptionPane.showInputDialog(message);
message = "<html><b style='color:red'>Enter Your Name:</b><br>"
+ "Use letters only.";
} while(playerName != null && !playerName.matches("[a-zA-Z]+"));
System.out.println("PlayerName: " + playerName);
This will include the error message into the next iteration of asking for a valid name. Makes it a bit nicer to work with as well.
Shows like this on first call:
and like this in case of errors:
Note
You might want to change your regex to "[a-zA-Z]\\w+" if numbers later on in the name are okay (for instance "bunny99")
Show an error dialog that displays the message, 'alert':
JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
Show an internal information dialog with the message, 'information':
JOptionPane.showInternalMessageDialog(frame, "information", "information", JOptionPane.INFORMATION_MESSAGE);
Show an information panel with the options yes/no and message 'choose one':
JOptionPane.showConfirmDialog(null,"choose one", "choose one", JOptionPane.YES_NO_OPTION);
Show an internal information dialog with the options yes/no/cancel and message 'please choose one' and title information:
JOptionPane.showInternalConfirmDialog(frame, "please choose one", "information",JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
Show a warning dialog with the options OK, CANCEL, title 'Warning', and message 'Click OK to continue':
Object[] options = { "OK", "CANCEL" };
JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,null, options, options[0]);
Show a dialog asking the user to type in a String:
String inputValue = JOptionPane.showInputDialog("Please input a value");
Show a dialog asking the user to select a String:
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,"Choose one", "Input",JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);
My JOptionPane shows up just fine, but when I click the button, nothing happens. Once I click the 'X', the console will print which of the buttons was clicked. It just won't close when the button is clicked:
final JOptionPane newDocWarning = new JOptionPane("Would you like to save before opening a new file?", JOptionPane.WARNING_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION);
final JDialog newDocDialog = new JDialog(this, "New Document", true);
newDocDialog.setContentPane(newDocWarning);
newDocDialog.setSize(420, 150);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
newDocDialog.setLocation(dim.width/2-newDocDialog.getSize().width/2, dim.height/2- newDocDialog.getSize().height/2);
newDocDialog.setVisible(true);
int value = ((Integer)newDocWarning.getValue()).intValue();
if(value == JOptionPane.YES_OPTION){
System.out.println("YES OPTION WAS CLICKED");
newDocDialog.dispose();
}else if(value == JOptionPane.NO_OPTION){
System.out.println("NO OPTION WAS CLICKED");
newDocDialog.setVisible(false);
}else if(value == JOptionPane.CANCEL_OPTION){
System.out.println("CANCEL OPTION WAS CLICKED");
newDocWarning.setVisible(false);
}
Can you try this one using JOptionPane.showConfirmDialog()
int value = JOptionPane.showConfirmDialog(frame,
"Would you like to save before opening a new file?", "New Document",
JOptionPane.INFORMATION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION);
In this case you don't need to close it. It will be automatically closed once any button is clicked.
For more info have a look at How to Make Dialogs.
Say you have this JOptionPane:
JOptionPane.showOptionDialog(null,
"Do you like this answer?",
"Feedback",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[]{"Yes I do", "No I don't"}, // this is the array
"default");
How would you set an answer to this to display? For example, how would you call the "Yes I do" and how would you call the "No I don't" ? I'm just not sure how to make the buttons do what I want them to do.
The option dialog return an int to determine what button have been pressed
int result = JOptionPane.showOptionDialog(null,
"Do you like this answer?",
"Feedback",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[]{"Yes I do", "No I don't"}, // this is the array
"default");
if(result == 0) { //0 is 'Yes I do' option
//do stuff
}
The static methods won't get you there. You will instead have to rely on the constructor with a similar parameter list:
JOptionPane optionPane = new JOptionPane("Do you like this answer?", OptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, new String[]{"Yes I do", "No I don't"},"default");
optionPane.setVisible(true);
optionPane.setInputValue("Yes I do");
I use:
char[] password = null;
JPasswordField jpf = new JPasswordField(30);
java.lang.Object [] messageInput = { prompt, jpf };
java.lang.Object [] options = { jpf , "OK", "Cancel"};
int result = JOptionPane.showOptionDialog(null, messageInput, title,
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options, "");
JOptionPane.showMessageDialog(null,
result);
if (result == 1) {
password = jpf.getPassword();
}
else if(result == JOptionPane.CANCEL_OPTION)
{
}
return password;
to get password, but this can not listen to the Enter button.
I know if I set the options parameter to null, can make the dialog listen to "enter" button, but if I do that, the dialog don't focus to the textbox when show up.
Can someone help me on this?
I know if I set the options parameter to null, can make the dialog listen to "enter" button, but if I do that, the dialog don't focus to the textbox when show up.
Dialog Focus should help you out.