Results 1 to 11 of 11
  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question Fonts (Changing Fonts and Color's)

    I am trying to make it so that the following code changes fonts and color

    Java Code:
    if (outputBmi < 18.5) {
                                txtResults.setText("Underweight");
                                //Needs to be orange
                                txtResults.setFont(new Font("Serif", BOLD, 18));
                                    } else{
                                        if (outputBmi > 24.9){
                                           txtResults.setText("Overweight");
                                           //Needs to be red
                                            } else
                                                txtResults.setText("OK");
                                                //needs to be green
    The line
    Java Code:
    txtResults.setFont(new Font("Serif", BOLD, 18));
    gives me an error, it doesnt like BOLD, I thought that the import java.awt.*; contained the library for fonts so surely BOLD should not casue this problem?

    I am also looking to change the colour as per comments under each statement.

    Thanks in advance for any assistance

  2. #2
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    After searching the web and going through books I have tried several things, the latest is

    Java Code:
    Font font3 = new Font("Papyrus",Color.ORANGE, Font.BOLD,20);
    this doesnt like Font, yet when I have

    Java Code:
    Font font2 = new Font("Ariel", Font.BOLD, 17);
    it accepts this

    can someone advise as to whether I am making this more difficult than it needs to be?


    Thanks

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    After searching the web and going through books I have tried several things,...
    Have you reviewed the Font API? That's where I'd start. Reviewing this shows no constructor that accepts a Color parameter.

    What is "txtResults", a JLabel? If so, then perhaps you want to set its foreground color via setForeground(Color.YELLOW)?

  4. #4
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    txtResults is the text field that displays 'ok', 'underweight' or 'overweight'

    I am currently looking at the link you suggested , ermm ok, i admit it, im lost, I dont see anything to tell me what I want to do.

  5. #5
    pheonix is offline Member
    Join Date
    Apr 2008
    Posts
    42
    Rep Power
    0

    Default

    looool b4 any1 posted a reply to this thread i was answering your question, but my laptop ran out of battery and it closed :P .. Anyway ,,

    to change the font you need Font.BOLD instead of just BOLD but i see you figuered that out .

    To change the color of the text :

    Java Code:
    txtResults.setForeground(Color.RED);  // or any other color
    or u can

    Java Code:
    Color c = new Color(int,int,int); // and define the values

  6. #6
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    Thats great, thanks for your help, glad your battery didnt take to long to recharge...lol

  7. #7
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    They are working apart from the overweight, this is printing out in green rather than red.

    Java Code:
    if (outputBmi < 18.5) {                                                        
                                txtResults.setText("Underweight"); 
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
                                    } else{
                                        if (outputBmi > 24.9){
                                           txtResults.setText("Overweight");
                                           txtResults.setForeground(Color.RED);
                                           txtResults.setBackground(Color.BLACK);
                                            } else
                                                txtResults.setText("OK");
                                                txtResults.setForeground(Color.GREEN);
                                                txtResults.setBackground(Color.BLACK);
                                                //needs to be green
                          }

  8. #8
    pheonix is offline Member
    Join Date
    Apr 2008
    Posts
    42
    Rep Power
    0

    Default

    Green !!!? huh .. i have never encountered anything like that :P .. BUT try putting curly brackets in the last else statement . coz usually when u dont put brackets after an if or an else statement in java, java only reads the next line .. so ...
    Java Code:
    else
    Statement1 // only statement1 executes
    Statement2
    Java Code:
    else{           // here both statement1 & 2 are executed
    Statement1
    Statement2 
    }
    I hope u understood what I meant :)

    lol just to be clear ill show u in ur example


    Java Code:
    if (outputBmi < 18.5) {                                                        
                                txtResults.setText("Underweight"); 
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
                                    } else{
                                        if (outputBmi > 24.9){
                                           txtResults.setText("Overweight");
                                           txtResults.setForeground(Color.RED);
                                           txtResults.setBackground(Color.BLACK);
                                            } else [COLOR="Lime"]{[/COLOR]
                                                txtResults.setText("OK");
                                                txtResults.setForeground(Color.GREEN);
                                                txtResults.setBackground(Color.BLACK);
                                                //needs to be green
    [COLOR="lime"]}[/COLOR]
                          }
    u 4got these brackets

  9. #9
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    lol, thanks for that, those braces get me every time

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by dbashby View Post
    I am currently looking at the link you suggested , ermm ok, i admit it, im lost, I dont see anything to tell me what I want to do.
    With practice, you will find the API to be your best friend, and if you are going to be coding in Java, you need this friendship. I strongly suggest that you start studying the API daily so that you will learn how to use it. If you do this, you will get better at using it, and you will be able to answer your own questions much quicker and more easily than you can get answers here.

    Best of luck.

  11. #11
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question

    So having the colors displaying as required, I have a further question

    I have a optionPane that if the user clicks Help will get the instruction of the use of the app

    Within those instructions from the information that has already been given here can someone tell me if it is possible to format the text within this optionPane?

    I am looking to center & color headings etc

    I would think it is but before I try I just want to know if it is possible.

    I am still trying to digest the link you gave me fubarable, thanks but at the moment its over my head, I will keep reading it though

    Thanks

Similar Threads

  1. how to get raw image data for fonts
    By Nicholas Jordan in forum Java 2D
    Replies: 8
    Last Post: 04-05-2009, 06:15 AM
  2. its not changing bgcolor
    By javanoobita in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 02:29 PM
  3. Replies: 0
    Last Post: 06-22-2008, 11:06 PM
  4. Replies: 0
    Last Post: 11-06-2007, 06:58 AM
  5. Replies: 3
    Last Post: 07-16-2007, 11:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •