I've been banging my head against the wall last couple of hours trying to get my program to work, but to no success.
I'm working on a simple JFrame based program that should open the window, let me input the variables and then if I press Save, save them to selected .csv file.
The problem however arises when I try to save 2 or more sets of variables, the first one always gets overwritten and only new one is there.
For example instead of:
Mark, 100, 2, John, 50, 1
In the file I only find
John 50, 1
I'm guessing it has to do with new bufferedwriter being created every time I click 'Save' button but I have no idea how to get around doing that, I tried multiple positions of placing it but it never works because I get error if it's outside the Action method.
Here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/**
* Demonstrates etc
*/
public class Homework1 extends JFrame implements ActionListener {
JTextField jtfName1;
JTextField jtfName2;
JTextField jtfName3;
JTextField jtfName4;
static File file = new File("121Lab1.csv");
public Homework1() {
// Set BorderLayout with horizontal gap 5 and vertical gap 10
setLayout(new BorderLayout(10, 10));
// Create a JPanel with FlowLayout for the South of the JFrame's BorderLayout
JPanel jpSouth = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 3));
// Create some buttons to place in the south area
JButton jbCalc = new JButton("Calculate");
JButton jbSave = new JButton("Save");
JButton jbClear = new JButton("Clear");
JButton jbExit = new JButton("Exit");
jpSouth.add(jbCalc);
jpSouth.add(jbSave);
jpSouth.add(jbClear);
jpSouth.add(jbExit);
jbCalc.addActionListener(this);
jbSave.addActionListener(this);
jbClear.addActionListener(this);
jbExit.addActionListener(this);
// Place the south panel in the JFrame's south area
add(jpSouth, BorderLayout.SOUTH);
// Add textfields to the rest of the frame
JPanel jpCenter = new JPanel(new GridLayout(0,2));
jpCenter.add(new JLabel("Item name: ", SwingConstants.RIGHT));
jtfName1 = new JTextField("", 10);
jpCenter.add(jtfName1);
jtfName1.addActionListener(this);
jpCenter.add(new JLabel("Number of: ", SwingConstants.RIGHT));
jtfName2 = new JTextField("", 10);
jpCenter.add(jtfName2);
jtfName2.addActionListener(this);
jpCenter.add(new JLabel("Cost: ", SwingConstants.RIGHT));
jtfName3 = new JTextField("", 10);
jpCenter.add(jtfName3);
jtfName3.addActionListener(this);
jpCenter.add(new JLabel("Amount owed: ", SwingConstants.RIGHT));
jtfName4 = new JTextField("", 10);
jpCenter.add(jtfName4);
jtfName4.addActionListener(this);
add(jpCenter, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae) {
String actionString = ae.getActionCommand(); // gets the string on the component
Object actionObj = ae.getSource();
if (actionString.equalsIgnoreCase("Calculate")) { // ae.getSource() == jbCancel
System.out.println("You clicked Calculate");
try {
double value = Double.parseDouble(jtfName2.getText())*Double.parseDouble(jtfName3.getText());
jtfName4.setText(String.format("%.2f", value));
}
catch (NumberFormatException nfe) {
jtfName4.setText("Not a number");
}
}
else if (actionString.equalsIgnoreCase("Save")) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(jtfName1.getText()+",");
writer.write(jtfName2.getText()+",");
writer.write(jtfName3.getText()+"\r\n");
writer.flush();
writer.close();
}
catch(IOException e) {
System.out.println("IO Error");
}
}
else if (actionString.equalsIgnoreCase("Clear")) {
System.out.println("You clicked Clear");
jtfName1.setText("");
jtfName2.setText("");
jtfName3.setText("");
jtfName4.setText("");
}
else if (actionString.equalsIgnoreCase("Exit")) {
setVisible(false);
dispose();
}
else {
System.out.println("Unknown command: " + actionString);
System.out.println("Unknown source: " + actionObj);
}
}
public static void main(String[] args) throws IOException {
Homework1 jfMain = new Homework1();
jfMain.setTitle("Item Order Calculator");
jfMain.setSize(450, 200);
jfMain.setLocationRelativeTo(null); // Center JFrame
jfMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfMain.setVisible(true);
} // end main
} // end class JPanelTester