Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 306,814 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,667 people online right now. Registration is fast and FREE... Join Now!




Creating JComboBoxes

 

Creating JComboBoxes

moses316

19 Sep, 2007 - 05:30 PM
Post #1

New D.I.C Head
*

Joined: 13 Sep, 2007
Posts: 26


My Contributions
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.

CODE

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 Sep, 2007 - 05:38 PM

User is offlineProfile CardPM
+Quote Post


William_Wilson

RE: Creating JComboBoxes

19 Sep, 2007 - 05:39 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,667



Thanked: 97 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

moses316

RE: Creating JComboBoxes

19 Sep, 2007 - 05:51 PM
Post #3

New D.I.C Head
*

Joined: 13 Sep, 2007
Posts: 26


My Contributions
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?

QUOTE(William_Wilson @ 19 Sep, 2007 - 08: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.


User is offlineProfile CardPM
+Quote Post

moses316

RE: Creating JComboBoxes

20 Sep, 2007 - 05:20 AM
Post #4

New D.I.C Head
*

Joined: 13 Sep, 2007
Posts: 26


My Contributions
Still looking for some help on this one.
User is offlineProfile CardPM
+Quote Post

Martyr2

RE: Creating JComboBoxes

20 Sep, 2007 - 09:27 AM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,305



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

My Contributions
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'

CODE

//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()

CODE

// 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()

CODE

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



This should get you up and running. Enjoy! smile.gif
User is online!Profile CardPM
+Quote Post

moses316

RE: Creating JComboBoxes

21 Sep, 2007 - 09:07 AM
Post #6

New D.I.C Head
*

Joined: 13 Sep, 2007
Posts: 26


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





QUOTE(Martyr2 @ 20 Sep, 2007 - 12:27 PM) *

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'

CODE

//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()

CODE

// 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()

CODE

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



This should get you up and running. Enjoy! smile.gif


User is offlineProfile CardPM
+Quote Post

Martyr2

RE: Creating JComboBoxes

21 Sep, 2007 - 09:32 AM
Post #7

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,305



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

My Contributions
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...

CODE

JButton submitButton = new JButton("Submit");


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

CODE

fifthRow.add(submitButton);


Lets then add that row to the panel...

CODE

fieldPanel.add(fifthRow);


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

CODE

submitButton.addActionListener(this);


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

CODE

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



There you go. smile.gif
User is online!Profile CardPM
+Quote Post

moses316

RE: Creating JComboBoxes

21 Sep, 2007 - 11:18 AM
Post #8

New D.I.C Head
*

Joined: 13 Sep, 2007
Posts: 26


My Contributions
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.





QUOTE(Martyr2 @ 21 Sep, 2007 - 12:32 PM) *

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...

CODE

JButton submitButton = new JButton("Submit");


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

CODE

fifthRow.add(submitButton);


Lets then add that row to the panel...

CODE

fieldPanel.add(fifthRow);


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

CODE

submitButton.addActionListener(this);


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

CODE

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



There you go. smile.gif


User is offlineProfile CardPM
+Quote Post

Armyant20

RE: Creating JComboBoxes

26 Sep, 2007 - 03:08 PM
Post #9

New D.I.C Head
*

Joined: 18 Sep, 2007
Posts: 2


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

QUOTE(moses316 @ 21 Sep, 2007 - 12:18 PM) *

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.





QUOTE(Martyr2 @ 21 Sep, 2007 - 12:32 PM) *

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...

CODE

JButton submitButton = new JButton("Submit");


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

CODE

fifthRow.add(submitButton);


Lets then add that row to the panel...

CODE

fieldPanel.add(fifthRow);


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

CODE

submitButton.addActionListener(this);


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

CODE

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



There you go. smile.gif



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 10:04PM

Live Java Help!

Be Social

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

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month