Results 1 to 6 of 6
  1. #1
    carl is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default Change the color in my program

    Hi, I am trying to learn Java but i have some problems.
    I want to know how i can show dynamically the fonts provided by my operating System in a choice Fonts =new Choice()?

    I want also need a choice for changing the colors of a string... I know how to create some colors in the choice...
    Java Code:
    Choice Fontcolor=new Choice();
    Fontcolor.addItem("Red");
    Fontcolor.addItem("Blue");
    Do you know something better than this? For example something that i can use it immediately for changing the color.... I was thinking if it is possible instead of text colors ..... to use colored lines in the choice... Is this possible?

    Finally how i can change the color of a String?
    I have a String inside a TextArea...i can;t find any option for this? Any suggestion?
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The GraphicsEnvironment class has a couple of handy methods you can use to get the fonts and their names.
    Java Code:
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font[] fonts = ge.getAllFonts();
    String[] names = ge.getAvailableFontFamilyNames();
    is possible instead of text colors ..... to use colored lines in the choice... Is this possible?
    I don't think you could do this in the AWT. I think you could in Swing by making a custom renderer for a JComboBox.
    Usually, I would use an array of Colors and fetch the color using the selectedIndex of the Choice.

    how i can change the color of a String? I have a String inside a TextArea
    TextArea supports only a single color and font. For multiple colors and fonts you will need to use a JTextPane and a StyledDocument.

  3. #3
    dhnsekaran is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default

    AM HAVING "The GraphicsEnvironment class has a couple ".RAR FILE IS NOT RUNNING ?

  4. #4
    Rooneyz is offline Member
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    25
    Rep Power
    0

    Default

    You can use the

    Java Code:
    JTextAreaObject.setForeGroundColor(Color.RED);
    method to change the color of the whole string inside the JTextArea.

    Note that if you've called the setEnabled(false) method on the JTextArea-object in combination with the method above the latter doesn't workwork. You probably want to do setEditable(false) instead of setEnabled(false).
    Last edited by Rooneyz; 09-19-2008 at 02:51 PM.

  5. #5
    j2vdk's Avatar
    j2vdk is offline Member
    Join Date
    Aug 2008
    Posts
    45
    Rep Power
    0

    Default

    Color c=new Color(100,150,200);
    setBackground(c);
    setForeground(c);


    like this create your own custom color, and use it,,, those 3 values are RGB values ranging from 0 to 255

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

    Question Changing font colors on conditions - netbeans 6.5

    Following on, I have tried the examples shown but to no avail

    Java Code:
                  if (outputBmi < 18.5) {
                                txtResults.setText("Underweight");
                                Color c=new Color(255,204,0);
                                    setBackground(c);
                                    setForeground(c);
                                    } else{
    
                                        if (outputBmi > 24.9){
                                           txtResults.setText("Overweight");
                                           Color c=new Color(255,51,51);
                                                setBackground(c);
                                                setForeground(c);
    
                                            } else
                                                txtResults.setText("OK");
                                                    Color c=new Color(102,255,102);
                                                        setBackground(c);
                                                        setForeground(c);
    Can someone tell me where I am going wrong?

    Basically the rgb values are the colors I want the fonts on those particular outputs.

    Thanks in advance.

Similar Threads

  1. Replies: 6
    Last Post: 05-14-2011, 07:48 AM
  2. Replies: 5
    Last Post: 04-25-2009, 09:00 PM
  3. Help with switch color
    By Daniel in forum AWT / Swing
    Replies: 2
    Last Post: 09-18-2008, 08:54 AM
  4. A bit of color!
    By tim in forum Java 2D
    Replies: 8
    Last Post: 02-12-2008, 12:57 AM
  5. How to change TXT color Onclick
    By dave700800 in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 02:39 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
  •