AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

notified with changes text in JTextField

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Madhur Ahuja
Guest





PostPosted: Mon Aug 23, 2004 7:21 pm    Post subject: notified with changes text in JTextField Reply with quote




Hello

Does JTextField invokes the listener, when the user changes the text in it.
I am trying to disable a button, based on the content in the JTextField, but
the listener method is not being invoked at all.

Compilable code:


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.net.*;
import java.beans.*;


class mainapp implements ActionListener
{

JPanel mainpanel;
JTextField from,to,server; file://these are the text fields which are to
be monitored
// JFormattedTextField from,to,server;
JTextArea body,result;
JFrame mainframe;
GridBagLayout gbc;
GridBagConstraints constraints;
JButton send;
JLabel lfrom,lto,lserver,lbody,lresult;
public mainapp()
{
gbc=new GridBagLayout();
mainpanel=new JPanel(gbc);
constraints = new GridBagConstraints();

mainframe =new JFrame("Mail send");

addwidgets();

mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// mainframe.setSize(new Dimension(120, 40));
mainframe.getContentPane().add(mainpanel);
mainpanel.setSize(400,400);

mainframe.pack();
mainframe.validate();
mainframe.setVisible(true);
}


void addwidgets()
{
from=new JTextField(10);
// from=new JFormattedTextField();
from.setColumns(10);

body=new JTextArea(10,30);

to=new JTextField(10);
file://to=new JFormattedTextField();
to.setColumns(10);
server=new JTextField(10);
file://server=new JFormattedTextField();
server.setColumns(10);

send=new JButton("Send");
send.setEnabled(false);
result=new JTextArea(4,30);

lfrom=new JLabel("From:");
lbody=new JLabel("Body:");
lto=new JLabel("To:");
lserver=new JLabel("Server:");

lresult=new JLabel("Result:");

from.addActionListener(this);
// from.addPropertyChangeListener("text",this);

file://body.addActionListener(this);
to.addActionListener(this);
server.addActionListener(this);
send.addActionListener(this);
file://result.addActionListener(this);

buildConstraints(constraints,0,0,1,1,80,10);
gbc.setConstraints(lfrom, constraints);
mainpanel.add(lfrom);

buildConstraints(constraints,1,0,1,1,20,10);
gbc.setConstraints(from, constraints);
mainpanel.add(from);

buildConstraints(constraints,0,3,1,1,0,10);
gbc.setConstraints(lbody, constraints);
mainpanel.add(lbody);


buildConstraints(constraints,1,3,1,1,0,10);
gbc.setConstraints(body, constraints);
mainpanel.add(body);


buildConstraints(constraints,0,1,1,1,0,10);
gbc.setConstraints(lto, constraints);
mainpanel.add(lto);


buildConstraints(constraints,1,1,1,1,0,10);
gbc.setConstraints(to, constraints);
mainpanel.add(to);


buildConstraints(constraints,0,2,1,1,0,10);
gbc.setConstraints(lserver, constraints);
mainpanel.add(lserver);


buildConstraints(constraints,1,2,1,1,0,10);
gbc.setConstraints(server, constraints);
mainpanel.add(server);

buildConstraints(constraints,1,4,1,1,0,0);
gbc.setConstraints(send, constraints);
mainpanel.add(send);


buildConstraints(constraints,0,5,1,1,0,10);
gbc.setConstraints(lresult, constraints);
mainpanel.add(lresult);

buildConstraints(constraints,1,5,1,1,0,10);
gbc.setConstraints(result, constraints);
mainpanel.add(result);

}



public static void main(String args[])
{
new mainapp();

}
public void propertyChange(PropertyChangeEvent evt)
{
Object src=evt.getSource();
if(src==from)
{
System.out.println("dffd");
}
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource().getClass()==JTextField.class)
{
System.out.println("dfd");
JTextField obj=(JTextField)e.getSource();
if(obj.getText()=="")
{
send.setEnabled(false);
}
else
send.setEnabled(true);
}

else if(e.getSource().getClass()==Button.class)
{
if(e.getSource()==send)
{

System.out.println("dffd");
}
}

}

void buildConstraints(GridBagConstraints gbc, int gx, int gy,
int gw, int gh, int wx, int wy) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}

}

--
Winners dont do different things, they do things differently.

Madhur Ahuja
India

Homepage : http://madhur.netfirms.com
Email : madhur<underscore>ahuja<at>yahoo<dot>com





Back to top
Luc Van Bogaert
Guest





PostPosted: Mon Aug 23, 2004 8:11 pm    Post subject: Re: notified with changes text in JTextField Reply with quote



On Mon, 23 Aug 2004 19:21:13 UTC, "Madhur Ahuja" <ef (AT) df (DOT) com> wrote:

Quote:
Does JTextField invokes the listener, when the user changes the text in it.
I am trying to disable a button, based on the content in the JTextField, but
the listener method is not being invoked at all.

Check out the Document class and the DocumentListener interface.

Try this:

JTextfield tfield = new JTextField();
tfield.getDocument().addDocumentListener(this);

....

/* DocumentListener implementation */
public void changedUpdate(DocumentEvent e) {
// actions when textfield changes
}
public void insertUpdate(DocumentEvent e) {
// actions when insert occurs
}
public void removeUpdate(DocumentEvent e) {
// actions when remove occurs
}

--
Luc Van Bogaert (E-mail ROT13'ed)

via ProNews/2 & eComStation (OS/2)...
http://www.os2world.com/os2ecs

Back to top
Andrew Thompson
Guest





PostPosted: Mon Aug 23, 2004 8:17 pm    Post subject: Re: notified with changes text in JTextField Reply with quote



On Tue, 24 Aug 2004 00:51:13 +0530, Madhur Ahuja wrote:

Quote:
Does JTextField invokes the listener, when the user changes the text in it.

If you attach an ActionListener, they will fire
an event when the user presses 'enter', but not
individual keystrokes..

You might need a KeyListener..

Quote:
..Compilable code:

Not entirely, some of your lines were too long
and wrapped. Please make them short lines to avoid that.

But also..

...
Quote:
class mainapp implements ActionListener

Please get used to using the naming conventions
used by the majority of other Java programmers.

A class name would normally have each word capitalized..

Vis..
class MainApp implements ActionListener

HTH

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.