Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: User Interface Design
Expertise: Beginner
Mar 19, 1997



Application Security Testing: An Integral Part of DevOps

Using replaceText with MenuItems

I created a UI for a selector guide using Frame, Menubar and MenuItems. I am having trouble writing the event handling; specifically, I need to see an example using replaceText when one chooses a menu item. I have no problem using textArea with a single string, but I can't seem to find my way with multiple text strings.

The following example demonstrates how to use replaceText with menuitems. Let's say you wanted a menuitem that duplicated the currently selected text in a text area. The TextDoubler class shown below does that by creating textArea and a menubar containing one menu item for doubling the currently selected text. It extends class Frame and overrides its action method to handle menuitem events.

Note that it is not sufficient to just instantiate class Frame directly because the event handler for a frame throw away the events it gets. You have to subclass Frame and override the action or handleEvent routines to get it to do anything useful.

import java.awt.*;

import java.io.*;

public class TextDoubler extends Frame {

        TextArea textarea;

        public TextDoubler() {

                // create a menubar with one menuitem for doubling the selected text.

                MenuBar menubar = new MenuBar();

                Menu menu = new Menu("Doubler");

                MenuItem menuitem = new MenuItem("Double text");




                textarea = new TextArea(10, 40);

                setLayout(new BorderLayout());

                add("South", textarea);




        public boolean action(Event e, Object arg) {

                if (e.target instanceof MenuItem &&

                        e.id == Event.ACTION_EVENT ) {

                        // grab the currently selected text and

                        // insert two copies of it in place.

                        // the selection can span multiple lines

                        textarea.replaceText(textarea.getSelectedText() +




                        return true;


                return false;


        public static void main(String argv[]) {

                TextDoubler t = new TextDoubler();




DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date