?
Solved

Change Font in JOptionPane

Posted on 2003-02-25
4
Medium Priority
?
1,532 Views
Last Modified: 2013-12-03
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
Comment
Question by:mattpeak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 1

Accepted Solution

by:
shji1 earned 200 total points
ID: 8019629
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
 

Expert Comment

by:CleanupPing
ID: 9059087
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
 
LVL 15

Expert Comment

by:jimmack
ID: 9671778
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question