Adding text in to text area of jframe at new line with different background colour

Ads
 

Adding text in to text area of jframe at new line with different background colour

Hi,

I am reading text from text file and i want to write that text in to text area of jframe.But i am reading line by line from file and i want to write that text every time at new line in text area with different background colour. it is possible in text area of jframe..?

View Answers

November 30, 2011 at 5:11 PM

import java.io.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;

public class TextAreaExample {
    public static void main(String[] args)throws Exception{
        JFrame f = new JFrame("First JTextPane Example");
        f.getContentPane().setLayout(new BorderLayout());

        JTextPane text = new JTextPane();
        Random r = new Random();

        BufferedReader br=new BufferedReader(new FileReader("data.txt"));
        String str=" ";

        StyledDocument doc = text.getStyledDocument();

        Style style = text.addStyle(" ", null);
        while((str=br.readLine())!=null){
            Color randomColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
            StyleConstants.setForeground(style, randomColor);
            try { doc.insertString(doc.getLength(), str,style);
            doc.insertString(doc.getLength(), "\n",style);
            }
            catch (Exception e){}
        }
        f.getContentPane().add(text, BorderLayout.CENTER);
        f.setSize(350, 250);
        f.show();
    }
    }

Ads









Related Tutorials/Questions & Answers:
Adding text in to text area of jframe at new line with different background colour
Adding text in to text area of jframe at new line with different background... to write that text every time at new line in text area with different background colour. it is possible in text area of jframe
Text Area
Text Area  how to browse an image in text area instead of giving url of particular image
Advertisements
How to write file text In New Line
text in new line in Java program. How to write the syntax for this with example. thanks   Hi, For write a new text new line we need bufferedwriter... you went new text in new line. For this we created a new text file named
Write Text To File In New Line.
Write Text To File In New Line. In this tutorial you will learn how to write text into file in a new line. When you are writing in a file you may be required to finish the line and want to write the other text in a new line
ModuleNotFoundError: No module named 'colour-text'
ModuleNotFoundError: No module named 'colour-text'  Hi, My Python... 'colour-text' How to remove the ModuleNotFoundError: No module named 'colour-text' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'colour-text'
ModuleNotFoundError: No module named 'colour-text'  Hi, My Python... 'colour-text' How to remove the ModuleNotFoundError: No module named 'colour-text' error? Thanks   Hi, In your python environment
Text Area in HTML
Text Area in HTML       Text  Area in HTML is used to write unlimited number of text in it. Understand with Example The Tutorial illustrate an example from Text Area
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program  
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program  
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program
Auto grow Text Area using jQuery plug-in
Auto grow Text Area using jQuery plug-in  Hi sir How can we make a textarea auto grow ? What is the name of the plug-in used for auto grow text area. Please mention code with your reply
Program to read the text from a file and display it on a JFrame.
Program to read the text from a file and display it on a JFrame.  ...*; class MegaViewer1 extends JFrame { JTabbedPane jtp1=new JTabbedPane(); JPanel jp1=new JPanel(); JTextArea t1=new JTextArea(); MegaViewer1() throws
Java Read Lines from Text File and Output in Reverse order to a Different Text File
by line and output in reverse order to the text area field. Can someone help...Java Read Lines from Text File and Output in Reverse order to a Different Text...[] args) { JFrame window = new FileInput(); window.setSize(600, 400
jframe background color not showing
jframe background color not showing  jframe background color is not showing. please suggest.   Just add the background color to JPanel and then add it to JFrame. For example .. JFrame myFrame = new JFrame(); JPanel
ModuleNotFoundError: No module named 'core-module-text-area-app'
ModuleNotFoundError: No module named 'core-module-text-area-app'  Hi...: No module named 'core-module-text-area-app' How to remove the ModuleNotFoundError: No module named 'core-module-text-area-app' error? Thanks  
Adding JTable into existing Jframe.
Adding JTable into existing Jframe.  i need to add JTable...("serial") public class AddPage extends JFrame { JDesktopPane pane = new...(100,60,1300,600); add(pane); //Action Listener for button //for adding new
Adding JTable into existing Jframe.
Adding JTable into existing Jframe.  i need to add JTable...("serial") public class AddPage extends JFrame { JDesktopPane pane = new...(100,60,1300,600); add(pane); //Action Listener for button //for adding new
Adding JTable into existing Jframe.
Adding JTable into existing Jframe.  i need to add JTable...("serial") public class AddPage extends JFrame { JDesktopPane pane = new...(100,60,1300,600); add(pane); //Action Listener for button //for adding new
ModuleNotFoundError: No module named 'text-new-wargame'
ModuleNotFoundError: No module named 'text-new-wargame'  Hi, My... named 'text-new-wargame' How to remove the ModuleNotFoundError: No module named 'text-new-wargame' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'text-new-wargame-git'
ModuleNotFoundError: No module named 'text-new-wargame-git'  Hi...: No module named 'text-new-wargame-git' How to remove the ModuleNotFoundError: No module named 'text-new-wargame-git' error? Thanks   Hi
gradient text, text effect, text
to take a new file with black background as here. Write Text: Select Horizontal... How to make a gradient text       We can make many different type of text by the help
How to read a large text file line by line in java?
How to read a large text file line by line in java?  I have been... memory assigned is also a limit. So, we have decided to read the text file line... you kind advice and let's know how to read a large text file line by line in java
java script to replace words starting and ending with tild(~) symbol in a text area with the word highlighted yellow.
with highlighting text in a text area. I am designing a page to send email. currently.... This is applicable in the subject line and the email body. AS per new...java script to replace words starting and ending with tild(~) symbol in a text
How to read text file to two different name array
How to read text file to two different name array   I have those numbers:12,4,9,5 numbers:19,12,1,1 how to put it in two different name array in text file to java
How to read text file to two different name array
How to read text file to two different name array   I have those numbers:12,4,9,5 numbers:19,12,1,1 how to put it in two different name array in text file to java
i want validation for one text field and text area,email,combobox, in that email will validte by useing regular expressions?
i want validation for one text field and text area,email,combobox... for one text field and text area,email,combobox, in that email will validte by useing...; Enter Name: <input type="text" name="name" size="30
How to extract the entire line with specific data from a Text in java?
How to extract the entire line with specific data from a Text in java?  Hi, i would like to extract data from text like the following one: [CODE... my prog? i know how to read specific line from text with the lineNumber
Map words to line number in text file and show occurence
Map words to line number in text file and show occurence  hi i want to Map words to line number in text file and show occurrence of word in java coding
ModuleNotFoundError: No module named 'text-new-wargame-git-twine'
ModuleNotFoundError: No module named 'text-new-wargame-git-twine'  Hi...: No module named 'text-new-wargame-git-twine' How to remove the ModuleNotFoundError: No module named 'text-new-wargame-git-twine' error? Thanks
How to make a cloudy text, cloudy text, text
of you text as I have done here. Take another new file for text. Take a New File: Now take a new file to write text as I have taken here. Setting of the Text: We... How to make a cloudy text      
Text to voice
Text to voice  sample code for text to voice in objective c

Ads