By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Detecting highlighted text in textarea.

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
"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

P: n/a

"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

P: n/a
> 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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.