473,320 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 9271
"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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.