Menu
i got the following bit of code:
[Code]
String baseFileName = “Salary”;
int i = 0;
String writer;
String tempFileName = baseFileName + (i++) + “.txt”;
writer = tempFileName;
PrintWriter out = new PrintWriter (writer);
[code]
i want everytime i press save it saves it to a new .txt file. but right now it is just saving to one txt file and not doin like an array. WATS WRONG?
<i>
</i>String baseFileName = "Salary";
int i = 0;
for (i=0; i < 10; i++);
String tempFileName = baseFileName + (i++) + ".txt";
FileWriter writer = new FileWriter("tempFileName.txt");
PrintWriter out = new PrintWriter (writer);
[code=php]
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
*
* Author:
* Date: Mar 6, 2006
* Time: 4:32:45 PM
*/
public class SaveFile extends JFrame implements ActionListener {
JTextField jtfName, jtfSalary;
JButton jbSave;
/**
* Constructor to start the processing
*/
public SaveFile() {
initApp();
}
/**
* Initilaizes the GUI
*/
public void initApp() {
this.setTitle("Save File GUI");
Container c = this.getContentPane();
c.setLayout(new FlowLayout());
JLabel lblName = new JLabel("Name : ");
jtfName = new JTextField("", 20);
jtfName.setName("Name");
JLabel lblSalary = new JLabel("Salary :");
jtfSalary = new JTextField("", 20);
jtfSalary.setName("Salary");
jbSave = new JButton("Save As");
jbSave.addActionListener(this);
c.add(lblName);
c.add(jtfName);
c.add(lblSalary);
c.add(jtfSalary);
c.add(jbSave);
//close frame when user clicks on X at the top right corner
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
this.setSize(300, 150);
this.setVisible(true);
}
/**
* @param e
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {//user has clicked on save as button
//now get text field values and then show the save as dialogue
String data = "";
data += jtfName.getName() + "=" + jtfName.getText();
data += ", "; //comma separates to values
data += jtfSalary.getName() + "=" + jtfSalary.getText();
//show save as dilogue
File newFile = saveFile(data);
//now u can use that newly saved file for further processing if needed.
//uncomment lines below to clear the text fields after they are saved
//clearTextFields();
}
}
/**
* This method will allow you to save data from text fields into a file by showing you the
* Save As file dialogue.
* Once file is saved it returns a handle to that newly saved file.
*
* @param data
* @return File
*/
public File saveFile(String data) {
File file; // new file object that we want to save this data as
file = null;
JFileChooser fd = new JFileChooser();
fd.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fd.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
file = fd.getSelectedFile();
if (file == null) {
JOptionPane.showMessageDialog(null, "Invalid file name", "Invalid file name", JOptionPane.ERROR_MESSAGE);
} else {
try {
//FileWriter writer = new FileWriter(file);
BufferedWriter br = new BufferedWriter(new FileWriter(file));
br.write(data);
br.flush();
br.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return file;
}
/**
* Clears up text fields.
* If a field is not intialized this method will throw a null pointer exception.
* Thats developers responsibility to set more checks for such errors.
*/
public void clearTextFields() {
jtfName.setText("");
jtfSalary.setText("");
}
/**
* @param args
*/
public static void main(String[] args) {
new SaveFile();
}
}
[/code]
<i>
</i>public class try1 {
public static void einfo() {
<i> </i>final int buttonWidth = 65;
<i> </i>final int buttonHeight = 25;
<i> </i> JButton saveButton = new JButton ("Save");
<i> </i> saveButton.setPreferredSize (new Dimension (buttonWidth, buttonHeight));
<i> </i> JLabel blankLabel = new JLabel (" "); // blank lines
<i> </i> // Date: Year Drop-down menu
<i> </i> final JComboBox yearChoice = new JComboBox();
<i> </i> int i;
<i> </i> i = 1940;
<i> </i> for (i = 1940; i < 2007; i++) {
<i> </i> yearChoice.addItem(i);
<i> </i> yearChoice.setSelectedItem(i);
<i> </i> }
<i> </i> //Contact Information
<i> </i> JLabel contactLabel = new JLabel (" Contact Information:");
<i> </i> JLabel telephoneLabel = new JLabel (" Telephone:");
<i> </i> final JTextField telephoneTextField = new JTextField(10);
<i> </i> JLabel cellphoneLabel = new JLabel (" Cell Phone:");
<i> </i> final JTextField cellphoneTextField = new JTextField(15);
<i> </i> JLabel emailLabel = new JLabel (" Email Address:");
<i> </i> final JTextField emailTextField = new JTextField(15);
<i> </i> JLabel haddressLabel = new JLabel (" House Address:");
<i> </i> final JTextField haddressTextField = new JTextField(15);
<i> </i>//Font
<i> </i>Font largeArialFont = new Font ("Arial", Font.BOLD, 16);
<i> </i> contactLabel.setFont (largeArialFont);
<i> </i> final JFrame info = new JFrame ("Employee Information");
<i> </i> info.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
<i> </i> info.setSize (150, 500);
<i> </i> info.setLayout (new GridLayout (8,2));
<i> </i> info.setLocationRelativeTo (null);
<i> </i> info.add (yearChoice);
<i> </i> info.add (contactLabel);
<i> </i> info.add (telephoneLabel);
<i> </i> info.add (telephoneTextField);
<i> </i> info.add (cellphoneLabel);
<i> </i> info.add (cellphoneTextField);
<i> </i> info.add (emailLabel);
<i> </i> info.add (emailTextField);
<i> </i> info.add (haddressLabel);
<i> </i> info.add (haddressTextField);
<i> </i> info.pack ();
<i> </i> info.show ();
}
0.1.9 — BETA 6.17