JFileChooser and create/read/write/edit a file

Posted on 2009-04-20
Last Modified: 2012-05-06

I am working on Java Swing program and I am wondering if I can create/write a file from user input and write/edit to that file by using JFileChooser.

Question by:dkim18
    LVL 92

    Assisted Solution

    JFileChooser is just used for file selection, you need to handle reading and writing yourself

    LVL 17

    Expert Comment

    To write text based files i find the class "PrintStream" to be the easiet for output
    PrintStream ps = new PrintStream("Sample.txt");
    ps.println("AnyString can be written " + 123 + " or number");
    ps.println("It functing like System.out....");

    Open in new window


    Author Comment

    Ok, I am able to read from a file and put string array and added JList. Now, JScrollpane is empty initially and how do I refresh after reading from a file and fill string array?

    String []contacts = new String[list.size()];
    	    for(int i=0; i< list.size(); i++){
    	    	contacts[i] = (String)list.get(i);
    personList = new JList();//initially  empty
    jScrollpane = new JScrollPane(personList);
    //from other class I read from a file and trying to set personList and refresh jScrollpane.

    Open in new window

    LVL 17

    Accepted Solution

    After repopulating the JList with the data you may need to call repaint on your frame to make sure it repaints.

    As for the repopulating you could iterate through your list and then add each string to the JList. However the far better way is to implement ListModel. However, a ListModel really isnt necessary if the list will never change and is read only.
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    public class SampleList extends JFrame
    	ArrayList<String> data = new ArrayList<String>();
    	ArrayListModel listModel = new ArrayListModel(data);
    	SampleList() throws Exception
    		Scanner s = new Scanner(new File("sample.txt"));
    		JList list = new JList(listModel);
    		JScrollPane scroll = new JScrollPane(list);
    	public static void main(String args[]) throws Exception
    		new SampleList();
    class ArrayListModel extends AbstractListModel
    	ArrayList<String> data;
    	ArrayListModel(ArrayList<String> data)
  = data;
    	public int getSize()
    		return data.size();
    	public String getElementAt(int i)
    		return data.get(i);
    	public void changed()

    Open in new window


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Problem to setup 18 67
    copyEvens challenge 6 47
    userCompare  challenge 3 55
    JVM encoding. How to change encoding. 27 55
    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    The viewer will learn how to implement Singleton Design Pattern in Java.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now