Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7623261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:39:45+00:00 2026-05-31T04:39:45+00:00

I have a file which i will read it line by line. Split each

  • 0

I have a file which i will read it line by line. Split each line into words by using the split method and color the words based on their positions (first 4 chars of each line etc) and also based on the words. Different color should be applied to different words like below. I want to know which class is useful, i looked into highlighter. Any suggestions, with example would be very helpful

String text = textArea.getText();
String newLine = "\n";
String spaceDelim = "[ ]+";
String[] tokens;
String lines = text.split(newLine);
for(String line : lines) {
    tokens = line.split(spaceDelim);
    tokens[1] //should be in redColor
    tokens[2] //should be in greenColor
    tokens[3] tokens[4] //should in blueColor
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:39:46+00:00Added an answer on May 31, 2026 at 4:39 am

    If you want to have different text literals to have different colours, you must read about How to use Editor Pane or TextPane. This will help you in that.

    A sample program :

    import java.awt.*;
    
    import java.awt.event.*;
    
    import javax.swing.*;
    
    import javax.swing.border.*;
    
    import javax.swing.text.AttributeSet;
    import javax.swing.text.SimpleAttributeSet;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyleContext;
    
    public class TextPaneTest extends JFrame
    {
        private JPanel topPanel;
        private JTextPane tPane;
    
        public TextPaneTest()
        {
            topPanel = new JPanel();        
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);            
    
            EmptyBorder eb = new EmptyBorder(new Insets(10, 10, 10, 10));
    
            tPane = new JTextPane();                
            tPane.setBorder(eb);
            //tPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
            tPane.setMargin(new Insets(5, 5, 5, 5));
    
            topPanel.add(tPane);
    
            appendToPane(tPane, "My Name is Too Good.\n", Color.RED);
            appendToPane(tPane, "I wish I could be ONE of THE BEST on ", Color.BLUE);
            appendToPane(tPane, "Stack", Color.DARK_GRAY);
            appendToPane(tPane, "Over", Color.MAGENTA);
            appendToPane(tPane, "flow", Color.ORANGE);
    
            getContentPane().add(topPanel);
    
            pack();
            setVisible(true);   
        }
    
        private void appendToPane(JTextPane tp, String msg, Color c)
        {
            StyleContext sc = StyleContext.getDefaultStyleContext();
            AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
    
            aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
            aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);
    
            int len = tp.getDocument().getLength();
            tp.setCaretPosition(len);
            tp.setCharacterAttributes(aset, false);
            tp.replaceSelection(msg);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        new TextPaneTest();
                    }
                });
        }
    }
    

    And here is the output of this code :

    JTEXTPANE EXAMPLE

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php file which I will be using as exclusively as an
i am generating *.reg file using code which will have some important data. and
I have this code which will include template.php file from inside each of these
I have a .csv file containing over 70 million lines of which each line
I have a PHP file which will return something like <div id=title>Add Us On
I have a Batch file which will call a Powershell Script : BATCH FILE
I have a Perl script which will allow for a file to be uploaded
I have a Function in CFC file, which will be called from .cfm file
i have requirement to store the regex in flat file and read them.There will
I have text file from which I need to get data by line by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.