I am a fairly new programmer still in school, and trying to have my program write the selected information to a designated file. I keep getting an error message on my file OutputStream.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.*;
class ExampleJComboBoxSample extends JFrame
implements ActionListener,
ItemListener
{
private static final int FRAME_WIDTH =1000;
private static final int FRAME_HEIGHT =1000;
private JComboBox comboBox1, comboBox2;
private JTextField parentinput, emailinput;
public static void main (String[] args)
{
ExampleJComboBoxSample frame1 = new ExampleJComboBoxSample();
frame1.setVisible(true);
}
public ExampleJComboBoxSample()
{
Container contentPane1, contentPane2, contentPane3;
JPanel comboPanel, okPanel, headerPanel;
JLabel image1, image2, title1, parent, teacher, child, emaildr;
JButton saveButton, cancelButton;
String[] teacherBoxItem = {"Choose Teacher...", "Mrs. Thomas", "Mr. Peter", "Mr. Key", "Mrs. Lucky" };
String[] studentBoxItem = {"Choose Student...", "Bobby Jones", "Hailey Johnson", "Sarah Lee", "Carolyn Birdie" };
// set the frame properties
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setTitle ("ExampleJComboBoxSample");
contentPane1 = getContentPane();
contentPane1.setBackground(Color.BLUE);
contentPane1.setLayout(new GridLayout(4,2));
contentPane2 = getContentPane();
contentPane2.setBackground(Color.BLUE);
contentPane2.setLayout(new BorderLayout());
contentPane3 = getContentPane();
contentPane3.setBackground(Color.BLUE);
contentPane3.setLayout(new BorderLayout());
// create and place a header for the frame
headerPanel = new JPanel(new FlowLayout());
image1 = new JLabel(new ImageIcon("/Users/bndandridge/Zion Mascot Image/mascot 2.gif"));
image2 = new JLabel(new ImageIcon("/Users/bndandridge/Zion Mascot Image/mascot 2.gif"));
title1 = new JLabel();
image1.setSize(50,50);
title1.setText("ZION PARENT CONTACT DIRECTORY");
image2.setSize(50,50);
headerPanel.setBackground(Color.BLUE);
headerPanel.add(image1, BorderLayout.WEST);
headerPanel.add(title1, BorderLayout.CENTER);
headerPanel.add(image2, BorderLayout.EAST);
// create and place combo box text fields and labels
comboPanel = new JPanel(new GridLayout(4,2));
comboPanel.setBorder(BorderFactory.createTitledBorder("Parent Information"));
comboPanel.setBackground(Color.BLUE);
// Labels for the center panel to line up with the necessary fields of choice.
parent = new JLabel();
parent.setText("PARENTS NAME:");
teacher = new JLabel();
teacher.setText("TEACHERS NAME:");
child = new JLabel();
child.setText("STUDENTS NAME:");
emaildr = new JLabel();
emaildr.setText("EMAIL ADDRESS:");
//Grouping of the text fields, panels, and boxes.
comboPanel.add(parent);
parentinput =new JTextField(0);
parentinput.addActionListener(this);
comboPanel.add(parentinput);
comboPanel.add(teacher);
comboBox1 = new JComboBox (teacherBoxItem);
comboBox1.addItemListener(this);
comboPanel.add(comboBox1);
comboPanel.add(child);
comboBox2 = new JComboBox (studentBoxItem);
comboBox2.addItemListener(this);
comboPanel.add(comboBox2);
comboPanel.add(emaildr);
emailinput =new JTextField(0);
emailinput.addActionListener(this);
comboPanel.add(emailinput);
// create and place the buttons for the frame
okPanel =new JPanel(new FlowLayout());
saveButton = new JButton ("SAVE");
cancelButton = new JButton ("CANCEL");
saveButton.addActionListener(this);
okPanel.add(saveButton);
cancelButton.addActionListener(this);
okPanel.add(cancelButton);
okPanel.setBackground(Color.BLUE);
contentPane1.add(comboPanel, BorderLayout.CENTER);
contentPane2.add(okPanel, BorderLayout.SOUTH);
contentPane3.add(headerPanel, BorderLayout.NORTH);
// register 'Exit upon closing' as a default close operation
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event)
{
String favorite, favorite2;
int loc, loc2;
favorite = (String) comboBox1.getSelectedItem();
loc = comboBox1.getSelectedIndex();
favorite2 = (String) comboBox2.getSelectedItem();
loc2 = comboBox2.getSelectedIndex();
JOptionPane.showMessageDialog(this, "Currently selected item'" +
favorite + " ' is at index position " + loc);
/*Have input from the parent to a prelabled and dated text file.
Having issues connecting the button action to save and write the new file to the drive.
*/
//Pull internal clock to date and time stamp the upcoming file output save.
Date today;
SimpleDateFormat simpleDF1;
today = new Date();
simpleDF1 = new SimpleDateFormat("EEEE MMMM dd, yyyy HH mm");
File outFile = new File("Parent Contact Directory.txt " + simpleDF1.format(today));
FileOutputStream outFileStream = new FileOutputStream(outFile);
PrintWriter outStream = new PrintWriter(outFileStream);
outStream.println(parentinput);
outStream.println(comboBox1);
outStream.println(comboBox2);
outStream.println(emailinput);
outStream.close();
}
public void itemStateChanged(ItemEvent event)
{
String state;
if (event.getStateChange() == ItemEvent.SELECTED)
{
state = "is selected";
}
else
{
state = "is deselected ";
}
JOptionPane.showMessageDialog(this, "JComboBox Item '" +
event.getItem() + "' " + state);
}
}