Results 1 to 7 of 7
- 01-14-2011, 03:19 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
Printing DefaultListModel to .txt
How do I print a DefaultListModel to a txt with the push of a button?
Here is what I got so far.
Java Code:public static void savetotxt (DefaultListModel d){ try { // FileWriter filer = new FileWriter("Boklista.txt"); // BufferedWriter out = new BufferedWriter(fstream); FileOutputStream f = new FileOutputStream(d); ObjectOutputStream s = new ObjectOutputStream(f); } catch (IOException e) { e.printStackTrace(); } }
-
Do you want to print the text held in the list model, since it is a text file after all? If so, then you shouldn't use an ObjectOutputStream but rather just the buffered writer and then use the methods of the list model to iterate through it, writing with the buffered writer as you go.
-
Or you can use a PrintWriter. For example, in pseudocode you'd do something like so:
Java Code:create file writer and then PrintWriter called out. You may wish to construct print writer with autoflushing on. for each item held in the JList get next item out.println(the item) end for loop close the PrintWriter in finally block.
- 01-14-2011, 03:35 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
When I assign the printwriter to print the JList, I get this
javax.swing.JList[,0,0,370x255,alignmentX=0.0,alignmentY=0.0,border= ,flags=50331944,maximumSize=,minimumSize=,preferre dSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=com.apple.laf.AquaImageFacto ry$SystemColorProxy[r=44,g=91,b=214],selectionForeground=com.apple.laf.AquaImageFactor y$SystemColorProxy[r=255,g=255,b=255],visibleRowCount=15,layoutOrientation=0]
I assume this is the stack memory code, but how do I make the writer pprint the actual contents of the list?Last edited by Embercloud; 01-14-2011 at 03:49 AM.
-
Right. If you print the whole model using an ObjectOutputStream, then you'll get a binary representation of the data, something completely inappropriate for a text file. If you print the toString representation of the list model, then you'll get an output defined by this method -- check the API for the list model as I highly doubt that this is what you want. No, you'll want to output the data held inside the list. Again, you'll likely need a for loop to iterate through the model printing each item's toString method as you iterate.
- 01-14-2011, 04:22 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
Ok, I think I solved it.
Java Code:public static void savetotxt (JList d){ try { FileWriter filer = new FileWriter("Boklista.txt"); PrintWriter out = new PrintWriter(filer); for (int i = 0; i < d.getModel().getSize(); i++) { Object item = d.getModel().getElementAt(i); out.println("Item = " + item); } out.close(); } catch (IOException e) { e.printStackTrace(); } }Last edited by Embercloud; 01-14-2011 at 04:26 AM.
-
Looks pretty good, but I would change this to declare your PrintWriter variable before the try, but initialize it in the try block as you do. Then close it in a finally block (after checking that it's not null). You don't want to take any chances of dangling resources.
Similar Threads
-
Help in Printing
By kirly in forum Advanced JavaReplies: 3Last Post: 10-03-2011, 03:40 PM -
Wierd delay in DefaultListModel.addElement()
By DaedalusAlpha in forum AWT / SwingReplies: 4Last Post: 04-19-2010, 07:51 AM -
DefaultListModel Elements
By jboy in forum New To JavaReplies: 7Last Post: 10-24-2009, 03:23 AM -
DefaultListModel being updated in another class (by reference?)
By rickyoswald in forum Advanced JavaReplies: 3Last Post: 04-24-2009, 06:28 PM -
Printing Example
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks