• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 4776
52
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: 4776
52
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: 4776
52
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.
 
Rancher
Posts: 2835
96
  • 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!