Working With JTextArea in Swing


Introduction

In this article we will describe how to set the background color of a test area and set its font style. And with the help of this article you can set the background to whatever you choose. So many colors are made by the help of the RGB concept. In RGB (Red , Green, Blue) you are free to give the value of three colors in integer form between 0 to 255. In Java the color is a separate class in awt package you create the object of the color class using a different constructor. And we also add the scroll bar in the text area. For implementing it you have to follow the steps given below:

Step 1: Import necessary packages and classes

import javax.swing.*;

import java.awt.event.*;

import java.awt.Color;// classes

import java.awt.Font;//classes

Step 2: Defining the components and creating the object of Color class.

JFrame fr;
JTextArea ta;
JScrollPane sp;
JLabel l1,l2;
JButton b1,b2,b3,b4,b5,b6;
Color red=
new Color(255,0,0);
Color green=
new Color(0,255,0);
Color blue=new Color(0,0,255);

Step 3: Creating the object of step-2 components and all the following thing we define in constructor its not a rule or necessarycondition that to defining here.

fr=
new JFrame(); fr.setLayout(null);
b1=
new JButton("RED");
b2=
new JButton("BLUE");
b3=
new JButton("GREEN");
b4=
new JButton("PLAIN");
b5=
new JButton("BOLD");
b6=
new JButton("ITALIC");
l1=
new JLabel("SET  BACKGROUND  COLOR ");
l2=
new JLabel("SET  FONT  STYLE");
ta=
new JTextArea();
sp=
new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.
HORIZONTAL_SCROLLBAR_ALWAYS);

Step 4:  Set the coordinate positions of each components and add in to frame.

l1.setBounds(30,150,300,200);
l2.setBounds(30,230,250,200);
sp.setBounds(30,30,350,200);
b1.setBounds(30,270,90,40);
b2.setBounds(120,270,90,40);
b3.setBounds(210,270,90,40);
b4.setBounds(30,350,90,40);
b5.setBounds(130,350,90,40);
b6.setBounds(220,350,90,40);
fr.add(sp);
fr.add(b1);
fr.add(b2);
fr.add(b3);
fr.add(b4);
fr.add(b5);
fr.add(b6);
fr.add(l1);
fr.add(l2);

Step 5:  Now attached (resgitration) all the buttons to the listener and set the size of frame make it visible true.and we also set the default close operation is close.

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

fr.setSize(500,500);

fr.setVisible(true);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Step 6: write the code of action performed method according to event you want to fire on click a particular button.
 

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==b1)

{

ta.setBackground(red);

}

if(ae.getSource()==b2)

{

ta.setBackground(blue);

}

if(ae.getSource()==b3)

{

ta.setBackground(green);

}

if(ae.getSource()==b4)

{

ta.setFont(new Font("Times New Roman",Font.PLAIN, 40));

}

if(ae.getSource()==b5)

{

ta.setFont(new Font("Times New Roman",Font.BOLD, 40));

}

if(ae.getSource()==b6)

{

ta.setFont(new Font("Times New Roman",Font.ITALIC, 40));

}

}

Step 7: Rest only the Action performed method all the things we wrote within a constructor so we need only create the object of my class with the main method.

Complete code

import javax.swing.*;

import java.awt.event.*;

import java.awt.Color;

import java.awt.Font;

class  TextAreaTest implements ActionListener

{

JFrame fr;

JTextArea ta;

JScrollPane sp;

JButton b1,b2,b3,b4,b5,b6;

JLabel l1,l2;

Color red=new Color(255,0,0);

Color green=new Color(0,255,0);

Color blue=new Color(0,0,255);

public TextAreaTest()

{

fr=new JFrame();

fr.setLayout(null);

b1=new JButton("RED");

b2=new JButton("BLUE");

b3=new JButton("GREEN");

b4=new JButton("PLAIN");
b5=
new JButton("BOLD");

b6=new JButton("ITALIC");

l1=new JLabel("SET  BACKGROUND  COLOR ");

l2=new JLabel("SET  FONT  STYLE");

ta=new JTextArea();

sp=new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

l1.setBounds(30,150,300,200);

l2.setBounds(30,230,250,200);

sp.setBounds(30,30,350,200);

b1.setBounds(30,270,90,40);

b2.setBounds(120,270,90,40);

b3.setBounds(210,270,90,40);

b4.setBounds(30,350,90,40);

b5.setBounds(130,350,90,40);

b6.setBounds(220,350,90,40);

fr.add(sp);

fr.add(b1);

fr.add(b2);

fr.add(b3);

fr.add(b4);

fr.add(b5);

fr.add(b6);

fr.add(l1);

fr.add(l2);
b1.addActionListener(
this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

fr.setSize(500,500);

fr.setVisible(true);

fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==b1)

{

ta.setBackground(red);

}

if(ae.getSource()==b2)

{

ta.setBackground(blue);

}

if(ae.getSource()==b3)

{

ta.setBackground(green);
}

if(ae.getSource()==b4)

{

ta.setFont(new Font("Times New Roman",Font.PLAIN, 40));
}
if(ae.getSource()==b5)

{

ta.setFont(new Font("Times New Roman",Font.BOLD, 40));
}

if(ae.getSource()==b6)

{

ta.setFont(new Font("Times New Roman",Font.ITALIC, 40));

}

}

public static void main(String[] args)

{

new TextAreaTest();

}

}
 

Output:


You run the code withe help of cmd and its output is following.

trcmd.jpg

Initial (default) output is following.

tr000.jpg

 Now you can set your background color and its font style on clicking the following buttons.

tr1.jpg



tr2.jpg



tr3.jpg

 Resources

Up Next
    Ebook Download
    View all
    Learn
    View all