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 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();
    }
    }









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... from Text Area in HTML.In this example, the code elaborate you to create text area
Adding a text file - Java Beginners
Adding a text file  Hello, I need a program that will search a text file of strings representing numbers of type int and will write the largest... representing numbers of type int, one per line. Can you help
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
Java file new line
Java file new line In this section, you will learn how to write the text in new line of text file. Description of code: The package java.io.* has provide... a text in the file, there are different output streams. Among them
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  
new String(text) - Java Beginners
: In this case text will go pool area and this.text refer it. this.text = new String...new String(text)  Please, what is the difference between this.text = text; and this.text = new String(text); in the example: public
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
spell cheking in text area - JSP-Servlet
spell cheking in text area   Hi Good Morning. I am developing web application. Now i am in one JSP page. in this page i have 1 text area. i want spell check functionality by pressing right click in this text area.but i don't
Change background color of text box - Java Beginners
Change background color of text box  Hi how can i change the background color to red of Javascript text box when ever user enters an incorrect value while on entering a correct value its background color should change green
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
How to Create Text Area In Java
How to Create Text Area In Java       In this section, you will learn how to create Text Area in Java... are created and a text area is also created on the panel by using JTextArea class
Auto grow Text Area using jQuery plug-in
Auto grow Text Area using jQuery plug-in In this section, you will learn how to create a text area which auto grow on increment of the content using jQuery Plug-in. We fixed the height of text area to 70 pixels .The size of the window
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  
Layout text along a line using LineMetrics
Layout text along a line using LineMetrics... to layout text along a line. For this, class LineMetrics is used which layouts the text along a line. The Font class represents the font. The class
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
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
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
Break a Line for text layout
Break a Line for text layout   ... to break a line. To break down the text into lines, the class LineBreakMeasurer... in the constructor of class AttributedString which holds the text. To allows iteration
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
gradient text, text effect, text
to take a new file with black background as here.ADS_TO_REPLACE_1 Write Text... How to make a gradient text       We can make many different type of text by the help
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
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
adding background image - Java Beginners
adding background image  how do i add background image to this code...*; public class sampleProg extends JFrame { JButton button = new JButton ("Result... JLabel ("Laurize Albarracin"); JTextField text = new JTextField
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
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 extract a specific line from a text file? - IoC
How to extract a specific line from a text file?  Hi all, i'm trying to write a code that return a specific line from a text file. so my first...){ String line = ""; int lineNo; try { FileReader fr = new FileReader("C
Replacing a Text Node with a New CDATA Section Node
Replacing a Text Node with a New CDATA Section Node... to replace a Text node with new CDATASection Node in a DOM document. Methods which are used for replacement of the text node in the DOM Document are described
How to create a new text file that contains the file names of the copied files in a new directory? - Java Beginners
How to create a new text file that contains the file names of the copied files in a new directory?  Okay so my question is: How to create a new text file that contains the file names of the copied files in a new directory
How to make a cloudy text, cloudy text, text
Take another new file for text. Take a New File: Now take a new file to write... How to make a cloudy text      ... to do that.  After this you will able to make a cloudy text. Here
iPhone Text Field Border 
to the view, after adding will select each text field and then go to tools ->...Text Field Boarder  In this tutorial will learn about the text field Boarder, we can set the boarder of text field by selecting the text field
Text to voice
Text to voice  sample code for text to voice in objective c
text field
static void main(String[] args){ JFrame f=new JFrame(); JLabel label1=new JLabel("Name: "); JLabel label2=new JLabel("Address: "); final...text field  How to retrieve data from text field   Hi
While retrieving text from database onto JSP, the alignment of line and paragraphs is not followed
While retrieving text from database onto JSP, the alignment of line and paragraphs is not followed  I have stored text database through textarea box. While trying to retrieve the same text from db, the text displayed onto
Text Field in HTML
Text Field in HTML       The Text Field in HTML are one line areas, which enable the user to enter input text. Understand with ExampleADS_TO_REPLACE_1 The Tutorial illustrates
How to read and compare content of two different text file
Description: In the given example you will see how a two text file's content are compared. The BufferedReader class allow us to read a file. The readLine() method used to read the contents of the specified file.  The way
text limitations
text limitations  to give text limitations, i want to show only 50 words in dat page

Ads