Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the default text of buttons in JOptionPane.showInputDialog

I want to set the text of OK and CANCEL buttons in JOptionPane.showInputDialog to my own strings.

There is a way to change the buttons' text in JOptionPane.showOptionDialog, but I couldn't find a way to change it in showInputDialog.

like image 273
Daniel Briskman Avatar asked Jan 18 '13 21:01

Daniel Briskman


People also ask

What is default data type in JOptionPane?

When I ask a user to enter a quantity for a program I have made using the code below, the default text is 3. String input = JOptionPane.

How do you change the font on JOptionPane?

There is an easy way to change the default font in JOptionPane . Pass a string modified in html format, which means you can either use <font> tag or even CSS. Using <font> tag. Using CSS.

How do you add a button in JOptionPane?

showOptionDialog( panel, "Choose one of the following options for Category " + category + ". \n" + "If skip is available, you may choose it to skip this category.", "Select option", optionType, JOptionPane. INFORMATION_MESSAGE, null, options, options[0]);


2 Answers

The code below should make a dialog appear and you can specify the button text in the Object[].

Object[] choices = {"One", "Two"};
Object defaultChoice = choices[0];
JOptionPane.showOptionDialog(this,
             "Select one of the values",
             "Title message",
             JOptionPane.YES_NO_OPTION,
             JOptionPane.QUESTION_MESSAGE,
             null,
             choices,
             defaultChoice);

Also, make sure to look through the Java tutorials on the Oracle site. I found the solution at this link in the tutorials http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#create

like image 84
Gregory Peck Avatar answered Sep 23 '22 23:09

Gregory Peck


if you don't want it for just a single inputDialog, add these lines prior to creating dialog

UIManager.put("OptionPane.cancelButtonText", "nope");
UIManager.put("OptionPane.okButtonText", "yup");

where 'yup' and 'nope' is the text you want displayed

like image 26
Michael Dunn Avatar answered Sep 22 '22 23:09

Michael Dunn