Results 1 to 7 of 7
  1. #1
    Embercloud is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default 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();
    			}
    		}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    Embercloud is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Embercloud View Post
    Do I need to do something with the defaultlistmodel before printing it?
    The objects loaded into the model have toString methods but if I just tell the printwriter to print the whole model, it wouldn't work, right?
    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.

  6. #6
    Embercloud is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  1. Help in Printing
    By kirly in forum Advanced Java
    Replies: 3
    Last Post: 10-03-2011, 03:40 PM
  2. Wierd delay in DefaultListModel.addElement()
    By DaedalusAlpha in forum AWT / Swing
    Replies: 4
    Last Post: 04-19-2010, 07:51 AM
  3. DefaultListModel Elements
    By jboy in forum New To Java
    Replies: 7
    Last Post: 10-24-2009, 03:23 AM
  4. Replies: 3
    Last Post: 04-24-2009, 06:28 PM
  5. Printing Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •