Question

The line of code below displays a dialog box with two buttons: Yes and No. I want those two buttons to be at least 3 times the actual default size. I understand I can create a customized JFrame and add button and set the Size but I have scores of dialog boxes; does not seems practical.

JOptionPane.showConfirmDialog(null, "Did you eat", "Confirmation", JOptionPane.YES_NO_OPTION);

The reason why I want to make the buttons larger is because I increased the font size of the dialog boxes (one line of code impacted all the dialog boxes in my code). And the small (default) size of the buttons look akward now compared to the the verbage.

So how can I manipulate JOptionPane dialog box's button's sizes?

Was it helpful?

Solution

Adding this before the dialog boxes, will change the font of the button's text; thereby, inreasing the button's dimensions. Make sure to import this:

import java.awt.Font; 
import javax.swing.plaf.FontUIResource; 




UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("ARIAL",Font.PLAIN,35))); 

If want to have different font for a specific dialog box, and then go back to the one you were using, all you have to do is put that line of code and change the font size. then after the dialog box, put the original one back. It kind of overrides it each time you do it.

OTHER TIPS

You will have to create an instance of JOptionPane and set a new PreferredSize(). Using setSize() does not work as expected. For example:

public static void showMessageBox(final String strTitle, final String strMessage)
{
        //Redone for larger OK button
         JOptionPane theOptionPane = new JOptionPane(strMessage,JOptionPane.INFORMATION_MESSAGE);
         JPanel buttonPanel = (JPanel)theOptionPane.getComponent(1);
        // get the handle to the ok button
        JButton buttonOk = (JButton)buttonPanel.getComponent(0);
        // set the text
        buttonOk.setText(" OK ");
        buttonOk.setPreferredSize(new Dimension(100,50));  //Set Button size here
        buttonOk.validate();
        JDialog theDialog = theOptionPane.createDialog(null,strTitle);
        theDialog.setVisible(true);  //present your new optionpane to the world.

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top