I've been having problems with my form program while trying to call data entered into a text field and displaying in a GUI after pressing a button. Here's my code.

import java.awt.*;
import java.awt.event.*;

public class ProjectForm {
  public static void main(String[] args) {
    Frame frm=new Frame("Personal Information Form");
    frm.setSize(350,200);
    frm.setVisible(true);
    frm.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){
        System.exit(0);
      }
    });
        Panel p = new Panel();
    Panel p1 = new Panel();
    Label jFirstName = new Label("First Name");
    TextField lFirstName = new TextField(20);
    Label jLastName =new Label("Last Name");
    TextField lLastName=new TextField(20);
    Label jYear = new Label("YYYY");
    TextField lYear = new TextField(4);
    Label jMonth = new Label("MM");
    TextField lMonth = new TextField(2);
    Label jDay = new Label("DD");
    TextField lDay = new TextField(2);
    p.setLayout(new GridLayout(12,0));
    p.add(jFirstName);
    p.add(lFirstName);
    p.add(jLastName);
    p.add(lLastName);
    p1.add(jDay);
    p1.add(lDay);
    p1.add(jMonth);
    p1.add(lMonth);
    p1.add(jYear);
    p1.add(lYear);
    Button Submit=new Button("Submit");
    p.add(Submit);
    p1.add(p);
    frm.add(p1,BorderLayout.NORTH);
    String FName = lFirstName;



  }
  public void windowOpened(WindowEvent e) {}
	public void windowActivated(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}


}

You need to add an actionListener to the button, so when it is clicked a method would be called.
Look at the API for the Button and for ActionListener.

Also try to use the swing package: javax.swing.*
And the classes: JFrame, JButton, ....

Also it is good to read some more current tutorials about GUI, because your way is not the best way.

You need to add an actionListener to the button, so when it is clicked a method would be called.
Look at the API for the Button and for ActionListener.

Also try to use the swing package: javax.swing.*
And the classes: JFrame, JButton, ....

Also it is good to read some more current tutorials about GUI, because your way is not the best way.

Thanks. Maybe that'll help.
But, what exactly is the difference between Button and JButton?

Edited 6 Years Ago by iZander: n/a

Thanks. Maybe that'll help.
But, what exactly is the difference between Button and JButton?

I don't know. But it is better to use the latest J classes.

I don't know. But it is better to use the latest J classes.

Alright thanks. :]

Alright, I've tried adding an actionlistener to my JButton, but I get an error "cannot find symbol method ActionListener(javax.swing.JButton)" Help please.

Edited 6 Years Ago by iZander: n/a

Because there is no such method. You cannot just invent methods on your own. Look at the API of the JButton.

Because there is no such method. You cannot just invent methods on your own. Look at the API of the JButton.

Alright. I'll give it a shot, I'm still really new to Java if you couldn't tell.

The JButton's method (addActionListener) takes as argument an ActionListener interface. If you implement it and look at the API you will see that there is only one method to implement. Whenever you click the button that method is called automatically, so whatever code you put there would be executed.

This article has been dead for over six months. Start a new discussion instead.