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!
//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);
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.
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.
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);
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);
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 }
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 }
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 }