Using TextAreas and JTextAreas in Applets

This is a very simple demonstration of a TextArea to get you started. Press the button on a running applet (not this screenshot!) to change the text.

TextArea applet in action

TextArea applet in action

You can also

  • change the font from the default with setFont;
  • control the appearance of scrollbars by replacing SCROLLBARS_NONE with SCROLLBARS_BOTH, SCROLLBARS_HORIZONTAL_ONLY, or SCROLLBARS_VERTICAL_ONLY;
  • add text to the end of the existing text using the append method;
  • insert text at a chosen position using the insert method;
  • use replaceRange to replace the existing text between two positions with different text.
  • obtain the text after it has been modified with getText;

We use most of these methods in the demonstration (also on this page) of the use of a JTextArea, which does not have much extra functionality.

See the documentation of the Java TextArea class for further help.

Code of the TextArea Applet

namespace textarea_demo;

interface

uses
  java.util, java.applet.*, java.awt.*;

type
  TextAreaDemo = public class(Applet, ActionListener)
  private
    count : Integer := 0;
    btnNext : Button;
    ta : TextArea;
    MyStrings : array[0 .. 2] of String := ['This is the first string that will not fit onto a single line.', 
                                            'This is the second string that will not fit onto a single line.',
                                            'This is the third string that will not fit onto a single line.'];
  public
    method init; override;
    method actionPerformed(e : ActionEvent);
  end;

implementation

method TextAreaDemo.init;
begin
  Background := Color.green.darker;
  btnNext := new Button('Next');
  add(btnNext);
  btnNext.addActionListener(self);
  ta := new TextArea(MyStrings[0], 3, 15, TextArea.SCROLLBARS_NONE);  //3 rows of 15 columns
  add(ta);
  ta.setForeground (Color.blue.brighter);
end;

method TextAreaDemo.actionPerformed(e : ActionEvent);
begin
  if e.getSource = btnNext then 
    begin
      inc(count);
      ta.setText(MyStrings[count]);
      if count = 2 then
        count := -1;
    end;

 end;

end.

Using a JTextArea

If you do not add a JTextArea to a JScrollPane it will not have any scrollbars. We set the line wrapping with setLineWrap(true) and make sure that words are not split with setWrapStyleWord(true). The code follows a screenshot showing the appending, insertion, replacement and copying of text.

JTextArea applet (400 x 270) in action

JTextArea applet (400 x 270) in action

namespace jtextarea_demo;

interface

uses
  java.util, java.applet.*, java.awt.*, javax.swing;

type
  JTextAreaDemo = public class(Applet, ActionListener)
  private
    btnAppend, btnReplace, btnInsert, btnCopy : Button;
    txtNew : TextField;
    taEdit, taCopy : JTextArea;
    initText : String := 'This is the starting text to modify by pressing the buttons.';
    lblInstruction : Label;
   
  public
    method init; override;
    method actionPerformed(e : ActionEvent);
  end;

implementation

method JTextAreaDemo.init;
begin
  Background := Color.green.darker;
  lblInstruction := new Label('Enter the text to append, insert, or replace with.');
  add(lblInstruction); 
  txtNew := new TextField('', 40);
  add(txtNew);
  btnAppend := new Button('Append');
  btnReplace := new Button('Replace selected text');
  btnInsert := new Button('Insert at cursor position');
  btnCopy := new Button('Copy to lower TextArea');
  add(btnAppend);
  add(btnReplace);
  add(btnInsert);
  add(btnCopy);

  btnAppend.addActionListener(self);
  btnReplace.addActionListener(self);
  btnInsert.addActionListener(self);
  btnCopy.addActionListener(self);

  taEdit := new JTextArea(initText, 5, 20); 
  taCopy := new JTextArea('', 5, 20);
  taEdit.setLineWrap(true);
  taCopy.setLineWrap(true);
  taEdit.setWrapStyleWord(true);
  taCopy.setWrapStyleWord(true);
  add(taEdit);
  add(taCopy);
  taEdit.setForeground (Color.blue.brighter);
  taCopy.setForeground (Color.red);
end;

method JTextAreaDemo.actionPerformed(e : ActionEvent);
begin
  if e.getSource = btnAppend then
    taEdit.append(txtNew.getText);
 
  if e.getSource = btnReplace then
    taEdit.replaceRange(txtNew.getText,  taEdit.getSelectionStart, taEdit.getSelectionEnd); 

  if e.getSource = btnInsert then 
    taEdit.insert(txtNew.getText, taEdit.getCaretPosition); 

  if e.getSource = btnCopy then 
    taCopy.setText(taEdit.getText);  
end;

end.

Programming - a skill for life!

How to use widgets such as JButtons, JTextAreas, JComboBoxes and dialogues in Oxygene for Java applets