简体   繁体   中英

How to display object in a JTextArea?

I have a Paper class that has 4 attributes.

public class Paper(int id, String author, String title, int rank){

}

Then I have a LinkedList that contains the paper object.

I have a GUI that displays the paper object in the JTextArea. GUI has buttons that let users to add paper, delete and save the file. When users press save button the Paper objects are saved in a txt file as follows:

46,Evolutionary Comp,Michael Smith,12/01/10,4 
61,Fuzzy Logic and App,John Peterson,13/01/10,3 
118,Neural Networks,Arthur London,20/01/10,5 
200,Evolutionary Comp,Scott Jones,30/01/10,1 
210,Fuzzy Logic and App,Joe Wang,01/02/10,4 

How do I display the content of txt file in the the JTextArea. Paper class has toString() method that prints the data of Paper Object.

Thanks. If the question is not very clear please make a comment and I will try and make it as clear as possible.

I haven't actually tried this myself, but it would seem you could use JTextArea.append(String) or JTextArea.insert(String, int).

Details: http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html

In general, if you want to know what you can do with a given type Foo, try googling "java Foo" and javadoc pages usually show up as the first few hits.

JTextarea.append(string) worked perfectly for me. Thanks

// addPaper() returns paper object
displayTxtArea.append(addPaper().toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM