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);
}

}

Recommended Answers

All 11 Replies

cannot find symbol
symbol : variable getSelectedItem

It's a method call, needs a ()

That was the first attempt I made and received an error message, I used the (combobox1.getItemSelected()"Choose Teacher...") and it did not work. I can send a code excerpt once I get home showing the other error message.

ehmmm.... no.
just

combobox1.getSelectedItem();

getItemSelected() is not (as far as I know, anyway) an existing method
and you can not let a method be followed immediately by a new String like you do in your last post.

Sorry for the delayed post had to work late last night. What I have below is the error message with the attempted prior exception code. I had a quick touch in class on exceptions and now running into difficulty understanding what I am missing now.

fileJComboBoxSample.java:125: incompatible types
found   : java.lang.Object
required: boolean
 if (comboBox1.getSelectedItem()){
                              ^
1 error
public void actionPerformed(ActionEvent event)
{
try
{

 if (comboBox1.getSelectedItem()){
		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");
	}
 


	}
if (comboBox1.getSelectedItem()){
throw new IOException ("Error");
}

what exactly are you trying to do here?
between the brackets of your if statement, you need to put an expression which returns a boolean, or a boolean.
at this point, you have an expression which returns an Object

what you could do is:

String test = (String)combobox.getSelectedItem();
if ( test.equals("trueIfThisValue")){
// code when true
}

My intention is to have the exception message appear in a joption message box if the combobox still states "Choose Teacher…" when the save button is pressed. But this is my first real dealings with exceptions so a little confused on my try catch block.

BDan...
an exception is ment to catch an EXCEPTIONAL interrupt of the normal flow. what you are describing is not exceptional, but just being prepared for a user that can forget to choose something.

I would suggest you add something like:

if(((String)combobox1.getSelectedItem()).equals("Choose Teacher..."))
// show error message - DO NOT THROW EXCEPTION
else
// handle normal flow here

you could have gotten that out of my previous post, though. just remember, you must avoid (as much as you can) to throw exceptions. don't have exceptions thrown if you can deal with it in a simple logical way.

never forget that an exception, if not rightly handled, can crash your entire application.

I don't want to start an argument here, but I must point out that not everybody will agree with stultuske's post on exceptions for user errors. It's a genuine controversy in the Java world. I'm not going to argue for one side or the other, but I will point out that the designers of the Java API do sometimes use exceptions to signal bad user input - eg input mismatch exceptions from Scanner's nextInt().

true, but in the code as given the developer want when this happens a loop to run again.
I doubt if an Exception is the best way to do this.

Using exceptions to catch and handle bad input is ONLY good if either that input is to cause an application crash OR if you have a dedicated framework in place to handle such exceptions at a higher (central) level.
The latter is rarely if ever the case in a small homework assignment.

Thanks for everyone's help, I took all of your advice into consideration and created an input exception. This will open a new joption message box that instructs the user the file cannot be saved unless both drop down choices have been selected.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.