This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Combobox Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Combobox Help" Watch "Combobox Help" New topic

Combobox Help

Marie Jeanne Thibault

Joined: Nov 15, 2003
Posts: 15
Hi Everyone,
I'm new to Java and this site. Can anyone out there please help me with my code? The program compiles but the province and postal code won't transfer on to the text area. I also get a whole bunch of exceptions. I'm sure the problem lies in the actionPerformed method but I've run out of ideas. I've figured out that I don't need an ItemListener for the jcb because I'm not asking it to do anything.
Here's my code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class UniStore extends JFrame implements ActionListener
private JTextField jtfName, jtfDept, jtfUni, jtfPost;
private JButton jbtStore;
private JComboBox jcboProv;
private JTextArea jtaArea;
private String[] provName = {"BC", "AB", "SK", "MN", "ON", "QC", "NF",
"PEI", "NB", "NS"};

//Main method
public static void main(String[] args)
UniStore frame = new UniStore();
frame.setSize(600, 300);
public UniStore()
setTitle("Educational Institution");

//Create panel p1
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3, 2, 5, 5));
p1.add(new JLabel("Name"));
p1.add(jtfName = new JTextField());
p1.add(new JLabel("Department"));
p1.add(jtfDept = new JTextField());
p1.add(new JLabel("University"));
p1.add(jtfUni = new JTextField());

//Create p2

JComboBox jcboProv = new JComboBox(provName);

JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(new JLabel("Province"));
p2.add(jcboProv = new JComboBox(provName));
p2.add(new JLabel("Postal Code"));
p2.add(jtfPost = new JTextField(10));
//Create p3
JPanel p3 = new JPanel();
p3.setLayout(new FlowLayout(FlowLayout.LEFT));
p3.add(jtaArea = new JTextArea(8,30));
p3.add(jbtStore = new JButton("Store"));
//Set panels in frame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.NORTH);
getContentPane().add(p2, BorderLayout.WEST);
getContentPane().add(p3, BorderLayout.SOUTH);
//Register Listeners
//Transfer the information to the textarea
public void actionPerformed(ActionEvent e)

if (e.getSource() == jbtStore)

String name = (jtfName.getText());
String dept = (jtfDept.getText());
String uni = (jtfUni.getText());

String post = (jtfPost.getText());

jtaArea.append(name + "\n");
jtaArea.append(dept + "\n");
jtaArea.append(uni + "\n");
jtaArea.append(post + "\n");

Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
You do not need pack if setting size explicitly.
You do not need repainting the text area.

Be careful with the variable jcboProv. It is first defined as an instance field, then shadowed by a local declaration in the constructor. Because you did not initialize this field, but the local variable in the constructor, when the actionperformed method refers later to the field, it finds that it was not intialized.

Maybe you find interesting a basic Java Tutorial like: this one
There is also a Tutorial for constructing Java GUIs.
[ November 16, 2003: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Marie Jeanne Thibault

Joined: Nov 15, 2003
Posts: 15
Jose, thank you soooo much for helping me out. I've been agonizing over this one for a long time. I won't make the same mistake again.
I agree. Here's the link:
subject: Combobox Help