Okay I am fine tuning my program and would like to have a new JOption Message appear when a particular input is caught and have the exception message thrown. I am trying to get my input to come from my combo box but getting the cannot find symbol message". Am I missing a variable to be identified? I have them identified above and without this adjustment the program compiles fine.
fileJComboBoxSample.java:124: cannot find symbol
symbol : variable getSelectedItem
location: class javax.swing.JComboBox
if (comboBox1.getSelectedItem = "Choose Teacher..."){
^
1 error
inserted actual code with error pointing to line 124 of the program
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.*;
class fileJComboBoxSample 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)
{
fileJComboBoxSample frame1 = new fileJComboBoxSample();
frame1.setVisible(true);
}
public fileJComboBoxSample()
{
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);
saveButton.setActionCommand("save");
okPanel.add(saveButton);
cancelButton.addActionListener(this);
cancelButton.setActionCommand("cancel");
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)
{
try
{
if (comboBox1.getSelectedItem = "Choose Teacher..."){
throw new IOException ("Error");
}
else if ("save".equals(event.getActionCommand()))
{
/*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.getText());
outStream.println(comboBox1.getSelectedItem());
outStream.println(comboBox2.getSelectedItem());
outStream.println(emailinput.getText());
outStream.close();
JOptionPane.showMessageDialog(this, "Your Contact Information Has Been Saved");
System.exit(0);
}
else {
System.exit(0);
}
}
catch (IOException e){
JOptionPane.showMessageDialog(this, "Invalid input, Retry input");
}
}
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);
}
}