• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting text from a JTextField  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Building a program that the user enters some text into a JTextField and then presses a button and the text is displayed in the JPanel. I'm struggling with the code for it. ive tried paper.getText() but that comes up with an error. Also paper.drawString("" + text, 30, 30) but again this comes with an error.

Thanks for looking.
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is "paper"? Could you please post your code. And, when posting code please enclose the code in "Code" tags. You do that in the post editor by first highlighting just the code part and then clicking on the button marked "Code" just above the edit window.
 
Ryan O'Mara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a basic code really, seem to be struggling with this haha.

 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you're attempting to do here.

If you want to get the text from the JTextField, in your code it would be text.getText().
 
Ryan O'Mara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Not sure what you're attempting to do here.

If you want to get the text from the JTextField, in your code it would be text.getText().


Tried that but it displays nothing.
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this

 
Ryan O'Mara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Try this



Thank you, it works as should now.
 
Master Rancher
Posts: 2045
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works, but this method has some drawbacks. For instance, after the text is displayed, resize your panel (drag one of the sides). Or, try "aaaaaa" as your first text, then "bb" as your second try.

To correct for this, you should draw the string in the paintComponent-method of the panel. To make this possible, create your panel like this:

where textFromTextfield is a String member, initially set to "". Your actionLstener could be like:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!