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]

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