472,805 Members | 1,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

Detecting highlighted text in textarea.

Hi,
I am working on an Applet which provides some mouse practice for
new computer users in our local seniors' computer club. The applet
contains several cards, in a card layout, which are displayed to
the user one after the other as needed to present some particular
mouse operation. This all works fine. One card (card 4 below)
includes a textarea and a button. What I wish to do is have the user
use the mouse to select some text from the textarea and click the button.
I wish to detect what the user highlighted and store it as a string to
check.
However I am having a problem doing this. Parts of the program are below:

import java.applet.*;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.*;
import java.util.*;
import java.awt.FontMetrics;
import java.awt.Font;
import java.awt.event.*;
import java.awt.image.*;
public class testmouse extends java.applet.Applet implements Runnable,
MouseListener, AdjustmentListener, ActionListener {
Jul 17 '05 #1
4 9210
"Doug van Vianen" <co*****@shaw.ca> wrote in message news:<Jud2c.691729$JQ1.506094@pd7tw1no>...
Hi,
I am working on an Applet which provides some mouse practice for
new computer users in our local seniors' computer club. The applet
contains several cards, in a card layout, which are displayed to
the user one after the other as needed to present some particular
mouse operation. This all works fine. One card (card 4 below)
includes a textarea and a button. What I wish to do is have the user
use the mouse to select some text from the textarea and click the button.
I wish to detect what the user highlighted and store it as a string to
check.
However I am having a problem doing this. Parts of the program are below:

import java.applet.*;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.*;
import java.util.*;
import java.awt.FontMetrics;
import java.awt.Font;
import java.awt.event.*;
import java.awt.image.*;
public class testmouse extends java.applet.Applet implements Runnable,
MouseListener, AdjustmentListener, ActionListener {
.
.
public void init() {
.
.
//create card four
p4=new myPanel(bgndm,new FlowLayout(),300,100);
bstext="Here is some text that is to be used\n";
bstext=bstext+"to select some text from by\n";
bstext=bstext+"highlighting it using the mouse.\n";
bstext=bstext+"This is a very useful, common operation.\n";
TextArea ts=new TextArea(bstext,4,30,TextArea.SCROLLBARS_NONE);
ts.addActionListener(this); // Do
I need this?
bs=new Button("OK");
p4.add(ts);
p4.add(bs);
.
.
//create background card and add other cards to it
p0=new myPanel(new Color(0).gray,new CardLayout(0,0),300,100);
setLayout(new FlowLayout());
add(p0);
p0.add("card1",p1);
p0.add("card2",p2);
p0.add("card3",p3);
p0.add("card4",p4); //
This is the card with the textarea and button.
((CardLayout)p0.getLayout()).show(p0, "card1");
p0.setVisible(false);
// set all card invisible until each needed
.
.
public void paint(Graphics g) {
.
.
}

public void actionPerformed(ActionEvent tevt) { // I tried this
if (tevt.getSource()== ts) { //
after discovering
txtval=ts.getSelectedText(); //
that thecode below
}
// would not work
}
//
.
.
public boolean action(Event evt, Object arg) { //
whichbut="";
// This was my
if (evt.target instanceof Button) { //
original try.
whichbut = ((Button)evt.target).getLabel(); // It
detects the

// use of the
if (whichbut.equals("OK")) { //
buttons ok but
txtval=ts.getSelectedText(); //
the getSelectedText
theop="P";
// statement does not
}
// work. (The check
repaint();
// for other buttons
}
// is not shown.)
}
//
.
.

return true;
}

When I detect that the OK button is clicked I wish to set the string
variable txtval to be the highlighted text, then check to see if it is
the correct text and use repaint to display to the user how he
or she did. The paint part works okay so I have not shown it.

Can someone tell me how, after I detect that the OK button is clicked,
I can set the string txtval to be the selected text? Thank you.

Doug van Vianen
co*****@shaw.ca


From your code above we can see:
(1)Button bs doesn't have its event handler.
(2)Textarea's event handler is actionPerformed. But text selection
does not trigger ActionEvent on Java AWT/Swing text component.
Jul 17 '05 #2

"hiwa" <HG******@nifty.ne.jp> wrote in message
news:68**************************@posting.google.c om...
"Doug van Vianen" <co*****@shaw.ca> wrote in message

news:<Jud2c.691729$JQ1.506094@pd7tw1no>...
Hi,
I am working on an Applet which provides some mouse practice for
new computer users in our local seniors' computer club. The applet
contains several cards, in a card layout, which are displayed to
the user one after the other as needed to present some particular
mouse operation. This all works fine. One card (card 4 below)
includes a textarea and a button. What I wish to do is have the user
use the mouse to select some text from the textarea and click the button. I wish to detect what the user highlighted and store it as a string to
check.
However I am having a problem doing this. Parts of the program are below:
import java.applet.*;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.*;
import java.util.*;
import java.awt.FontMetrics;
import java.awt.Font;
import java.awt.event.*;
import java.awt.image.*;
public class testmouse extends java.applet.Applet implements Runnable,
MouseListener, AdjustmentListener, ActionListener {
.
.
public void init() {
.
.
//create card four
p4=new myPanel(bgndm,new FlowLayout(),300,100);
bstext="Here is some text that is to be used\n";
bstext=bstext+"to select some text from by\n";
bstext=bstext+"highlighting it using the mouse.\n";
bstext=bstext+"This is a very useful, common operation.\n";
TextArea ts=new TextArea(bstext,4,30,TextArea.SCROLLBARS_NONE);
ts.addActionListener(this); // Do I need this?
bs=new Button("OK");
p4.add(ts);
p4.add(bs);
.
.
//create background card and add other cards to it
p0=new myPanel(new Color(0).gray,new CardLayout(0,0),300,100);
setLayout(new FlowLayout());
add(p0);
p0.add("card1",p1);
p0.add("card2",p2);
p0.add("card3",p3);
p0.add("card4",p4); // This is the card with the textarea and button.
((CardLayout)p0.getLayout()).show(p0, "card1");
p0.setVisible(false);
// set all card invisible until each needed
.
.
public void paint(Graphics g) {
.
.
}

public void actionPerformed(ActionEvent tevt) { // I tried this if (tevt.getSource()== ts) { // after discovering
txtval=ts.getSelectedText(); // that thecode below
}
// would not work
}
//
.
.
public boolean action(Event evt, Object arg) { //
whichbut="";
// This was my
if (evt.target instanceof Button) { // original try.
whichbut = ((Button)evt.target).getLabel(); // It
detects the

// use of the
if (whichbut.equals("OK")) { //
buttons ok but
txtval=ts.getSelectedText(); // the getSelectedText
theop="P";
// statement does not
}
// work. (The check
repaint();
// for other buttons
}
// is not shown.)
}
//
.
.

return true;
}

When I detect that the OK button is clicked I wish to set the string
variable txtval to be the highlighted text, then check to see if it is
the correct text and use repaint to display to the user how he
or she did. The paint part works okay so I have not shown it.

Can someone tell me how, after I detect that the OK button is clicked,
I can set the string txtval to be the selected text? Thank you.

Doug van Vianen
co*****@shaw.ca


From your code above we can see:
(1)Button bs doesn't have its event handler.
(2)Textarea's event handler is actionPerformed. But text selection
does not trigger ActionEvent on Java AWT/Swing text component.


Thank you for your response.

I can add an event handler for the button and that works fine. I do not
expect to have the TextArea trigger an event. But can I not use
ts.getSelectedText() to set a string to have the value of the text in the
textarea that was selected by the user when it is detected that the button
was clicked on?

Doug van Vianen
co*****@shaw.ca
Jul 17 '05 #3
> But can I not use ts.getSelectedText() to set a string to have
the value of the text in the textarea that was selected by the
user when it is detected that the button was clicked on?


OK. It does work as follows:
[code]
import java.awt.*;
import java.awt.event.*;

public class Selected implements ActionListener{
TextArea ta;
TextField tf;

public Selected(){
Frame frame = new Frame();
//make the content string just ONE STRING before compile
//comp.lang.java format fragments it! No good!
ta = new TextArea("I can add an event handler for the button and
that works fine. I do not expect to have the TextArea trigger an
event. But can I not use ts.getSelectedText() to set a string to have
the value of the text in the textarea that was selected by the user
when it is detected that the button was clicked on?",
20, 60, TextArea.SCROLLBARS_VERTICAL_ONLY);
Button bt = new Button("COPY");
tf = new TextField(60);

frame.setLayout(new BorderLayout());

frame.add(tf, BorderLayout.NORTH);
frame.add(ta, BorderLayout.CENTER);
frame.add(bt, BorderLayout.SOUTH);

bt.addActionListener(this);

frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});

frame.pack();
frame.show();
}

public void actionPerformed(ActionEvent e){
tf.setText(ta.getSelectedText());
}

public static void main(String[] args){
new Selected();
}
}
/code]
Jul 17 '05 #4
Hi,

Thank you very much for the code. I now have it working fine--until the next
problem. This group has been a very great help.

Doug van Vianen
co*****@shaw.ca
OK. It does work as follows:
[code]
import java.awt.*;
import java.awt.event.*;

public class Selected implements ActionListener{
TextArea ta;
TextField tf;

public Selected(){
Frame frame = new Frame();
//make the content string just ONE STRING before compile
//comp.lang.java format fragments it! No good!
ta = new TextArea("I can add an event handler for the button and
that works fine. I do not expect to have the TextArea trigger an
event. But can I not use ts.getSelectedText() to set a string to have
the value of the text in the textarea that was selected by the user
when it is detected that the button was clicked on?",
20, 60, TextArea.SCROLLBARS_VERTICAL_ONLY);
Button bt = new Button("COPY");
tf = new TextField(60);

frame.setLayout(new BorderLayout());

frame.add(tf, BorderLayout.NORTH);
frame.add(ta, BorderLayout.CENTER);
frame.add(bt, BorderLayout.SOUTH);

bt.addActionListener(this);

frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});

frame.pack();
frame.show();
}

public void actionPerformed(ActionEvent e){
tf.setText(ta.getSelectedText());
}

public static void main(String[] args){
new Selected();
}
}
/code]

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Csaba2000 | last post by:
How do I detect when the font size has been changed (especially by user action: either Ctrl+Scroll wheel or View/Text Size)? This is just for use on IE 5.5+, but it would be great if there was a...
3
by: Holden Caulfield | last post by:
Does anyone know how to detect if a user has pasted code using the right mouse button into a textarea? I have fields in a form that autoupdate when text is changed in a textarea, but if it is...
1
by: laredotornado | last post by:
Hello, I am trying to detect whether a user entered text in a textarea or hit the delete/backspace button (thus, erasing something). Once that is detected, I would like to set "var bSaveRequired =...
1
by: Mark Szlazak | last post by:
Is there a way to detect if an textarea onscroll event is working in Firefox (or Mozilla). I know that there is an onscroll event bubbling bug with current vesions of these browsers so I want to...
1
by: IkBenHet | last post by:
Hello, I found this script to create a simple rich text form (http://programmabilities.com/xml/index.php?id=17): <html> <head> <title>Rich Text Editor</title> </head> <body>
14
by: Gary Shell | last post by:
I have five comboboxes each has a item listed populated by hand in the IDE. Each has the text property bound to a text field in the database. When I initially fill the dataset and display the page...
3
by: brisco5 | last post by:
I have a TEXTAREA element. A user right clicks within in to get the context menu and they select "paste". I want my javascript code to know that they selected "paste". I know you can capture the...
3
by: njuneardave | last post by:
Hey, Here is a quick question: I have two forms that I am using, and I need to highlight the text in Form1 by clicking and dragging (obviously). So, when I switch to Form2 (making it the focused...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.