combobox question
I am trying to make 2 combo boxes.1 for state and 1 for zip. I have the array anf GUI setup but I can't figure out what I'm doing wrong. Can someone check it out and provide guidance...please?
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;
publicclass personalInfoextends JFrameimplements 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);
}
publicstaticvoid 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);
}
publicvoid actionPerformed(ActionEvent e)
{
//user clicks the combo box
if (e.getSource() == stateCombo)
if (e.getSource() == zipCombo)
}
}
[7772 byte] By [
dbrinea] at [2007-11-27 4:56:05]

Perhaps you could describe your problem. Most people are unlikely to copy, compile and run your code.
Where are you adding either of the combo boxes in a panel? If they're not in a JPanel, they can't be seen. It's as if they didn't exist.
adding them here. The problem is that I don't see the combo boxes. GUI works I see the state and zip textbox but that's about it. not drop downs.fourthRow.add(city);stateCombo.add(state);zipCombo.add(zip);
What exactly are state and zip?Perhaps you mean stateCombo and zipCombo.
In a previous reply it was mentioned that combo box had to in a JPanel. How do I do that and keep the same layout. Ie City (textfield), state and zip all on the last row
You can keep them on the last row by putting the combo boxes in their own jpanel and adding that jpanel to a master jpanel as borderlayout.south making sure that the master panel uses borderlayout.
Still you are adding stuff to the comboboxes, but you have not added them to any component.
i.e.:
JPanel myPanel = new JPanel();
myPanel.add(zipCombo);
myPanel.add(stateCombo);
.......
You have to do this. otherwise you will not solve this problem.
Am I able to add the panel like this? Boxes don't show but the city does
fourthRow.add(city);
myPanel.add(stateCombo);
myPanel.add(zipCombo);
here is the layout I'm using (format desired)
fieldPanel.setLayout(new GridLayout(8,1));
FlowLayout rowSetup = newFlowLayout(FlowLayout.LEFT,5,3);
firstRow.setLayout(rowSetup);
secondRow.setLayout(rowSetup);
thirdRow.setLayout(rowSetup);
fourthRow.setLayout(rowSetup);
Message was edited by:
dbrine
> Am I able to add the panel like this? Boxes don't
> show but the city does
>
> fourthRow.add(city);
> myPanel.add(zipCombo);
>
> here is the layout I'm using (format desired)
>
> fieldPanel.setLayout(new
> GridLayout(8,1));
> FlowLayout rowSetup = new
>FlowLayout(FlowLayout.LEFT,5,3);
> irstRow.setLayout(rowSetup);
> secondRow.setLayout(rowSetup);
> thirdRow.setLayout(rowSetup);
> fourthRow.setLayout(rowSetup);
>
> Message was edited by:
That's fine if myPanel is a panel that is added to your GUI, but in my pseudocode, myPanel was a generic name for any panel. So if you are not adding myPanel to a visible panel, you have to add your state and zip comboboxes to one of your panels, one that is either added to another panel or added to the jframe.
I just kept the myPanel name for now just to get it working. visible is true. But I still don't see it? What am I missing? Thanks for all the help.
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);
//populate JComboBox
stateCombo.addItem();
zipCombo.addItem();
stateCombo.addActionListener(this);
zipCombo.addActionListener(this);
//construct JComboBox
JPanel myPanel = new JPanel();
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);
myPanel.add(stateCombo);
myPanel.add(zipCombo);
//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)
{
}
}
good god man, I can only say this one more time, then I'm through. nothing is visible unless it is eventually added to the JFrame. Period.
You've added the comboboxes to the myPanel, but what did you add the myPanel to?
The comboboxes have to be added to a panel or something that is eventually in some way added to the jframe of the application.
You can nest panels, but eventually the parent-most panel must be added to a top-level container such as a JFrame.
I can't get more blunt or clear than that.
Sorry, about losing patience, but I can only say this so much. Good luck, it's all up to you now.
Please read this article titled [url=http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html]Using Top-Level Containers[/url].
One of the statements contained in the article is:
"To appear onscreen, every GUI component must be part of a containment hierarchy.
A containment hierarchy is a tree of components that has a top-level container as its root. "
Message was edited by:
petes1234
Hay bro , is this your code ?!!!!!!!!!!!!!!!!!
There are lots of mistakes you have done !!!!!!!!!!!!!!!!!!!!!
You first study the container , component and listener buddy .
Then post codes .
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
//initialize array
private static String zipCode[] = {"16503", "16502", "16504", "16505", "16501"};
private static 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"};
private static JComboBox stateCombo = new JComboBox(states);
private static JComboBox zipCombo = new JComboBox(zipCode);
//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);
//populate JComboBox
//stateCombo.addActionListener(this);
//zipCombo.addActionListener(this);
//construct JComboBox
JPanel myPanel = new JPanel();
public static void main(String[] args)
{
personalInfo f= new personalInfo();
//stateCombo.addItem(states);
//zipCombo.addItem(zipCode);
f.setDefaultCloseOperation(JFrame.EXIT_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);
myPanel.add(stateCombo);
myPanel.add(zipCombo);
//add rows to panel
fieldPanel.add(firstRow);
fieldPanel.add(secondRow);
fieldPanel.add(thirdRow);
fieldPanel.add(fourthRow);
fieldPanel.add(myPanel);
stateCombo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.out.println("For stateCombo's Action !!!!!! ");
}
});
zipCombo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.out.println("For zipCombo's Action !!!!!! ");
}
});
//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)
{
}
}
Thanks
sb
Again,
If you want to select an Item from that combo then use this code ->
stateCombo.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.out.println("For stateCombo's Action !!!!!! ");
System.out.println(String.valueOf(stateCombo.getSelectedItem()));
}
});
Thanks ,
sb