Saving Data from a List to a File

Hi,

I have a question about saving files. What I want to do is create a list that the user can customize by adding or subtracting entries. I want these entries to be linked to certain Strings in various text fields, such that when the user clicks an "Add to List" button, the strings in several text fields are obtained and stored/associated with this particular entry. Then, I want the user to be able to save the list to a file so that when they load this file, this list and all of its attributes will be re-loaded. Any help with this would be much appreciated.

Thanks,

JOD8FY

[604 byte] By [JOD8FYa] at [2007-11-27 6:05:29]
# 1
What is the question - who will write the program for me?Your prior posts show you can do GUI programming. The storeage of information as you describe is best done as properties. See the tutorial: http://java.sun.com/docs/books/tutorial/essential/environment/properties.html
ChuckBinga at 2007-7-12 16:51:36 > top of Java-index,Desktop,Core GUI APIs...
# 2

package com.sun.edu.gui;

import java.awt.BorderLayout;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class SunExample21 extends JFrame implements ActionListener {

private JTextField firstName;

private JTextField lastName;

private ArrayList list;

private JPanel panel;

private JButton addBtn;

public SunExample21() {

super("Example");

firstName = new JTextField(10);

lastName = new JTextField(10);

list = new ArrayList();

addBtn = new JButton("Add");

panel = new JPanel();

panel.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

JLabel fnLbl = new JLabel("First Name:");

JLabel lnLbl = new JLabel("Last Name:");

Insets in = new Insets(5,5,5,5);

gbc.gridx = 0;

gbc.gridy = 0;

panel.add(fnLbl, gbc);

gbc.insets = in;

gbc.gridx = 1;

gbc.gridy = 0;

panel.add(firstName, gbc);

gbc.insets = in;

gbc.gridx = 0;

gbc.gridy = 1;

panel.add(lnLbl, gbc);

gbc.insets = in;

gbc.gridx = 1;

gbc.gridy = 1;

panel.add(lastName,gbc);

JPanel btnPanel = new JPanel();

btnPanel.add(addBtn);

addBtn.addActionListener(this);

getContentPane().add(panel, BorderLayout.CENTER);

getContentPane().add(btnPanel, BorderLayout.SOUTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pack();

}

public void actionPerformed(ActionEvent ae) {

StringBuffer sb = new StringBuffer(firstName.getText().toString());

sb.append(lastName.getText().toString());

list.add(sb);

}

public void printList() {

try

{

File outData = new File("sample.txt");

BufferedWriter write = new BufferedWriter(new FileWriter(outData));

for(int i=0;i<list.size();i++) {

write.write(list.get(i).toString());

write.newLine();

write.close();

}

}

catch (IOException e)

{

System.out.println("Exception" + e.getMessage());

}

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

new SunExample21().setVisible(true);

}

}

This is a sample program how to design a gui with gridbag layout with 5 components.

This program reads the first name & last name from textboxes and store it in the file.

enhance this program according to y0ur requirements. No validation has been done.>

AnanSmritia at 2007-7-12 16:51:36 > top of Java-index,Desktop,Core GUI APIs...
# 3

The main concept for this would be, create a JList with a default list model and add it to o JSCrollPane or whatever needed.

Then, in the event handling method for the specified button, you need to collect all textfield data (either by consecutive getText() methods in the textfields or by implementing some document listener)

Then, add this to the Vector which lies under the list model, and invoking setModel on the JList.

Then, in the event handling method for the save button, you will need to iterate through this Vector, and write all its elements to java.io.BufferedWriter (http://java.sun.com/javase/6/docs/api/java/io/BufferedWriter.html).

sztyopeka at 2007-7-12 16:51:36 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thanks everyone for all your help; I am capable of writing the application myself, I am simply looking for some guidance.

AnanSmriti, your program compiles perfectly. I did have one question, though: where is the file that stores the information saved to? Or is there a way to specify where it is saved to? Thanks again for all the help.

Best,

JOD8FY

JOD8FYa at 2007-7-12 16:51:36 > top of Java-index,Desktop,Core GUI APIs...
# 5
This file will be stored in the current directory. Change the file location if you want to save in another directory.
AnanSmritia at 2007-7-12 16:51:36 > top of Java-index,Desktop,Core GUI APIs...