Total members 11891 |It is currently Thu Jul 18, 2024 10:14 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Text Listener for events TextListener-on TextArea
java code
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class TextEventExample extends Applet
implements ActionListener {
TextField textObj;
TextArea textAreaObj1;
TextArea textAreaObj2;

public void init() {
Button button = new Button("Empty");
button.addActionListener(this);

textObj = new TextField(25);
textObj.addActionListener(new MyCustomActListener());
textObj.addTextListener(new MyCustomTxtListener("Text Field"));

textAreaObj1 = new TextArea(5, 20);
textAreaObj1.addTextListener(new MyCustomTxtListener("Text Area"));

textAreaObj2 = new TextArea(5, 20);
textAreaObj2.setEditable(false);

GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints gridBagConstrain = new GridBagConstraints();
setLayout(gridbag);
gridBagConstrain.fill = GridBagConstraints.BOTH;
gridBagConstrain.weightx = 1.0;


Panel leftPanel = new Panel();
leftPanel.setLayout(new BorderLayout());
leftPanel.add("North", textObj);
leftPanel.add("Center", textAreaObj1);

gridBagConstrain.gridheight = 2;
gridbag.setConstraints(leftPanel, gridBagConstrain);
add(leftPanel);

gridBagConstrain.weighty = 1.0;
gridBagConstrain.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstrain.gridheight = 1;
gridbag.setConstraints(textAreaObj2, gridBagConstrain);
add(textAreaObj2);

gridBagConstrain.weighty = 0.0;
gridbag.setConstraints(button, gridBagConstrain);
add(button);
// set focus to it.
textObj.requestFocus();
}

class MyCustomTxtListener implements TextListener {
String preface;
String newline;

public MyCustomTxtListener(String source) {
lineSeparator = System.getProperty("line.separator");
preface = source
+ " text value changed."
+ lineSeparator
+ " First 10 characters: \"";
}

public void textValueChanged(TextEvent e) {
TextComponent tc = (TextComponent)e.getSource();
String s = tc.getText();
try {
s = s.substring(0, 10);
} catch (StringIndexOutOfBoundsException ex) {
}

textAreaObj2.append(preface + s + "\"" + lineSeparator);

//Scroll down, unless the peer still needs to be created.
if (textAreaObj2.isValid()) {
textAreaObj2.setCaretPosition(java.lang.Integer.MAX_VALUE);
}
}
}

class MyCustomActListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
int selStart = textAreaObj1.getSelectionStart();
int selEnd = textAreaObj1.getSelectionEnd();

textAreaObj1.replaceRange(textObj.getText(),
selStart, selEnd);
textObj.selectAll();
}
}


public void actionPerformed(ActionEvent e) {
textAreaObj2.setText("");
// set focus to it.
textObj.requestFocus();
}
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Text Listener for events TextListener-on TextArea-TextField
 Button action listener should implement abstract method     -  
 check keyboard events using C++     -  
 How can a GUI component handle its own events     -  
 Window events-handling by WindowListener     -  
 Handle Focus events with FocusListener     -  
 relationship an event-listener interface & event handler     -  
 Able to Copy Text from Uneditable Text Boxes(JTextfields)     -  
 Java- Copy text area into disabled text area     -  
 Get Text Field value by php     -  
 i tag for italic text     -  



Topic Tags

Java AWT, Java Applet
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com