Using replaceText with MenuItems

Using replaceText with MenuItems

I created a UI for a selector guide using Frame, Menubar andMenuItems. I am having trouble writing the event handling; specifically, Ineed to see an example using replaceText when one chooses a menu item. Ihave no problem using textArea with a single string, but I can’t seem tofind 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 textin a text area. The TextDoubler class shown below does that by creating textArea and a menubar containing one menu item for doubling the currentlyselected text. It extends class Frame and overrides its action method tohandle menuitem events.

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

import java.awt.*;import*;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 ( instanceof MenuItem &&               == 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();      ;        }}

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing