Search code examples
javajoptionpane

Change text size of JOptionPane.showInputDialog options


I'm trying to change the font size of the possiblities that I have provided in the following code:

    font = new Font("Arial", Font.PLAIN, 20);

    UIManager.put("OptionPane.messageFont", font);
    UIManager.put("OptionPane.buttonFont", font);

    Object[] possibilities = {"100%", "80%", "20%"};
    selected = (String)JOptionPane.showInputDialog(
                        frame,
                        "Select the accuracy level.",
                        "Accuracy",
                        JOptionPane.PLAIN_MESSAGE,
                        null, possibilities,
                        "100%");

As you can see, I know how to change the message font and button font, I just can't change the font of the options themselves.


Solution

  • Keep in mind, changing component properties through the UIManager will also affect all future JOptionPanes as well that utilize those properties changed. It's best just to deal with the JOptionPane at hand by simply creating a custom panel for the dialog.

    In the example code below we use a custom Confirm Dialog Box to act as a Input Box. Here is how you can do it:

    // Your desired Accuracy choices for dialog combox.
    Object[] possibilities = {"20%", "80%", "100%"};
    
    //The panel to display within the Dialog
    JPanel jp = new JPanel();
    jp.setLayout(new BorderLayout());   // Panel layout manager
    
    // JLabel to hold the dialog text. HTML is used to add pizzaz. :)
    JLabel jl = new JLabel(
            "<html>Select the desired <font color=blue><b>Accuracy Level</font>:"
            + "</b><br><br></html>");  
    // Desired font, style, and size for Message
    Font font = new Font("Arial", Font.PLAIN, 14); 
    jl.setFont(font);    // Set the font to JLabel (the msg)
    jp.add(jl, BorderLayout.NORTH);     // Add JLabel to top of Dialog
    
    // JComboBox to hold all the available Accuracy choices.
    JComboBox jc = new JComboBox(possibilities);
    jc.setSelectedIndex(2);  // Set 100% as default in Combo
    // Desired font, style, and size for combo items
    font = new Font("Arial", Font.PLAIN, 20); 
    jc.setFont(font);   // Set the font to combo
    jp.add(jc, BorderLayout.SOUTH);      // Add JComboBox to Bottom section of Dialog
    
    String valueSelected;  // Variable to hold the combo selected value.
    
    /* Display the custom Input Box Dialog which is actually a
       customized Confirm Dialog Box with the above JPanel supplied
       as the message content.  Also, if the OK button was selected
       then fill the valueSelected string variable declared above 
       with the Combo selection. 100% has been set as default.*/
    if (JOptionPane.showConfirmDialog(this, jp, "Accuracy", 
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)  == 0) {
        valueSelected = jc.getSelectedItem().toString();
        System.out.println("Accuracy Selected Is: " + valueSelected);
    }
    else {
        System.out.println("Input Canceled");
    }
    

    Ultimately, when this code is encountered a custom Input Dialog something like this will be displayed:

    enter image description here

    If the OK button is selected without any selection then Accuracy Selected Is: 100% will be displayed within the Console Window. On the other hand, if the Cancel or Title Bar Close button [x] is selected then Input Canceled! is displayed within the Console Window.

    If 20% is selected from the Combo Box within the dialog and the OK button is selected then Accuracy Selected Is: 20% is displayed within the Console Window.

    enter image description here

    On another note, if you want to add a custom Image to your dialog as shown below:

    enter image description here

    Then add this line to the top of the code:

    final ImageIcon icon = new ImageIcon("AccuracyIcon.png");
    

    then change the call line to the dialog to this:

    if (JOptionPane.showConfirmDialog(this, jp, "Accuracy", 
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, icon)  == 0) {
        valueSelected = jc.getSelectedItem().toString();
        System.out.println("Accuracy Selected Is: " + valueSelected);
    }
    else {
        System.out.println("Input Canceled");
    }
    

    Of course you would have to supply the proper path and file name to the image you want to use. If you want to use the image in the example then you can acquire it here (be sure to resize it accordingly - 72x76).

    You can easily see how flexible this can be by doing it this way and...it won't affect any other future JOPtionPanes.