This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Combobox Help  RSS feed

Marie Jeanne Thibault
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
Marie Jeanne Thibault
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!