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
  1. import javax.swing.*;    
  2. import java.awt.event.*;   
  3. import java.awt.Color;// classes    
  4. import java.awt.Font;//classes  
Step 2: Defining the components and creating the object of Color class.
  1. JFrame fr;   
  2. JTextArea ta;  
  3. JScrollPane sp;  
  4. JLabel l1,l2;  
  5. JButton b1,b2,b3,b4,b5,b6;  
  6. Color red=new Color(255,0,0);  
  7. Color green=new Color(0,255,0);  
  8. 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 it's not a rule or necessarycondition that to defining here
  1. fr=new JFrame(); fr.setLayout(null);  
  2. b1=new JButton("RED");  
  3. b2=new JButton("BLUE");  
  4. b3=new JButton("GREEN");  
  5. b4=new JButton("PLAIN");  
  6. b5=new JButton("BOLD");  
  7. b6=new JButton("ITALIC");  
  8. l1=new JLabel("SET  BACKGROUND  COLOR ");  
  9. l2=new JLabel("SET  FONT  STYLE");  
  10. ta=new JTextArea();  
  11. sp=new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.  
  12. HORIZONTAL_SCROLLBAR_ALWAYS);  
Step 4: Set the coordinate positions of each component and add into the frame.
  1. l1.setBounds(30,150,300,200);  
  2. l2.setBounds(30,230,250,200);  
  3. sp.setBounds(30,30,350,200);  
  4. b1.setBounds(30,270,90,40);  
  5. b2.setBounds(120,270,90,40);  
  6. b3.setBounds(210,270,90,40);  
  7. b4.setBounds(30,350,90,40);  
  8. b5.setBounds(130,350,90,40);  
  9. b6.setBounds(220,350,90,40);  
  10. fr.add(sp);  
  11. fr.add(b1);  
  12. fr.add(b2);  
  13. fr.add(b3);  
  14. fr.add(b4);  
  15. fr.add(b5);  
  16. fr.add(b6);  
  17. fr.add(l1);  
  18. 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.
  1. b1.addActionListener(this);  
  2. b2.addActionListener(this);  
  3. b3.addActionListener(this);  
  4. b4.addActionListener(this);  
  5. b5.addActionListener(this);  
  6. b6.addActionListener(this);  
  7. fr.setSize(500,500);  
  8. fr.setVisible(true);  
  9. 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
  1. public void actionPerformed(ActionEvent ae)  
  2. {  
  3.    if(ae.getSource()==b1)  
  4.    {  
  5.       ta.setBackground(red);  
  6.    }  
  7.    if(ae.getSource()==b2)  
  8.    {  
  9.       ta.setBackground(blue);  
  10.    }  
  11.    if(ae.getSource()==b3)  
  12.    {  
  13.       ta.setBackground(green);  
  14.    }  
  15.    if(ae.getSource()==b4)  
  16.    {  
  17.       ta.setFont(new Font("Times New Roman",Font.PLAIN, 40));  
  18.    }  
  19.    if(ae.getSource()==b5)  
  20.    {  
  21.       ta.setFont(new Font("Times New Roman",Font.BOLD, 40));  
  22.    }  
  23.    if(ae.getSource()==b6)  
  24.    {  
  25.       ta.setFont(new Font("Times New Roman",Font.ITALIC, 40));  
  26.    }  
  27. }  
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
  1. {  
  2.       
  3.  JFrame fr;  
  4.  JTextArea ta;  
  5.  JScrollPane sp;  
  6.  JButton b1, b2, b3, b4, b5, b6;  
  7.  JLabel l1, l2;  
  8.  Color red = new Color(25500);  
  9.  Color green = new Color(02550);  
  10.  Color blue = new Color(00255);  
  11.    
  12.  public TextAreaTest()  
  13.  {  
  14.        
  15.   fr = new JFrame();  
  16.   fr.setLayout(null);  
  17.   b1 = new JButton("RED");  
  18.   b2 = new JButton("BLUE");  
  19.   b3 = new JButton("GREEN");  
  20.   b4 = new JButton("PLAIN");  
  21.   b5 = new JButton("BOLD");  
  22.   b6 = new JButton("ITALIC");  
  23.   l1 = new JLabel("SET  BACKGROUND  COLOR ");  
  24.   l2 = new JLabel("SET  FONT  STYLE");  
  25.   ta = new JTextArea();  
  26.     
  27.   sp = new JScrollPane(ta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  
  28.     
  29.   l1.setBounds(30150300200);  
  30.   l2.setBounds(30230250200);  
  31.   sp.setBounds(3030350200);  
  32.   b1.setBounds(302709040);  
  33.   b2.setBounds(1202709040);  
  34.   b3.setBounds(2102709040);  
  35.   b4.setBounds(303509040);  
  36.   b5.setBounds(1303509040);  
  37.   b6.setBounds(2203509040);  
  38.    
  39.   fr.add(sp);  
  40.   fr.add(b1);  
  41.   fr.add(b2);  
  42.   fr.add(b3);  
  43.   fr.add(b4);  
  44.   fr.add(b5);  
  45.   fr.add(b6);  
  46.   fr.add(l1);  
  47.   fr.add(l2);  
  48.    
  49.   b1.addActionListener(this);  
  50.   b2.addActionListener(this);  
  51.   b3.addActionListener(this);  
  52.   b4.addActionListener(this);  
  53.   b5.addActionListener(this);  
  54.   b6.addActionListener(this);  
  55.   fr.setSize(500500);  
  56.   fr.setVisible(true);  
  57.   
  58.   fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  59.  }  
  60.  public void actionPerformed(ActionEvent ae)  
  61.  {  
  62.   if (ae.getSource() == b1)  
  63.   {  
  64.    ta.setBackground(red);  
  65.   }  
  66.   if (ae.getSource() == b2)  
  67.   {  
  68.    ta.setBackground(blue);  
  69.   }  
  70.   if (ae.getSource() == b3)  
  71.   {  
  72.    ta.setBackground(green);  
  73.   }  
  74.   if (ae.getSource() == b4)  
  75.   {  
  76.    ta.setFont(new Font("Times New Roman", Font.PLAIN, 40));  
  77.   }  
  78.   if (ae.getSource() == b5)  
  79.   {  
  80.    ta.setFont(new Font("Times New Roman", Font.BOLD, 40));  
  81.   }  
  82.   if (ae.getSource() == b6)  
  83.   {  
  84.    ta.setFont(new Font("Times New Roman", Font.ITALIC, 40));  
  85.   }  
  86.  }  
  87.  public static void main(String[] args)  
  88.  {  
  89.   new TextAreaTest();  
  90.  }  
  91. }  
Output:
 
You run the code with the 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