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

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




JComboBox and arraylist

 
Reply to this topicStart new topic

JComboBox and arraylist

dbrine
16 May, 2007 - 02:38 PM
Post #1

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 48


My Contributions
I am trying to make a JComboBox for state that is a drop down with all 50 states. How to create an array with all 50 states without having to right all 50 states out(needs to be uneditable?? And a zip JComboBox that is editable.? I have the GUI done but the comboboxes are whats confusing and the array??? any help would be great. Here is what I got so far.

CODE

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

public class personalInfo extends JFrame implements ActionListener
{
        //construct components
        JComboBox stateCombo = new JComboBox();
        JComboBox zipCombo = new JComboBox();

        //initialize array
        String zipCode[];
        String states[];

        //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 a panel for the fields and buttons
        JPanel fieldPanel = new JPanel();
        //JPanel buttonPanel = 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);

    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);
                fourthRow.add(state);
                fourthRow.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 is offlineProfile CardPM
+Quote Post

Amadeus
RE: JComboBox And Arraylist
16 May, 2007 - 02:59 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
If you don't want to write out all 50 states, you'll need the information from another source. How about storing them in a file, and loading them from there?
User is online!Profile CardPM
+Quote Post

dbrine
RE: JComboBox And Arraylist
16 May, 2007 - 03:02 PM
Post #3

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 48


My Contributions
Do you know any any other arraylists that are already done? Or is writing them out the only way?

QUOTE(Amadeus @ 16 May, 2007 - 03:59 PM) *

If you don't want to write out all 50 states, you'll need the information from another source. How about storing them in a file, and loading them from there?


User is offlineProfile CardPM
+Quote Post

dbrine
RE: JComboBox And Arraylist
19 May, 2007 - 08:33 PM
Post #4

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 48


My Contributions
I wrote the array but I think its too long and I get<identifer> error. I am trying to make a dropdown for the states (uneditable) and zipcode

CODE

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

public class personalInfo extends JFrame implements ActionListener
{
        //construct components
        JComboBox stateCombo = new JComboBox();
        JComboBox zipCombo = new JComboBox();

        //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 a panel for the fields and buttons
        JPanel fieldPanel = new JPanel();
        //JPanel buttonPanel = 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);

    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);
                fourthRow.add(state);
                fourthRow.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 is offlineProfile CardPM
+Quote Post

dbrine
RE: JComboBox And Arraylist
20 May, 2007 - 04:27 PM
Post #5

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 48


My Contributions
OK I got the array to compile but I still don't have combo boxes for the state and zip code?? Where am I going wrong?

CODE

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.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)
    }
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 04:55PM

Be Social

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

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month