Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1547
  • Last Modified:

Change Font in JOptionPane

Is it possible to change the default font used in JOptionPane?

This is my code.....

int n = JOptionPane.showConfirmDialog(
      this,
      "Hello",
      "Welcome",
      JOptionPane.PLAIN_MESSAGE,
      JOptionPane.DEFAULT_OPTION,
      icon);

Or do I have to create my own Dialog?
0
mattpeak
Asked:
mattpeak
1 Solution
 
shji1Commented:
The static invokation will not allow you to set the Font.
I solved it with this code:

public static void main(String[] args) {
    JOptionPane pane =
        new JOptionPane(
            "Hello, how are you",
            JOptionPane.PLAIN_MESSAGE,
            JOptionPane.DEFAULT_OPTION);
    Font font = new Font("Courier New",Font.BOLD,14);
    setFontRecursive(font,pane);
    JDialog dialog = pane.createDialog(null, "Welcome");
    dialog.show();
}
     
private static void setFontRecursive(Font f, JComponent component) {
    component.setFont(f);
    Component comp[] = component.getComponents();
    if (comp.length == 0) {
         return;
    }
    for (int i = 0; i < comp.length; i++) {
        if (comp[i] instanceof JComponent) {
            setFontRecursive(f, (JComponent) comp[i]);
        }
    }
}
0
 
CleanupPingCommented:
mattpeak:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
jimmackCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept shji1's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now