Reputation: 144
I would like to increase the button
size of a JOptionePane
. The code for the JOptionPane
of the button
is:
addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent evt) {
if (JOptionPane.showConfirmDialog(rootPane, "¿Desea salir de la aplicación?",
"Gestor de clientes", JOptionPane.ERROR_MESSAGE) == JOptionPane.ERROR_MESSAGE) {
dispose();
Login login = new Login();
login.setVisible(true);
}
}
});
How can I increase the font of the text and the size of the button
?
Upvotes: 1
Views: 282
Reputation: 9202
Another functional way to do this would be to indeed utilize the JOptionPane#showOptionDialog() method (as already indicated) instead of the JoptionPane#showConfirmDialog(). The showOptionDialog()
method allows you to have more than an ample number of custom buttons you might desire.
I think the easiest way to customize the message area and the buttons text, color, and size is to use basic HTML strings instead of Plain Text strings. This is typical in message areas for all JOptionPane dialogs, You can even add small images to your text this way if you like. As far as I'm concerned, it opens a whole new world of flexibility to how your Dialog boxes can be presented to the end User. If you really want to get down and dirty, create your own JDialog option window dialog to look and feel exactly the way you want.
For what you currently want to do (with custom buttons), the JOptionPane#showOptionDialog() method would be enough, for example (in english):
Here is the code to display the above Confirmation Type dialog:
// The dialog box title
String title = "Exit Application?"; // HTML won't work here :(
// The dialog's Message area - Basic HTML can be used here. :)
String message = "<html><font size='4'>Are you Sure"
+ " you want to exit this application?</font><br><br><center>"
+ "<font size='5',color=blue><b>My Application Name</b>"
+ "</font></center><br></html>";
/* The desired buttons for the dialog Box. - Basic HTML can be used here. :)
The buttons are automatically sized based on the font size of the text
applied to the button Text (caption). */
String[] optionButtons = {
// The YES Button.
"<html><font size='5',color=red> Yes </font><br>"
+ "<center><font size='1',color=gray>....</font></center</html",
// The NO button.
"<html><font size='5'>No! Not Now</font><br>"
+ "<center><font size='1',color=gray>....</font></center</html",
// The NOT SURE button. :)
"<html><font size='5'>Well, Not Sure ◔̯◔</font><br>"
+ "<center><font size='2'>(I think)</font></center</html"};
// Display the JOptionPane dialog.
/* The Swing Component that will be parent for this Dialog.
Use 'this' if the class this code is run in an extend JFrame.
If the code is run through an action event of a JButton
then just provide the variable name for that button. If
worse comes to worse...just supply: null. */
java.awt.Component comp = (java.awt.Component) null;
int result = JOptionPane.showOptionDialog(comp, message, title,
int result = JOptionPane.showOptionDialog(comp, message, title,
JOptionPane.DEFAULT_OPTION, // Button Options - DEFAULT_OPTION - moot here.
JOptionPane.QUESTION_MESSAGE, // The internal image to use.
null, // No custom icon
optionButtons, // Button Captions (Text)
optionButtons[1] // Default focused button - NO is made to have default focus.
);
switch (result) {
// Yes button selected
case 0:
System.out.println("You selected: Yes! Please");
break;
// No button selected
case 1:
System.out.println("You selected: No! Not now.");
break;
// Don't Know button selected
case 2:
// Just a little decision making between Yes and No.
while (result == 2) {
result = new java.util.Random().nextInt(2);
System.out.println(result);
}
// Display the Decision _ Ternary Operator used here.
System.out.println("You selected: Well, Not Sure - Decided this one for you, " +
(result == 0 ? "Yes selected!" :
result == 1 ? "No Sselected!" :
"Dialog Canceled (Closed)"));
break;
// Dialog simply closed from title bar.
default:
System.out.println("None selected - Dialog Canceled (Closed)");
}
In the above code, the JOptionPane actually returns the index value of the button selected within the dialog and the dialog is properly closed upon that button selection (as it should be).
About the HTML tags used in the code above:
When using HTML strings you need to always start the string with the <html>
and end the overall string with the closing </html>
tag.
<br>
tag.
<b>
and </b>
tags.
<u>
and </u>
tags.
<font>
and
</font>
tags.
<center> and </center>
tags.
- Entity - A Non-Breaking Space.
There is a lot you can do to spice up your Message Boxes and text in certain swing components like JLabels and JButtons, etc (including ToolTips) by using HTML.
Upvotes: 2
Reputation: 2169
This code can help you with showOptionDialog :
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class CustomizedJOption {
public static void main(String[] args) {
//Confirm button
JButton btnYes = new JButton ( " OK ");
btnYes.setFont(new Font("young circle", Font.BOLD, 30));
btnYes.setForeground(Color.MAGENTA);
//Negative button
JButton btnNo = new JButton ( " No ");
btnNo.setFont(new Font("young circle", Font.ITALIC, 30));
btnNo.setForeground(Color.blue);
//Add button options to the array
Object [] options = {btnYes, btnNo} ;
//text content
JLabel label = new JLabel ( "\"¿Desea salir de la aplicación?\"");
label.setForeground(Color.BLUE);
label.setFont(new Font("young circle", Font.ITALIC, 30));
//Display Dialog
JOptionPane .showOptionDialog(null, label, "Title", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options, options[0]);
}
}
Upvotes: 1