Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: text color changer based on words/ word classification by color

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    44
    My Mood
    Mad
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Angry text color changer based on words/ word classification by color

    i am a complete novice programmer, I'm in high-school [freshmen] and i would like to create a program [it is not an assignment so font worry] but i would like a program that i could either have in an executable file [preferably] or be able to be put onto a website but basically what this program would do is it would have multiple text boxes that a user could put information in and one text box that would be the output.

    now after reading this you may think that i am trying to create a spell checker well i already have one it's called Microsoft word and i know about it but rather this program would have one MAIN text box where you would put in your text from your document [preferably still for mated of coarse] and it would output with different words being different colors. it would figure out which words would be what colors by another set of text boxes, each of these text boxes would be assigned a color and the words [or set of words] inside these text boxes would be the words [or set of words] that would be turned that specific color [or would be highlighted by a specific color or underlined a specific color]

    i know the basics of how it would work it would be something along the lines of say

    if( and, the, but, But,) then output>

    now i would be able to do this to an extent except this has to do with the color of the text and maybe/ hopefully a highlighted or underlined color of the text, and i have know idea what the terms are.

    this would be used for proff reading essays and stuff [my English teacher said she would love to have something like this] or documents of code programming to proof read <<[this has already been done ] but the difference is that there wouldn't be a rigid set the user decides what words are what colors but i have no idea how to do this even though it would probably be easy. but basically what i want is a input box to put a couple pages of text in formatting and all and then have other text boxes one for red, blue, green, yellow, purple, orange.



    but as i said before i have no idea how do do this due to the text color stuff [probably wouldn't anyways] and i would really really like some help with this as soon as possible.

    and thank you just for reading this and if you want to contact me

    Reply on here

    or my Psn's are knoxy5467, Parad0xs12, and my xbx gamertag is Parad0xs [more likely to reach me at my Psn's though]



    so thank you for reading and please help this would make me and several teachers very happy and our lives would be allot easier


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: text color changer based on words/ word classification by color

    What have you coded so far?
    Look at the JEditorPane class as a way to have different lines in different colors.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    44
    My Mood
    Mad
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: text color changer based on words/ word classification by color

    nothing really because i dont know where to start i know that if it was more indavidual statments i could use if then statments but since it's going to be alot of words changing colors i dont know what to do will seperating staments with a comma do it?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: text color changer based on words/ word classification by color

    Start with small bits and add to it as you go.
    will seperating staments with a comma do it?
    Do what?
    Please explain what adding commas will do for your problem.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    44
    My Mood
    Mad
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: text color changer based on words/ word classification by color

    i was thinking that in the text boxes where the text that supposed to change colors will separating each word with a comma allow the if statement to recognize each and every one of those words in the main text body using the main text input as a variable and the text color changers as something that recognizes specified words as things they need to change? [hope i didn't confuse you]

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: text color changer based on words/ word classification by color

    There are different ways to scan a String. One is to use delimiters and you seem to be thinking to use a comma.

    allow the if statement to recognize each and every one of those words
    here you are running ahead and thinking of how you are going to implement some of the code.
    Design what you want the program to do and then figure out the best technique for doing it.

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    44
    My Mood
    Mad
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: text color changer based on words/ word classification by color

    i want to be able to take text from say a word document and put it into this program and then tell this program via dialog boxes to change specific words in this body of text to a specific color and then i want it to output the body of text i put in with the text color changes implemented

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: text color changer based on words/ word classification by color

    i want it to output the body of text i put in with the text color changes implemented
    I understand the first part, but not this part.
    What is the output to look like? How are the colored words to be represented?
    If you output the text with markup like HTML then the coloring could be preserved.

  9. #9
    Member
    Join Date
    Jun 2011
    Posts
    44
    My Mood
    Mad
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: text color changer based on words/ word classification by color

    yes with markup like html

    [|v| this may be a noob question |v|]
    how do i preserve the coloring?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: text color changer based on words/ word classification by color

    The coloring controls for the text being displayed would be around somewhere that you could read and use when you create the HTML.
    I've never done this, so I'm not sure where they would be.

  11. #11
    Member
    Join Date
    Jun 2011
    Posts
    44
    My Mood
    Mad
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: text color changer based on words/ word classification by color

    well i was thinking that i would have the main string [the place where the suser puts the main body of text]

    and then have other strings that tells another funtion or somthing to change specific tokens different colors

    also the content of the strings is added by the user via text box input

    and then have it all acumulated in a output text where all the color changes are shown and the formatting is still there

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: text color changer based on words/ word classification by color

    output text where all the color changes are shown and the formatting is still there
    Sounds like HTML

  13. #13
    Member
    Join Date
    Jun 2011
    Posts
    44
    My Mood
    Mad
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: text color changer based on words/ word classification by color

    maybe we could use strings somhow

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: text color changer based on words/ word classification by color

    That what HTML is made up of: Strings of text.

  15. #15
    Member
    Join Date
    Jun 2011
    Posts
    44
    My Mood
    Mad
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: text color changer based on words/ word classification by color

    ok so we could make all of the words in the text box that states that the user wants the word lets say "car" to be blue well we would make "car" a known variable and if "car" is in the body of text that we want changed and its in the text box that we want those words blue then it would recognize it in the main body of text [classified as a string with each word being a token] and would change it's color to blue would that work? with like 30 different words possibly being blue and their being over 1000 words in the main body of text?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: text color changer based on words/ word classification by color

    Yes, I think that could be done. I'm experimenting with the JTextPane (JTP) class as a possibility for your project.
    The way it works is:
    You add text to the JTP
    When you want to color a word, search for the word, color it at the location it is found and loop back to search again and color again until all instances colored.

    You'd have to define the output format for the text to be able to decide how to preserve the coloring when you write the text to a file.

  17. #17
    Member
    Join Date
    Jun 2011
    Posts
    44
    My Mood
    Mad
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: text color changer based on words/ word classification by color

    would there be a way to look for the positions in the text that reterns are or where the words that are colord are OR just use a replace function like in word [i know java can do that i just dont have the slightest idea how]

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: text color changer based on words/ word classification by color

    a way to look for the positions in the text that reterns are or where the words that are colord are OR just use a replace function
    Sorry, I don't understand your question. What is a "reterns"?
    To find where the words that are colored are located, you'd need to save their location as you colored them

  19. #19
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: text color changer based on words/ word classification by color

    I think you're asking a few different questions here. You might want to start with something that doesn't have a GUI- it could take a text file and output an html file, with certain hard-coded words colored using basic html font tags. Then just open that file in a web browser, and see how you like the effect.

    From there, you can go to a GUI that allows the user to customize which words are which colors, and from there you can go to a GUI that displays the output html file.

    There are many ways to accomplish your goal, so you might want to experiment a bit. This is a doable project for a novice, but it's going to be slow- slow down, take your time, and don't get carried away with "how cool would this be" type thoughts. Start small and go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  20. #20
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: text color changer based on words/ word classification by color

    This is possible to achieve with HTML in, for example a JLabel - you start the text with "<html>", then wrap each word with HTML to set its color, for example: "<html>The cat <font color=red>sat</font> on the mat" would display: "The cat sat on the mat" (see How To Use HTML In Swing Components).

    You could have lists of words for each required color, a text area to paste text into, a label area to display the colored text, and a button to process the text and display it in color. For (a crude) example:


    The main difficulties with this technique involve finding and wrapping the target words, and formatting the JLabel for display (e.g. line-wrapping needs "<br>" inserted as line breaks).

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: text color changer based on words/ word classification by color

    Nice job dlorde!

    main difficulties with this technique involve finding and wrapping the target words, and formatting the JLabel for display (e.g. line-wrapping needs "<br>" inserted as line breaks).
    Using JTextPane moves the difficulties further on to the output step.
    The JTP handles word wrap and can be in a JScrollPane to allow for longer text. Setting the color for a word is a simple search (indexOf) and a call to setCharacterAttributes to set the color for the word.
    To build the output would require saving a few references to where the words were and their colors and then using that with substring to parse out the non colored and colored parts to build HTML output.

  22. #22
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: text color changer based on words/ word classification by color

    Yes Norm, I think you're absolutely right - using JTextPane for the initial color processing avoids all the problems of trying to edit HTML text (e.g. avoiding replacing the contents of the HTML tags already inserted, if the word list contains 'font' or 'blue', etc). Generating HTML output from the JTP would be relatively easy.

    Having said that, finding a word to color in the text isn't necessarily as simple as just using indexOf, because quite a few words can appear as part of other words ('us', 'on', 'the', etc.), so a regex word search is necessary (to find the word between non-word characters), which isn't quite as simple as it sounds given the methods available. For example, you can replace with a regex, but if the regex is 'word between non-word characters', the replacement text will replace the non-word characters too, and you don't know what they are in advance... perhaps cunning use of Matcher can do the job.
    Last edited by dlorde; June 7th, 2011 at 05:03 AM.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: text color changer based on words/ word classification by color

    I think for a student I'd leave the regex for version 2. Keep it simpler for version 1.
    For example after indexOf() finds the word, verify that the char before and the char after is a terminating char by an easy lookup in a table of word terminating chars.

  24. #24
    Member
    Join Date
    Jun 2011
    Posts
    44
    My Mood
    Mad
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: text color changer based on words/ word classification by color

    Quote Originally Posted by dlorde View Post
    This is possible to achieve with HTML in, for example a JLabel - you start the text with "<html>", then wrap each word with HTML to set its color, for example: "<html>The cat <font color=red>sat</font> on the mat" would display: "The cat sat on the mat" (see How To Use HTML In Swing Components).

    You could have lists of words for each required color, a text area to paste text into, a label area to display the colored text, and a button to process the text and display it in color. For (a crude) example:


    The main difficulties with this technique involve finding and wrapping the target words, and formatting the JLabel for display (e.g. line-wrapping needs "<br>" inserted as line breaks).

    um you have the sorce code for this?
    Last edited by knoxy5467; June 15th, 2011 at 07:47 AM.

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: text color changer based on words/ word classification by color

    would either of those help?
    What problem are you trying to solve?
    There are two different solutions posted earlier in the thread.

Page 1 of 2 12 LastLast

Similar Threads

  1. Need help with changing color of JOptionPane
    By jwise95 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2011, 09:49 AM
  2. Trying to output color....HELP PLEASE!
    By toppcon in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 15th, 2011, 10:52 PM
  3. JTable Row Color
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 8th, 2010, 03:59 PM
  4. Color Problem
    By aussiemcgr in forum Java Theory & Questions
    Replies: 5
    Last Post: July 12th, 2010, 03:53 PM
  5. Change of color for selected text in AWT
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: April 9th, 2010, 03:51 AM