aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Alphabetizing a JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Alphabetizing a JTextArea" Watch "Alphabetizing a JTextArea" New topic
Author

Alphabetizing a JTextArea

S. Shin
Greenhorn

Joined: Apr 02, 2009
Posts: 4
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

Joined: Oct 13, 2005
Posts: 32604
    
    4
This sort of question is usually discussed on our Swing forum. Moving.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16482
    
    2

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

Joined: Apr 02, 2009
Posts: 4
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

Joined: Feb 23, 2007
Posts: 1561
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)
 
I agree. Here's the link: http://jrebel.com/download
 
subject: Alphabetizing a JTextArea
 
Similar Threads
Mouse clicking
JList selection to int not working
Table added to SQL database, but with empty records.
Browse and Load a Text File
JButton Action Listener Problem