Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2005

Tip: Looking for answers? Try searching our database.

TextListener Event

Thread view: 
Ron Barnhart - 25 Jul 2005 09:16 GMT
Hi,

I'm working on a program for a class assignment and ran into a problem.  The program is essentially finished, I just need to add one additional feature to it.  The program is split up into two classes: one that handles creating the GUI (MortgageDisplay) and the other that performs calculations and manipulates the GUI (MortgageCalc).  What I want to do is add a listener that will tell me when text has been entered into a JTextField.

The program is a simple mortgage calculator.  I have two ways for the user to enter information.  They can either choose between three pre-selected mortgage terms from a JComboBox or type in the terms (rate and number of years) for the mortgage.  What I want to accomplish is to deactivate the combo box when text is entered into either of the term text fields (txtRate and txtYears).

The Java documentation says this about adding a text listener for a JTextCompent:

DocumentListener myListener = ??;
JTextField myArea = ??;
myArea.getDocument().addDocumentListener(myListener);

This doesn't make any sense because DocumentListener is an Interface and cannot be instantiated.  The compiler gives an error if you try.  Even if it did work, I do not think it would work correctly.  Because the program is separated into two classes, all of the other action listeners are setup like this:

calculate.addActionListener (calculations);  //'calculations' is an object of type MortgageCalc that links the listeners defined in MortgageDisplay class. 'calculate' is a JButton.

The above code for the DocumentListener might work for a single class, but probably not across two classes.  I've tried several variations with no success.

I did try an ActionListener, but that didn't work either.  This is what I did:

//In the MortgageDisplay class

txtRate.addActionListener (calculations);
txtYears.addActionListener (calculations);

//In the MortgageCalc class

public void actionPerformed (ActionEvent e) {

   if (e.getSource ( ) == txtRate) {
       userInterface.comboSelectTerms.setEnabled (false); //'userInterface' is an object of type MortgageDisplay
    }

   if (e.getSource ( ) == txtYears) {
       userInterface.comboSelectTerms.setEnabled (false);
   }
}

I figured that the keypress events would be caught when the user typed into the textboxes, but I was wrong.  The only listener defined in JTextField is ActionLister (i.e. addActionListener).  The other listeners (such as key listeners) are defined in the awt.  AFAIK, you can't implement an awt event listener on a JComponent.  Any recommendations or links to additional info?

Signature

Ron Barnhart

Andrew Thompson - 25 Jul 2005 09:39 GMT
> I'm working on a program ..

Your post shows as 43 lines, but reads as 186 in
my newsreader due to the (missing) attachment.

Please do not post attachments to the comp.lang.java.*
groups - they are not set up to accept attachments, and
most servers strip them (as does mine).

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Voted "Best"

Ron Barnhart - 25 Jul 2005 17:06 GMT
Hi Andrew,

> Please do not post attachments

I'm not sure what you're referring to.  There were no attachments in my
post.  It was straight text.    I'm not sure why you're not receiving part
of it.  I can send a copy to you by email if you like.

Let me know. Thanks.

Signature

Ron Barnhart

Andrew Thompson - 25 Jul 2005 18:36 GMT
> Hi Andrew,
>
>> Please do not post attachments
>
> I'm not sure what you're referring to.  There were no attachments in my
> post.  

My bad, on closer inspection it seems to be (have been)..

> ..It was straight text.    

..some form of HTML, according to my best guesses.

Here is your first post as it appears in the Google archive.  
<http://groups.google.com.au/group/comp.lang.java.gui/msg/4af6e6f8189a87bf?dmode=
source
>

Note how the content type is listed as multi-part mime,
and the text appears again, lower, wrapped in HTML?

>..I'm not sure why you're not receiving part of it.  

Your news client is now sending posts in the form usually
expected (plain text).  No biggy.

It turns out I got the entire (40ish) line text in the
first post, it's just my news reader was also counting
(but not displaying) the HTML version.

>..I can send a copy to you by email if you like.

Thanks ..no.

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
In Hypno-Vision

Ron Barnhart - 25 Jul 2005 18:57 GMT
Hi Andrew,

I think I know what happened.  My newsgroup program is set to respond in the
same format that was posted, but is defaulting to HTML for new posts.  I
have it set that way for my college classes (which is done entirely online).
Sorry for the confusion.

Signature

Ron Barnhart

Thomas Weidenfeller - 26 Jul 2005 08:37 GMT
>>Hi Andrew,
>>>Please do not post attachments
[quoted text clipped - 3 lines]
>
> My bad, on closer inspection it seems to be (have been)..

So called "attachments" and HTML are both MIME messages, and MIME is
what triggers some news servers to discard a message. It is just that
modern newsreaders like to display different mime contents different
(the whole idea behind mime :-).

Another issue is size. Below 32K is fine, between 32K and 64K it starts
to get difficult. Everything greater 64K is really problematic. The FAQ
suffers from distribution problems because it is already greater 64K.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

JediJ - 26 Jul 2005 14:32 GMT
It looks like your still looking for a little help, so I'll chime in.
I would add a focus listener to your JTextField.

This is fairly straight forward.
First you need to import the Listener and the Event that is associated
with it...

import  java.awt.event.FocusListener;
import java.awt.event.FocusEvent;

The FocusListener is an interface, so you must implement all of its
methods, even if you arent going to use them for anything.

There are quite a few different strategies for doing the Listeners.
For something like this, I would make it an inner class. Something
like:

    class FocusHandler implements FocusListener
    {
       public void focusGained(FocusEvent fe)
       {
       }
       public void focusLost(FocusEvent fe)
       {
       }
    }
In the constructor of your GUI class, create an instance of your
FocusHandler:
FocusHandler myFocusHandler = new FocusHandler();
then add it to your components...
yourComponent.addFocusListener(myFocusHandler);

The last thing you will need to do is add the logic to the methods of
the FocusHandler class.  You can find out what component has the Focus
using fe.getSource();
So you would do something like
if(fe.getSource() == myTextField)
{  //disable/enable stuffs}
else if(fe.getSource() == myComboThingy)
{ //disable/enable other stuffs}

Hope that helps.
JediJ - 26 Jul 2005 14:40 GMT
One other thing that I just noticed about your post...while JComponent
is a swing component, it is a direct decendant of Container, which is a
decendant of Component, both of which are AWT.  Thus, you inherit all
functionality of the awt components (including the listeners).
Ron Barnhart - 27 Jul 2005 05:15 GMT
Hi JediJ,

Thank you for your help with this.  I thought about using focus as possible
way to do this, but I didn't know how to implement it.

>while JComponent
> is a swing component, it is a direct decendant of Container, which is a
> decendant of Component, both of which are AWT.  Thus, you inherit all
> functionality of the awt components (including the listeners).

That thought occurred to me, but I couldn't figure out how to take advantage
of it.  I've spent so much time hopping around the Java documentation, it's
making my head spin.  I'll look into both of these options and see if I can
make it work.

Thanks again for your help.  I'll let you know what I come up with.

Signature

Ron Barnhart



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.