[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JFileChooser and create/read/write/edit a file

Posted on 2009-04-20
4
Medium Priority
?
329 Views
Last Modified: 2012-05-06
Hi,

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.

thx,
0
Comment
Question by:dkim18
  • 2
4 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 24190492
JFileChooser is just used for file selection, you need to handle reading and writing yourself

0
 
LVL 17

Expert Comment

by:Thomas4019
ID: 24190643
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....");
ps.close();

Open in new window

0
 

Author Comment

by:dkim18
ID: 24196973
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

0
 
LVL 17

Accepted Solution

by:
Thomas4019 earned 1600 total points
ID: 24199627
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.*;
import java.io.*;
 
public class SampleList extends JFrame
{
	ArrayList<String> data = new ArrayList<String>();
	ArrayListModel listModel = new ArrayListModel(data);
 
	SampleList() throws Exception
	{
		setSize(320,240);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
 
		Scanner s = new Scanner(new File("sample.txt"));
 
		while(s.hasNextLine())
			data.add(s.nextLine());
 
		JList list = new JList(listModel);
 
		JScrollPane scroll = new JScrollPane(list);
		add(scroll);
 
		setVisible(true);
 
		while(true)
		{
			Collections.shuffle(data);
			listModel.changed();
			repaint();
			Thread.sleep(1000);
		}
	}
 
	public static void main(String args[]) throws Exception
	{
		new SampleList();
	}
}
 
class ArrayListModel extends AbstractListModel
{
	ArrayList<String> data;
 
	ArrayListModel(ArrayList<String> data)
	{
		this.data = data;
	}
 
	public int getSize()
	{
		return data.size();
	}
 
	public String getElementAt(int i)
	{
		return data.get(i);
	}
 
	public void changed()
	{
		this.fireContentsChanged(this,0,data.size()-1);
	}
}

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

872 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