title: Save data from JTextField in columns in .csv File
body: <p>I have the coded the following program to get user input in four JTextFields and than saves it in a .csv file. But I am getting it in different rows. I want the output in the .csv file to be from column to column</p>
  
  <p>My code is as follows:</p>
  
  <pre><code>public class panel extends FirstClick {
  
                 final JTextField items;
                 final JTextField number;
                 final JTextField cost;
                 final JTextField amount;
  
                public panel() {    
                    JFrame myFrame = new JFrame();    
                      myFrame.setLayout( new FlowLayout() );
  
                      myFrame.setTitle("GUI Demo - ISTE-121");
                      myFrame.pack();
                      myFrame.setLocation(600,300);
                      myFrame.setSize(400,200);
                      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      myFrame.setVisible(true);
  
                JPanel order = new JPanel();
                order.setLayout(new GridLayout(5,5,2,2));
  
                myFrame.add(order, BorderLayout.CENTER);
  
                order.add(new JLabel("Item no:", SwingConstants.RIGHT));
                order.add(items = new JTextField(3));
  
                order.add(new JLabel("Number of:", SwingConstants.RIGHT));
                order.add(number = new JTextField(3));
  
                order.add(new JLabel("Cost", SwingConstants.RIGHT));
                order.add(cost = new JTextField(3));
  
                order.add(new JLabel("Amount owed:", SwingConstants.RIGHT));
                order.add(amount = new JTextField(10));
                amount.setEditable(false);                  
  
                JPanel buttons = new JPanel();
                buttons.setLayout(new GridLayout(1,1,2,2));
  
            myFrame.add(buttons ,BorderLayout.SOUTH);
  
                JButton calculate;
                buttons.add(calculate=new JButton("Calculate"));
  
                calculate.addActionListener(new ActionListener() 
                {
                  @Override
                  public void actionPerformed(ActionEvent e) 
                  {
                      // TODO Auto-generated method stub
                      double cost1 = new Double(cost.getText().trim()).doubleValue();
                      double number1 = new Double(number.getText().trim()).doubleValue();
  
                      double result =  cost1*number1;
                      amount.setText(String.valueOf(result));                     
                  }                     
                });
  
                JButton save;
                buttons.add(save = new JButton("Save"));
  
                save.addActionListener(new ActionListener()
              {
                  @Override
                  public void actionPerformed(ActionEvent e) { 
  
                      // The name of the file to open.
                      String fileName = "temp1.csv";    
                      try {
                          // Assume default encoding.
                          FileWriter fileWriter =
                              new FileWriter(fileName, true);
  
                          // Always wrap FileWriter in BufferedWriter.
                          BufferedWriter bufferedWriter =
                              new BufferedWriter(fileWriter);
  
                          // Note that write() does not automatically
                          // append a newline character.
                          bufferedWriter.write(items.getText());
                          bufferedWriter.newLine();
                          bufferedWriter.write(number.getText());
                          bufferedWriter.newLine();
                          bufferedWriter.write(cost.getText());
                          bufferedWriter.newLine();
                          bufferedWriter.write(amount.getText());
                          bufferedWriter.newLine();
                          // Always close files.
                          bufferedWriter.close();
                      }
                      catch(IOException ex) {
                          System.out.println(
                              "Error writing to file '"
                              + fileName + "'");
                          // Or we could just do this:
                          // ex.printStackTrace();
                      }
  
                  }}  
                  );
  
                JButton clear;
                buttons.add(clear=new JButton("Clear"));
  
                clear.addActionListener(new ActionListener() {
  
                  @Override
                  public void actionPerformed(ActionEvent e) {
                      // TODO Auto-generated method stub
                       items.setText(null);
                       number.setText(null);
                       cost.setText(null);
                       amount.setText(null);
                  }});
  
                JButton exit;
                buttons.add(exit= new JButton("Exit"));
  
                 exit.addActionListener(new ActionListener()
                 {
  
              @Override
              public void actionPerformed(ActionEvent e) 
              {
                  // TODO Auto-generated method stub
                System.exit(0);
  
              }
                 });
  
          }
  }
  
  
  
  public class FirstClick {
  
      public static void main(String args[]) {
          panel second = new panel();
  
  
      }  // end main
  }
  </code></pre>