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");
Related
Say I am making a program that keeps track of people's favorite food. I have a dropdown, as such:
String foods = { "Pizza", "Burgers", "Pasta", "Bacon" };
String favoriteFood = JOptionPane.showInputDialog(null, "What is your favorite food?", "Choice", JOptionPane.QUESTION_MESSAGE, null, foods, foods[0]));
JOptionPane.showMessageDialog(null, favoriteFood);
How do I make a part in the dropdown that is like "Choose now...", but if you click the "Choose now...", it doesn't become your choice? Thank you!
You may do it like this
String[] foods = { "Pizza", "Burgers", "Pasta", "Bacon" };
JComboBox<String> cb = new JComboBox<String>(foods);
cb.getModel().setSelectedItem("Choose now...");
cb.addHierarchyListener(hEv -> {
if((hEv.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && cb.isShowing()) {
JButton ok = SwingUtilities.getRootPane(cb).getDefaultButton();
ok.setEnabled(cb.getSelectedIndex() >= 0);
cb.addActionListener(aEv -> ok.setEnabled(cb.getSelectedIndex() >= 0));
} });
JPanel p = new JPanel(new GridLayout(0, 1, 0, 8));
p.add(new JLabel("What is your favorite food?"));
p.add(cb);
int choice = JOptionPane.showConfirmDialog(null,
p, "Choice", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
JOptionPane.showMessageDialog(null,
choice == JOptionPane.OK_OPTION? cb.getSelectedItem(): "no choice");
The first challenge is to set a (pre)selected value that is not part of the selectable choices. When you call setSelectedItem on a non-editable JComboBox, it will reject any values outside the model. However, we can set the selected value on the model directly, like in cb.getModel().setSelectedItem("Choose now...");
Then, to ensure that we won’t confuse this initial selection with an actual selection, we have to disable the “Ok” button until a choice from the list has been made (cb.getSelectedIndex() >= 0). To get the “Ok” button itself, we wait until the entire AWT hierarchy has been constructed and get the default button.
A possible solution using the JOptionPane is shown below. In this code the JDialog is more or less manually created. The OK button and available options are then pulled from the JOptionPane and the OK button is only enabled when anything other than 'Choose from...' is selected.
String[] foods = new String[]{"Choose now...", "Pizza", "Burgers", "Pasta", "Bacon"};
JOptionPane pane = new JOptionPane("What is your favorite food?", JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION, null,
null, null);
pane.setWantsInput(true);
pane.setSelectionValues(foods);
pane.setInitialSelectionValue(foods[0]);
// create the dialog and select the initial value
JDialog dialog = pane.createDialog( null, "title" );
pane.selectInitialValue();
// find the OK Button and disable it by default
JPanel buttonPanel = (JPanel) pane.getComponent( 1 );
JButton ok = (JButton) buttonPanel.getComponent( 0 );
ok.setEnabled( false );
// find the JComboBox (the panel holding the available options)
JPanel childPanel = (JPanel) ((JPanel) pane.getComponent( 0 )).getComponent( 0 );
JPanel innerPanel = (JPanel) childPanel.getComponent( 1 );
JComboBox options = (JComboBox) innerPanel.getComponent( 1 );
// add an action listener to the JComboBox; enable the OK button if a valid option is selected
options.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if ( options.getSelectedIndex() == 0 ) {
ok.setEnabled( false );
} else {
ok.setEnabled( true );
}
}
});
// show the dialog
dialog.show(); // <--- note this one is deprecated, should probably use: dialog.setVisible( true );
dialog.dispose();
// get the selected value
String value = pane.getInputValue().toString();
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]);
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 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);
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.