Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Alphabetizing a JTextArea  RSS feed

 
S. Shin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the title implies I'm working on an assignment where I have to create a program that displays a window, has buttons that feed text to a text area (that is editable), and must then be able to write the list to a file, clear the list, and sort the left alphabetically. I've finished every thing pretty much and it's all working perfectly save for the sorting portion. I'm trying to wrap my head around how to do this part. Would I somehow load the text from the text area into an array and then alphabetize it that way, or is there a more straight forward way to do it. If I were to create the array how would I go about getting the text from the text area? I can easily alphabetize the list once I have it in an array, but I'm unsure about how to get it from the text area and then back into it in the proper order.

Here's my code so far:

Thanks for any help you guys can throw my way.
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sort of question is usually discussed on our Swing forum. Moving.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S. Shin wrote:Would I somehow load the text from the text area into an array and then alphabetize it that way, or is there a more straight forward way to do it. If I were to create the array how would I go about getting the text from the text area? I can easily alphabetize the list once I have it in an array, but I'm unsure about how to get it from the text area and then back into it in the proper order.

Yeah, you would somehow do that. You can get the text from a JTextArea via its getText() method and you can put it back via its setText() method.

I don't know what it means to alphabetize text, or to put it into an array, but that's just from lack of specifications. I'm sure you have the actual specs. There isn't any Swing component to that part of the problem.
 
S. Shin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using the getText() how exactly is it retrieved? In order to load it into an array I would assume I'd have to get each line individually, is there a way to do that? Like a get line # and do an if else to see if the line is empty?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using the getText() how exactly is it retrieved?

It is retrieved as one large String. The new lines are separated by "\n" characters, I believe.
In order to load it into an array I would assume I'd have to get each line individually, is there a way to do that?


could work. Note the double backslash (escaped backslash).

You could then place this into a List<String> and sort via Collections.sort(myList)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!