Strange issue with addTextListener

Here is my code

Class1 implements TextListener:

TextFiled txtComp = new TextFiled("");

void TextValueChanged(event){

System.out.println("Reaching here");

}

Class2:

populateFileds(){

txtComp.setText("sample");

Class1.txtComp.addTextListener(Class1);

}

==================

Though the addTextListener is called after setText, its triggering textValueChanged Event unnecessarily..

Please HELP ME !!

[480 byte] By [smajetia] at [2007-11-26 16:52:00]
# 1
What is a TextListener?Please post a working example.
zadoka at 2007-7-8 23:19:44 > top of Java-index,Java Essentials,Java Programming...
# 2
TestListener is to keep track of events when we change the text in the TextField component. My issue is..: though I add that listener after setText, its triggering an event.
smajetia at 2007-7-8 23:19:44 > top of Java-index,Java Essentials,Java Programming...
# 3

> TestListener is to keep track of events when we

> change the text in the TextField component. My issue

> is..: though I add that listener after setText, its

> triggering an event.

Ok. Based on what you have told me I can only guess. Please

create a Short,Self Contained, Compilable and Executable, Example Program.

And when you post it, please use code tags.

zadoka at 2007-7-8 23:19:44 > top of Java-index,Java Essentials,Java Programming...
# 4

Have you added the listener to another component? You should use this in the textValueChanged() method to distinguish between different components that might trigger the TextEvent

if (event.getSource() == txtComp)

// do whatever you want when text in txtComp is altered

Jukka

duckbilla at 2007-7-8 23:19:44 > top of Java-index,Java Essentials,Java Programming...
# 5

Hi Here are three class files..just save in same directory , compile and run TestFrame:

GUI logic is causing setting of Text and then registering the listener.

Use the button to issue setText call on TextFiled component.You can even see the System Prints order..

My main aim is to trigger event only on user changing the text field using keyboard. When setText called programmatically , it should not trigger txtValueChagned event. Thats why i am trying to add Listener after setText to some default value.

1. TxtPanel.java

-

import java.awt.Button;

import java.awt.Component;

import java.awt.Label;

import java.awt.Panel;

import java.awt.BorderLayout;

import java.awt.TextField;

import java.awt.event.TextEvent;

import java.awt.event.TextListener;

/**

* @author Srinivasu Majeti

*/

public class TxtPanel extends Panel implements TextListener{

TextField txtUserName = new TextField();

Button btn = new Button("Issue setText");

Label lbl = new Label();

private static final long serialVersionUID = 1;

public TxtPanel() {

super();

initComponents();

}

private void initComponents() {

setLayout(null);

add(txtUserName);

txtUserName.setBounds(10,10,200,24);

add(btn);

btn.setBounds(10,50,200,24);

add(lbl);

lbl.setBounds(10,150,200,24);

txtUserName.setText("Just a Check");

}

public void textValueChanged(TextEvent e) {

if(e.getSource() == txtUserName){

lbl.setText("textValueChanged(e) called");

System.out.println("textValueChanged(e) called");

}

}

public void registerListener(){

System.out.println("registerListener()");

txtUserName.addTextListener(this);

}

}

=================================

2. GuiLogic.java

-

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class GuiLogic implements ActionListener {

TxtPanel _txtPanel ;

public GuiLogic(TxtPanel pnlTest) {

_txtPanel = pnlTest;

}

public void actionPerformed(ActionEvent e) {

System.out.println("actionPerformed(event) called");

populateTextFiled();

}

private void populateTextFiled() {

_txtPanel.txtUserName.setText("From populate");

System.out.println("populateTextFiled() called");

_txtPanel.registerListener();

}

}

==================================

3.TestFrame.java

-

import java.awt.CardLayout;

import java.awt.Frame;

import java.awt.event.ComponentAdapter;

import java.awt.event.ComponentEvent;

import javax.swing.JFrame;

public class TestFrame extends JFrame{

public TestFrame(java.awt.Frame parent, boolean modal) {

setSize(250,250);

setTitle("Sample GUI Container");

setVisible(false);

initComponents();

}

private void initComponents() {

TxtPanel pnlTest = new TxtPanel();

GuiLogic logic = new GuiLogic(pnlTest);

pnlTest.btn.addActionListener(logic);

getContentPane().add(pnlTest);

getContentPane().setLayout(new CardLayout());

}

public static void main(String args[]) {

String filePath="";

if(args != null && args.length != 0)

filePath = args[0];

new TestFrame(new javax.swing.JFrame(), true).show();

}

}

Message was edited by:

smajeti

Message was edited by:

smajeti

smajetia at 2007-7-8 23:19:44 > top of Java-index,Java Essentials,Java Programming...
# 6

I see that the textValueChanged() after the component is registered. I am understanding that you don't want this to happen.

However, I notice that it is only called if you call setText() first. (If you comment out the set text before you add the actionlistener, this doesn't happen).

Don't know if that solves your problem? But why aren't you using Swing anyway?

zadoka at 2007-7-8 23:19:44 > top of Java-index,Java Essentials,Java Programming...
# 7
Thanks for your reply. But my doubt is: setText is called before registering listener. Then how do setText trigger an event with the listener . I thought those events wont be tracked untill we register a listener.
smajetia at 2007-7-8 23:19:44 > top of Java-index,Java Essentials,Java Programming...
# 8
Don't mix Swing and AWT.And post in Abstract Window Toolkit (AWT) forum.
Rodney_McKaya at 2007-7-8 23:19:44 > top of Java-index,Java Essentials,Java Programming...
# 9
Continued here: http://forum.java.sun.com/thread.jspa?threadID=5131492&tstart=0
zadoka at 2007-7-8 23:19:44 > top of Java-index,Java Essentials,Java Programming...