Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Listener/Events Help

 
Nate Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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?

Thanks!
 
Mike Southgate
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you need to create an actionListener :


then you have to associate it with your button:


ms
 
David Mace
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
System.out.println(userField.getText());
System.out.println(passField.getText());

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.

HTH,
David
 
Nate Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic