1 Replies - 8192 Views - Last Post: 17 February 2010 - 06:30 AM Rate Topic: -----

#1 oha055   User is offline

  • D.I.C Regular

Reputation: 49
  • View blog
  • Posts: 273
  • Joined: 02-February 09

Writing input from JTextField to a file

Posted 17 February 2010 - 05:46 AM


I'm having some problems regarding my latest assignement at school. The application I'm making is going to take input from the user from four JTextFields. When the user press the 'Add person' button, the fields must be stored so that it is possible to add a new person. Finally, when the 'Save as' button on the file menu is pressed, all the data is going to be saved as an external text file (.csv).

I hope some of you may be able to help me with the functionality :)
(Btw, the GUI is in another class, which I didn't bother posting :P)

Posted Image

The code so far:

package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.io.*;

import view.PhoneBookGUI;

public class PhoneBookFunc implements ActionListener {

	private PhoneBookGUI 		pbGUI;
	private JMenuItem 			saveItem, closeItem, aboutItem;
	private JTextField 			nameField, companyField, phoneField, notesField;
	private JButton 			addButton;
	private JFrame				frame;
	private FileReader 			fileReader;
	private BufferedReader 		inputReader;
	private FileWriter 			fileWriter;
	private BufferedWriter 		outputWriter;
	private File outputFile		= new File("Output.csv");

	public PhoneBookFunc(PhoneBookGUI pbGUI) {

		this.pbGUI = pbGUI;
		this.saveItem = pbGUI.getSaveItem();
		this.closeItem = pbGUI.getCloseItem();
		this.aboutItem = pbGUI.getAboutItem();
		this.nameField = pbGUI.getNameField();
		this.companyField = pbGUI.getCompanyField();
		this.phoneField = pbGUI.getPhoneField();
		this.notesField = pbGUI.getNotesField();
		this.addButton = pbGUI.getAddButton();


	public void actionPerformed(ActionEvent e) {

		if(e.getSource().equals(addButton)) {

			if(nameField.getText().equals("") || companyField.getText().equals("") || phoneField.getText().equals("")) {
				JOptionPane.showMessageDialog(frame, "Name, company and phone fields must be filled!!", "Oops!", JOptionPane.WARNING_MESSAGE);
			} else {

				String name 		= nameField.getText();
				String company 		= companyField.getText();
				String phone 		= phoneField.getText();
				String notes 		= notesField.getText();


		if(e.getSource().equals(closeItem)) {

		if (e.getSource().equals(aboutItem)) {
			JOptionPane.showMessageDialog(frame, "PhoneBook v0.1\nBy Øyvind Hauge");

This post has been edited by oha055: 17 February 2010 - 05:48 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Writing input from JTextField to a file

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3542
  • View blog
  • Posts: 16,146
  • Joined: 20-September 08

Re: Writing input from JTextField to a file

Posted 17 February 2010 - 06:30 AM

Use the Ostermiller class to read and write CSV

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1