School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,138 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,870 people online right now. Registration is fast and FREE... Join Now!



Creating JComboBoxes

Creating JComboBoxes Rate Topic: -----

#1 moses316  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 26
  • Joined: 13-September 07


Dream Kudos: 0

Posted 19 September 2007 - 05:30 PM

Can someone please tell me what I'm doing wrong. When I compile the program it keeps telling me that there is a missing return statement.

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.*;

public class personalInfo extends JFrame implements ActionListener
{
		//initialize array
		String zipCode[] = {"16503", "16502", "16504", "16505", "16501"};
		String states[] = {"AL", "AK","AZ","AR","CA","CO","CT","DE","FL","GA","GU","HI","ID","IL","IN","IA","KS","KY","LA","MA","MD","MS","MI","MN","MI","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","PR","RI","SC","SD","TN","TX","UT","VT","VI","VA","WA","WV","WI","WY"};

		//construct a panel for each row
		JPanel firstRow = new JPanel();
		JPanel secondRow = new JPanel();
		JPanel thirdRow = new JPanel();
		JPanel fourthRow = new JPanel();
		JPanel fifthRow = new JPanel();

		//construct components

		JComboBox zipCombo = new JComboBox(zipCode);
		JComboBox stateCombo = new JComboBox(states);

		//construct a panel for the fields and buttons
		JPanel fieldPanel = new JPanel();

		//construct labels and text boxes
		JLabel firstNameLabel = new JLabel("First Name:				 ");
			JTextField firstName = new JTextField(10);
		JLabel lastNameLabel = new JLabel("Last Name:		   ");
			JTextField lastName= new JTextField(15);
		JLabel cityLabel = new JLabel("City:							  ");
			JTextField city = new JTextField(10);
		JLabel stateLabel = new JLabel("State:   ");
			JTextField state = new JTextField(2);
		JLabel zipLabel = new JLabel("Zip:");
			JTextField zip = new JTextField(9);

	//create the content pane
	public Container createContentPane()
	{
		//populate the JComboBox
		stateCombo.addActionListener(this);
		zipCombo.addActionListener(this);
		zipCombo.setEditable(true);

	}



	public static void main(String args[])
		{
			personalInfo f= new personalInfo();
			f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			f.setSize(350,200);
			f.setTitle("Personal Information");
			f.setResizable(false);
			f.setLocation(200,200);
			f.setVisible(true);
		}

	public personalInfo()
	{
		Container c = getContentPane();
				c.setLayout((new BorderLayout()));
				fieldPanel.setLayout(new GridLayout(8,1));
				FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
					firstRow.setLayout(rowSetup);
					secondRow.setLayout(rowSetup);
					thirdRow.setLayout(rowSetup);
					fourthRow.setLayout(rowSetup);

			//	buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

				//add fields to rows
				firstRow.add(firstNameLabel);
				firstRow.add(lastNameLabel);

				secondRow.add(firstName);
				secondRow.add(lastName);

				thirdRow.add(cityLabel);
				thirdRow.add(stateLabel);
				thirdRow.add(zipLabel);

				fourthRow.add(city);
				stateCombo.add(state);
				zipCombo.add(zip);

				//add rows to panel
				fieldPanel.add(firstRow);
				fieldPanel.add(secondRow);
				fieldPanel.add(thirdRow);
				fieldPanel.add(fourthRow);

				//add button to panel
				//buttonPanel.add(submitButton);

				//add panels to frame
				c.add(fieldPanel, BorderLayout.CENTER);
				//c.add(buttonPanel, BorderLayout.SOUTH);

				//add functionality to buttons
		//submitButton.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e)
	{
		//user clicks the combo box
		if (e.getSource() == stateCombo);

		if (e.getSource() == zipCombo);
	}
}

This post has been edited by William_Wilson: 19 September 2007 - 05:38 PM

Was This Post Helpful? 0
  • +
  • -


#2 William_Wilson  Icon User is offline

  • lost in compilation
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 4,692
  • Joined: 23-December 05


Dream Kudos: 3275

Expert In: Java, C, Javascript

Posted 19 September 2007 - 05:39 PM

code tags next time please.

public Container createContentPane()
here your return type is Container, but you do not return anything.
Make it void or find a Container object to return.
Was This Post Helpful? 0
  • +
  • -

#3 moses316  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 26
  • Joined: 13-September 07


Dream Kudos: 0

Posted 19 September 2007 - 05:51 PM

I tried doing a return c; but that didn't work. Can you suggest a Container object to return to make it work? Also can you please tell me if I have to have a clean compile before I'll get a .class file?

View PostWilliam_Wilson, on 19 Sep, 2007 - 08:39 PM, said:

code tags next time please.

public Container createContentPane()
here your return type is Container, but you do not return anything.
Make it void or find a Container object to return.

Was This Post Helpful? 0
  • +
  • -

#4 moses316  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 26
  • Joined: 13-September 07


Dream Kudos: 0

Posted 20 September 2007 - 05:20 AM

Still looking for some help on this one.
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 7,676
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Posted 20 September 2007 - 09:27 AM

As William stated, that function is returning a Container type. However, in the function you don't even use a container, you are attaching events and setting a property. So the return type of Container is not even needed. What I did for you is change the return type from Container to void because really, you are not returning anything in that function so no need to return a type.

I also saw a small problem with adding controls to your form. You don't seem to be adding your drop down combo or your zip code textfield. It looks like you attempt to add them, but forgot to use fourthrow to do the actual attaching. So I made that change for you and now your state and zip text fields show up. Everything else appears to be working for you.

This should be enough to get you unstuck. The changes are shown below....

Change 1 - createContentPane() change Container to return type 'void'

//create the content pane
public void createContentPane()
{
	  //populate the JComboBox
	  stateCombo.addActionListener(this);
	  zipCombo.addActionListener(this);
	  zipCombo.setEditable(true);
}



Change 2 - Attach your combo and zip code text fields to the form using fourthrow in layout manager - personalInfo()

// Attach city, stateCombo, and zip fields to form using Grid
fourthRow.add(city);
fourthRow.add(stateCombo);
fourthRow.add(zip);



Change 3 - Your flowlayout paraemters were cutting off a bit of the controls, readjusted to look better - personalInfo()

// Change vgap parameter to zero in flowlayout manager
FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,0);




This should get you up and running. Enjoy! :)
Was This Post Helpful? 0
  • +
  • -

#6 moses316  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 26
  • Joined: 13-September 07


Dream Kudos: 0

Posted 21 September 2007 - 09:07 AM

Thanks, that worked great. Now my last question is how come my submit button isn't working. Any help would be greatly appreciated.





View PostMartyr2, on 20 Sep, 2007 - 12:27 PM, said:

As William stated, that function is returning a Container type. However, in the function you don't even use a container, you are attaching events and setting a property. So the return type of Container is not even needed. What I did for you is change the return type from Container to void because really, you are not returning anything in that function so no need to return a type.

I also saw a small problem with adding controls to your form. You don't seem to be adding your drop down combo or your zip code textfield. It looks like you attempt to add them, but forgot to use fourthrow to do the actual attaching. So I made that change for you and now your state and zip text fields show up. Everything else appears to be working for you.

This should be enough to get you unstuck. The changes are shown below....

Change 1 - createContentPane() change Container to return type 'void'

//create the content pane
public void createContentPane()
{
	  //populate the JComboBox
	  stateCombo.addActionListener(this);
	  zipCombo.addActionListener(this);
	  zipCombo.setEditable(true);
}



Change 2 - Attach your combo and zip code text fields to the form using fourthrow in layout manager - personalInfo()

// Attach city, stateCombo, and zip fields to form using Grid
fourthRow.add(city);
fourthRow.add(stateCombo);
fourthRow.add(zip);



Change 3 - Your flowlayout paraemters were cutting off a bit of the controls, readjusted to look better - personalInfo()

// Change vgap parameter to zero in flowlayout manager
FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,0);




This should get you up and running. Enjoy! :)

Was This Post Helpful? 0
  • +
  • -

#7 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 7,676
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Posted 21 September 2007 - 09:32 AM

Well that is because you don't define a submitbutton, you then don't add it to the layout row, then you don't add the row to the panel and lastly you don't check in your actionPerformed method for the source coming from the button. So in short it doesn't work because you haven't written code for a button yet (sure you have a few lines commented out, but you never create the button etc).

Define a button...

JButton submitButton = new JButton("Submit");



Add it to your row layouts, lets put it on row 5...

fifthRow.add(submitButton);



Lets then add that row to the panel...

fieldPanel.add(fifthRow);



Then lets attach the event handler to it....

submitButton.addActionListener(this);



Lastly, check if the button is the source of the actionPerformed...

if (e.getSource() == submitButton) { 
	  //do something here 
}




There you go. :)
Was This Post Helpful? 0
  • +
  • -

#8 moses316  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 26
  • Joined: 13-September 07


Dream Kudos: 0

Posted 21 September 2007 - 11:18 AM

Thanks for the help again! It worked out great, for some reason my submit button and cancel button don't display all the way, but I guess I can live with it.





View PostMartyr2, on 21 Sep, 2007 - 12:32 PM, said:

Well that is because you don't define a submitbutton, you then don't add it to the layout row, then you don't add the row to the panel and lastly you don't check in your actionPerformed method for the source coming from the button. So in short it doesn't work because you haven't written code for a button yet (sure you have a few lines commented out, but you never create the button etc).

Define a button...

JButton submitButton = new JButton("Submit");



Add it to your row layouts, lets put it on row 5...

fifthRow.add(submitButton);



Lets then add that row to the panel...

fieldPanel.add(fifthRow);



Then lets attach the event handler to it....

submitButton.addActionListener(this);



Lastly, check if the button is the source of the actionPerformed...

if (e.getSource() == submitButton) { 
	  //do something here 
}




There you go. :)

Was This Post Helpful? 0
  • +
  • -

#9 Armyant20  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 2
  • Joined: 18-September 07


Dream Kudos: 0

Posted 26 September 2007 - 03:08 PM

moses316
I'm running into the same issues, did you ever get it to work???

View Postmoses316, on 21 Sep, 2007 - 12:18 PM, said:

Thanks for the help again! It worked out great, for some reason my submit button and cancel button don't display all the way, but I guess I can live with it.





View PostMartyr2, on 21 Sep, 2007 - 12:32 PM, said:

Well that is because you don't define a submitbutton, you then don't add it to the layout row, then you don't add the row to the panel and lastly you don't check in your actionPerformed method for the source coming from the button. So in short it doesn't work because you haven't written code for a button yet (sure you have a few lines commented out, but you never create the button etc).

Define a button...

JButton submitButton = new JButton("Submit");



Add it to your row layouts, lets put it on row 5...

fifthRow.add(submitButton);



Lets then add that row to the panel...

fieldPanel.add(fifthRow);



Then lets attach the event handler to it....

submitButton.addActionListener(this);



Lastly, check if the button is the source of the actionPerformed...

if (e.getSource() == submitButton) { 
	  //do something here 
}




There you go. :)

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month