20-CS-694 Advanced Programming Techniques Spring 2012
Widgets

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous      Next      Applet Source      All lectures

JTextArea Test

Add some text to the Source Window on the right in the usual manner. Click on the Paste To Front button to place a copy of that text to the beginning of the Target Window on the left. Click on the Paste To Middle button to place a copy of the text in the middle of the text in the Target Window. Click on the Paste To Rear button to append the text of the Source Window to the text of the Target Window.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

public class TextareaApplet extends Applet implements ActionListener {
   JTextArea t1, t2;
   JButton b, c, d;
   
   public void init () {
      setLayout(new BorderLayout(10,10));
      Panel p = new Panel();
      p.setLayout(new GridLayout(1,2,10,10));
      Panel p1 = new Panel();
      p1.setLayout(new BorderLayout());
      p1.add("North", new JLabel("Target Window"));
      p1.add("Center", new JScrollPane(t2=new JTextArea("Hello world,\nHello World.", 12, 6)));
      p.add(p1);
      Panel p2 = new Panel();
      p2.setLayout(new BorderLayout());
      p2.add("North", new JLabel("Source Window"));
      p2.add("Center", new JScrollPane(t1 = new JTextArea(12, 6)));
      p.add(p2);
      add("Center", p);
      Panel q = new Panel();
      q.setLayout(new GridLayout(1,3,20,10));
      q.add(b = new JButton("Paste To Front"));
      q.add(c = new JButton("Paste To Middle"));
      q.add(d = new JButton("Paste To Rear"));
      add("South", q);
      b.addActionListener(this);
      c.addActionListener(this);
      d.addActionListener(this);
   }
   
   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == b)
         t2.insert(t1.getText()+"\n",0);
      else if (evt.getSource() == c)
         t2.insert(t1.getText()+"\n", (t2.getText().length()+1)/2);
      else if (evt.getSource() == d)
         t2.append(t1.getText()+"\n");
   }
}