The Artima Developer Community
Sponsored Link

Java Answers Forum
printing an array in a text field

1 reply on 1 page. Most recent reply: Dec 13, 2002 3:16 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
mary

Posts: 5
Nickname: marymary
Registered: Nov, 2002

printing an array in a text field Posted: Dec 13, 2002 7:10 AM
Reply to this message Reply
Advertisement
I have returned an array to the action performed method and I need to print it to a TextField. I know I could use a StringBuffer to do this, but is there any other way to loop through the array and print each element to a textfield


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: printing an array in a text field Posted: Dec 13, 2002 3:16 PM
Reply to this message Reply
A JTextField (or TextField) is a single-line edit box, so you have to first decide how you want to separate the strings in the array. After you have made that decision, it is a simple matter to do as you intimated and gather all the lines together in a StringBuffer, then use one call to setText(). You could do it without the StringBuffer (this is what you were asking, I think) by doing a loop something like this:
if( strings.length > 0 )
{
   editBox.setText( strings[0] );
   String sep = " ";  // Space, or whatever you like.
   for( int i = 1; i < strings.length; i++ )
      editBox.setText( editBox.getText() + sep + strings[i] );
}
else
   editBox.setText( "" );


Of course, this second method is grossly inefficient, especially if there are several billion strings in the array. Hmm... that isn't too likely now is it? So either method is equally acceptible.

Flat View: This topic has 1 reply on 1 page
Topic: ANother  try(interesting prob) Previous Topic   Next Topic Topic: ANother  try(interesting prob)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use