Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

Using replaceText with MenuItems

Question:
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.

Answer:
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");

                menu.add(menuitem);

                menubar.add(menu);

                setMenuBar(menubar);



                textarea = new TextArea(10, 40);

                setLayout(new BorderLayout());

                add("South", textarea);

                pack();

                show();

        }



        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() +

                                                textarea.getSelectedText(),

                                                textarea.getSelectionStart(),

                                                textarea.getSelectionEnd());

                        return true;

                }

                return false;

        }



        public static void main(String argv[]) {

                TextDoubler t = new TextDoubler();

                t.show();

        }

}

DevX Pro
 
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date