Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Combobox Help

 
Marie Jeanne Thibault
Greenhorn
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
{
//Declare
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.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 300);
frame.setVisible(true);
}
//Constructor
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
jbtStore.addActionListener(this);
}
//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((String)jcboProv.getSelectedItem());
jtaArea.append(post + "\n");
jtaArea.repaint();

}
}
}
 
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
Greenhorn
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.
Marie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic