File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Listener/Events Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Listener/Events Help" Watch "Listener/Events Help" New topic

Listener/Events Help

Nate Smith

Joined: Jan 25, 2005
Posts: 3

I am pretty much a swing/awt newbie but feel I have a good grasp on Java itself. I am designing a log in type window and have the text fields and buttons layed out. I want the program to do something(right now just print the contents of the text field to standard output) when I click the 'OK' button. When I enter in stuff into the first text field, i only get the desired effect when I push enter. When I click the button I just get a bunch of errors. So my question is how can I "connect" the two text fields and the 'OK' button?

Mike Southgate
Ranch Hand

Joined: Jul 18, 2003
Posts: 183
First you need to create an actionListener :

then you have to associate it with your button:


ms<br />SCJP, SCJD
David Mace
Ranch Hand

Joined: May 26, 2004
Posts: 35
In doing something like this, you're not actually "connecting" the buttons with the JTextFields. If you would post your code to see how you're pulling the test from the textfields, we'll have a look at it and can probably tell you what you're doing wrong.

But basically all you would have to actually code to do what you're describing is something similar to the following:

Assuming your username field and password fields are userField & passField, just call the .getText() methods on the JTextFields themselves in the action events assigned to the JButton.

//inside action listener

It is a simple reply to your question, but if you post the code, we could more specifically....and don't forget your code tags.


Dad always thought laughter was the best medicine, which I guess is why several of us died of tuberculosis. -Jack Handy
Nate Smith

Joined: Jan 25, 2005
Posts: 3
I just came back to this site today to check the post and by that time I had already fixed the problem on my own. The way I did it was pretty much the same way as described in David Mace's post. Thanks for the help
Nate Smith

Joined: Jan 25, 2005
Posts: 3
Hi, I've got another question now. How can I make another window/pane I have designed open up when I click the ok button on my login screen?
I agree. Here's the link:
subject: Listener/Events Help
It's not a secret anymore!