I have this program all working, except in how the text looks in the file. The text is supposed to be all on different lines, but its all on the same.
Here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/*
* class LogOrder
* Allows user to pick from five pieces of clothing to order
* user enters name and picks clothing and presses order.
* Stores users order to "order.txt"
* @author Kimberlie Davis
* @version 11/20/08
*/
public class LogOrder
{
/*
* main method
* declares and sets frame as visible
* @param args not used
*/
public static void main(String args[])
{
EventQueue.invokeLater(
new Runnable()
{
public void run()
{
LogOrderFrame mainFrame = new LogOrderFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
}
});
}
}
/*
* LogOrderFrame method
* sets up frame
* Declares and adds panel
*/
class LogOrderFrame extends JFrame
{
public LogOrderFrame()
{
this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
LogOrderPanel panel = new LogOrderPanel();
this.add(panel);
this.setTitle("Log order");
}
private static int DEFAULT_WIDTH = 500;
private static int DEFAULT_HEIGHT = 500;
}
/*
* LogOrderPanel method
* Layouts and panels
* Sets up panel with components
* adds actionListener to button
*/
class LogOrderPanel extends JPanel
{
public LogOrderPanel()
{
//set up layouts
this.setLayout(new BorderLayout());
//north
JPanel north = new JPanel(new BorderLayout());
this.add(north, BorderLayout.NORTH);
//north center
JPanel northCenter = new JPanel();
north.add(northCenter, BorderLayout.CENTER);
//center
JPanel center = new JPanel();
this.add(center, BorderLayout.CENTER);
Box boxLayout = Box.createVerticalBox();
center.add(boxLayout);
//set up components
JLabel companyName = new JLabel("Clothing Inc.");
JLabel askName = new JLabel("Please enter your name:");
enterName = new JTextField(5);
//Array of 5 checkboxes
for(int i = 0; i < 5; i++)
{
check[i] = new JCheckBox();
}
check[0].setText("Jacket");
check[1].setText("Shirt");
check[2].setText("Pants");
check[3].setText("Socks");
check[4].setText("Shoes");
JButton order = new JButton("Order");
//add components
northCenter.add(askName);
northCenter.add(enterName);
boxLayout.add(check[0]);
boxLayout.add(check[1]);
boxLayout.add(check[2]);
boxLayout.add(check[3]);
boxLayout.add(check[4]);
north.add(companyName, BorderLayout.NORTH);
this.add(order, BorderLayout.SOUTH);
ClothingChooser chooseAction = new ClothingChooser();
order.addActionListener(chooseAction);
}
/*
* ClothingChooser class
* implements ActionListener
* sets up actions for button
*/
private class ClothingChooser implements ActionListener
{
/*
* actionPerformed method
* checks what checkboxes have been selected
* stores users order to file "order.txt"
* @param ActionEvent
*/
public void actionPerformed(ActionEvent e)
{
try{
try
{
String output;
if(writer == null)
writer = new BufferedWriter(new FileWriter("order.txt"));
toStream = new FileWriter(writeTo, true);
toStream.write("Order:" + '\n');
toStream.write("For: " + enterName.getText() + " " + '\n');
if(check[0].isSelected())
{
toStream.write("Jacket \n");
//toStream.write("\n");
}
if(check[1].isSelected())
toStream.write('\t' + "Shirt" + '\n');
if(check[2].isSelected())
toStream.write('\t' + "Pants" + '\n');
if(check[3].isSelected())
toStream.write('\t' + "Socks" + '\n');
if(check[4].isSelected())
toStream.write('\t' + "Shoes" + '\n');
toStream.write('\n');
writer.newLine();
JOptionPane.showMessageDialog(null, "Conratulations " + enterName.getText() + " your order was successful!");
}
finally
{
toStream.close();
}
}
catch(IOException exception)
{
JOptionPane.showMessageDialog(null, "Error: Order not receive\n Please call Clothing Inc. to order");
}
}
}
//datafields
BufferedWriter writer;
JCheckBox[] check = new JCheckBox[5];
File writeTo = new File("order.txt");;
JTextField enterName;
FileWriter toStream;
}