Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 86,253 Java Programmers. There are 2,125 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Java Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Saving GUI info

2 Pages V  1 2 >  
Reply to this topicStart new topic

Saving GUI info

iNaStY v3
post 7 May, 2008 - 10:02 PM
Post #1


D.I.C Head

Group Icon
Joined: 3 Feb, 2008
Posts: 63



I'm making a a GUI to emulate what a teller uses.

My algorithm looks like this: start with a welcome screen, prompting to create a new account or search previous. if the new account button is pressed, a new window is opened prompting user to display all information. etc...

i can't figure out how to save this information?

java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
public class NewAccount extends JFrame {

public NewAccount() {
JFrame acctFrame = new JFrame("Personal Information");
JPanel acctPane = new JPanel();
acctPane.setLayout(new GridLayout(14,2));

JLabel first = new JLabel(" First Name:");
JLabel last = new JLabel(" Last Name:");
JLabel middle = new JLabel(" Middle Name:");
JLabel type = new JLabel(" Gender:");
String [] combopkg = {"Male","Female"};
JComboBox gender = new JComboBox(combopkg);
............labels for the address and such.............
JLabel password = new JLabel(" Password");

JTextField fName = new JTextField(15);
// this is what i tried but it still doesn't work
String fieldText = fName.getText();
............text fields for the address and such.............
JPasswordField pf = new JPasswordField(15);
JTextField cphone = new JTextField(10);

JButton create = new JButton("Create");
JButton cancel = new JButton("Cancel");
acctPane.add(first);
acctPane.add(fName);
acctPane.add(last);
acctPane.add(lName);
........the rest of the acctPane.add()......................
acctPane.add(cancel);
acctPane.add(create);

JLogin login = new JLogin();
JLogin1 login1 = new JLogin1();
cancel.addActionListener(login);
create.addActionListener(login1);


acctFrame.getContentPane().add(acctPane);
acctFrame.setSize(300,500);
acctFrame.setResizable(false);
boolean resizable = acctFrame.isResizable();
acctFrame.setVisible(true);
}

class JLogin implements ActionListener {
public void actionPerformed(ActionEvent cancel) {
Object ob = cancel.getSource();
if (ob == cancel) {
Welcome w = new Welcome();
}
}
}

String name;
class JLogin1 implements ActionListener {
public void actionPerformed(ActionEvent create) {
// this is where my code faults.
// it's not taking the values from what the user enters
name = (fieldText.getText());
Object ob = create.getSource();
if (ob == create) {
try {
PrintWriter outFile = new PrintWriter ("info.txt");
outFile.println("Name: " + name);
}
}
}
}
}


I realize that I'm not finished writing the code to save the info, but I haven't been able to figure out how to get the program to read the values the user enters in those fields for about 5 hours now lol so i figured i'd just post this

thanks in advance
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


pbl
post 8 May, 2008 - 05:53 AM
Post #2


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

QUOTE(iNaStY v3 @ 7 May, 2008 - 10:02 PM) *

I'm making a a GUI to emulate what a teller uses.

My algorithm looks like this: start with a welcome screen, prompting to create a new account or search previous. if the new account button is pressed, a new window is opened prompting user to display all information. etc...

i can't figure out how to save this information?


Just a little little misconcept... I think

java

public class NewAccount extends JFrame {

public NewAccount() {

JTextField fName = new JTextField(15);
// this is what i tried but it still doesn't work
// If you think that means whenever fName changes the result will be put in fieldText
// you are wrong
// this takes what is in fName NOW and put it in fieldText
// as fName hasn't been populated at this moment fieldText == ""
// so this statement is useless
String fieldText = fName.getText();
............text fields for the address and such.............
JPasswordField pf = new JPasswordField(15);
}


String name;
class JLogin1 implements ActionListener {
public void actionPerformed(ActionEvent create) {
// this is where my code faults.
// it's not taking the values from what the user enters
// there is still no values in fieldText but now you want to copy what is in fName
/// name = (fieldText.getText());
name = (fName.getText());
Object ob = create.getSource();
if (ob == create) {
try {
PrintWriter outFile = new PrintWriter ("info.txt");
outFile.println("Name: " + name);
}
}
}
}
}

User is offlineProfile CardPM
Go to the top of the page
+Quote Post

iNaStY v3
post 8 May, 2008 - 12:31 PM
Post #3


D.I.C Head

Group Icon
Joined: 3 Feb, 2008
Posts: 63

It's still not working, I'm using Eclilpse and it's telling me that the field cannot be resolved...

java

public class NewAccount extends JFrame {

public NewAccount() {
JTextField fName = new JTextField(15);
/* here it's saying that fieldText is never read */
String fieldText = fName.getText();

JLogin login = new JLogin();
JLogin1 login1 = new JLogin1();
cancel.addActionListener(login);
create.addActionListener(login1);
}
class JLogin1 implements ActionListener {
public void actionPerformed(ActionEvent event) {
String fullName;
String fullAddress;
String phoneNumbers;
File tmpFile;
File backupFile;
File file;
/* and here it's saying that it can't be resolved */
fullName = fieldText.getText();


literally, all I want to do is take what the user enters for his name (first middle and last) address and phone number and store it in a file so I can recall it with a search method and display it as a list.....and I'm soooo lost

This post has been edited by iNaStY v3: 8 May, 2008 - 12:32 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 8 May, 2008 - 12:46 PM
Post #4


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

QUOTE(iNaStY v3 @ 8 May, 2008 - 12:31 PM) *

It's still not working, I'm using Eclilpse and it's telling me that the field cannot be resolved...

java

public class NewAccount extends JFrame {
// fName should be declared here as an instance variable
JTextField fName = new JTextField(15);

// now your constructor
public NewAccount() {
/* here it's saying that fieldText is never read */
String fieldText = fName.getText();
// Eclipse is right... your are putting the content of the JTextField in fieldText
// but you never use fieldText after so Eclipse tells you that you are performing a useless operation

JLogin login = new JLogin();
JLogin1 login1 = new JLogin1();
cancel.addActionListener(login);
create.addActionListener(login1);
}
class JLogin1 implements ActionListener {
public void actionPerformed(ActionEvent event) {
String fullName;
String fullAddress;
String phoneNumbers;
File tmpFile;
File backupFile;
File file;
/* and here it's saying that it can't be resolved */
// sure fieldText is defined in your constructor JLogin1 cannot see it
fullName = fieldText.getText();
// you should do
fullName = fName.getText()


literally, all I want to do is take what the user enters for his name (first middle and last) address and phone number and store it in a file so I can recall it with a search method and display it as a list.....and I'm soooo lost

User is offlineProfile CardPM
Go to the top of the page
+Quote Post

iNaStY v3
post 8 May, 2008 - 01:25 PM
Post #5


D.I.C Head

Group Icon
Joined: 3 Feb, 2008
Posts: 63

Ohhhhh that makes perfect sense. I think I tried that earlier, but when I did I think I changed everything to static (for some strange reason lol) and obviously it didn't work.

making those changes you suggested, now it won't write it to a file at all, and I'm getting a different error message

java

class JLogin1 implements ActionListener {
public void actionPerformed(ActionEvent event) {
String fullName;
String fullAddress;
String phoneNumbers;
File tmpFile;
File backupFile;
File file;
fullName = lName.getText() + ", " + fName.getText() + ", " + mName.getText();
fullAddress = add.getText() + "\n" + cit.getText() + ", " + st.getText() + " " + zipCode.getText();
phoneNumbers = "Home: " + hphone.getText() + "\nWork: " + wphone.getText() + "\nCell: " + cphone.getText();
/* Used a try/catch loop to first write the file as a temp
* then used FilSaver.java constructor to save as .bak (backup)
* This is actually a snipet I found online
* but then here it says that at FileSaver(file) that String cannot be converted to a File
* As it is, it's saying that the local variable file hasn't been initialized
*/
try {
FileSaver saver = new FileSaver(file);
final Writer writer = saver.getWriter();
PrintWriter out = new PrintWriter(writer);
out.close();
saver.finish();
System.out.println("Saved OK");
}
catch (IOException e) {
System.out.println("Save FAILED"); // exception thrown
}

}


I've been searching online, and all I've found is long long long snipets that I can't implement.

I've also tried to use FileWriter, but that also doesn't work with a String.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 8 May, 2008 - 01:33 PM
Post #6


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

Which error message ?

And if you can't figuring it out post your code
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

iNaStY v3
post 8 May, 2008 - 01:50 PM
Post #7


D.I.C Head

Group Icon
Joined: 3 Feb, 2008
Posts: 63

Well right now as it is (the code from above) I'm getting the error message at
java

FileSaver saver = new FileSaver(file);

at (file) --- it's saying that the local variable may not have been initialized.

if I declare
java

File tmpFile;
File backupFile;
File file;


before my constructor like before, that error message goes away, but when I run the program, if I click on create, nothing happens (I know I haven't told it what to do) but no file gets created -- at least I don't think it does, because saving it like this I have no idea where it goes, or what it's name is, or MOST importantly how to call it....I've tried calling it with FileReader, but I can't figure out what it's called
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 8 May, 2008 - 01:55 PM
Post #8


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

at (file) --- it's saying that the local variable may not have been initialized.

if I declare
java

File tmpFile;
File backupFile;
File file; <----- true it is not initialized



So new FileSaver(file);
on null
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

iNaStY v3
post 8 May, 2008 - 02:10 PM
Post #9


D.I.C Head

Group Icon
Joined: 3 Feb, 2008
Posts: 63

QUOTE(pbl @ 8 May, 2008 - 01:55 PM) *

So new FileSaver(file);
on null


I have no idea what that means pbl lol sorry. Let me rephrase:

As my code is, it tells me that (file) hasn't be initialized. So if I declare it before my constructor, that error message goes away, but when I run the program, if I click on create, nothing happens (I know I haven't told it what to do) but no file gets created -- at least I don't think it does, because saving it like this I have no idea where it goes, or what it's name is, or MOST importantly how to call it....I've tried calling it with FileReader, but I can't figure out what it's called
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 8 May, 2008 - 02:41 PM
Post #10


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

If you say

File file;
new FileSaver(file);

file is not initialized.

File file;
....
file = new File("abc.dat");
new FileSaver(file);

then file is initialized.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

iNaStY v3
post 8 May, 2008 - 03:08 PM
Post #11


D.I.C Head

Group Icon
Joined: 3 Feb, 2008
Posts: 63

Yes that part I've gotten, but that created two more problems ( mad.gif )

first of all, it's creating the file, but it's not writing anything to it, so I added to that (my code is below with comments) but this starts my second, more important problem.

doing it this way writes a new file everytime, and they'll all be called the same thing (and I think I need everything saved in one place)

Bottom line is I need to be able to search my customer's by their name, and have my program output their name, address, and phone numbers in a String (and then later I'll add accounts) and with the code written this way I have no idea how to do that.... I just need a starting point

Sorry I'm not explaining this very well but I hope I cleared it up. Thanks for the help

java

/* for the file name, is it possible to save it as the persons name?? */
file = new File("Name.dat");
FileSaver saver = new FileSaver(file);
final Writer writer = saver.getWriter();
PrintWriter out = new PrintWriter(writer);
/* this is what I added to write the file, but I get the error message
* The method myWriteOutputFile(PrintWriter) is undefined for the type
* NewAccount.JLogin1 */
myWriteOutputFile(out);
out.close();
saver.finish();
System.out.println("Saved OK");
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

iNaStY v3
post 9 May, 2008 - 09:53 AM
Post #12


D.I.C Head

Group Icon
Joined: 3 Feb, 2008
Posts: 63

This writes the files, but it doesn't put anything into it. Why?

java

class JLogin1 implements ActionListener {
public void actionPerformed(ActionEvent event) {

fullName = lName.getText() + ", " + fName.getText() + ", " + mName.getText();
fullAddress = add.getText() + "\n" + cit.getText() + ", " + st.getText() + " " + zipCode.getText();
phoneNumbers = "Home: " + hphone.getText() + "\nWork: " + wphone.getText() + "\nCell: " + cphone.getText();
String toString = fullName + "\n" + fullAddress + "\n" + phoneNumbers;
// this is where I'm trying to save the file

inputFile = new File(toString);
try {
File inputFile = (FileSaver.getFile());
inputFile = new File("tmpFile");
FileSaver saver = new FileSaver(inputFile);
final Writer writer = saver.getWriter();
Writer out = new FileWriter(tmpFile);
out.close();
saver.finish();


in the folder, it saves tmpFile.bak and tmpFile.tmp, but it doesn't write anything into it.

Here's the methods from my class FileSave
java

public FileSaver(File input) throws IOException {

// Step 1: Create temp file in right place
this.inputFile = input;
tmpFile = new File(inputFile.getAbsolutePath() + ".tmp");
tmpFile.createNewFile();
tmpFile.deleteOnExit();
backupFile = new File(inputFile.getAbsolutePath() + ".bak");
state = State.AVAILABLE;
}

public static File getFile() {
return inputFile;
}

public Writer getWriter() throws IOException {

if (state != State.AVAILABLE) {
throw new IllegalStateException("FileSaver not opened");
}
Writer out = new FileWriter(tmpFile);
state = State.INUSE;
return out;
}

public void finish() throws IOException {

if (state != State.INUSE) {
throw new IllegalStateException("FileSaver not in use");
}

// Delete the previous backup file if it exists;
backupFile.delete();

// Rename the user's previous file to itsName.bak,
// UNLESS this is a new file ;
if (inputFile.exists() && !inputFile.renameTo(backupFile)) {
throw new IOException("Could not rename file to backup file");
}

// Rename the temporary file to the save file.
if (!tmpFile.renameTo(inputFile)) {
throw new IOException("Could not rename temp file to save file");
}
state = State.AVAILABLE;
}
}
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 09:24AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month