-1

Can you help me with this?

Here is the code first:

public RegistrationForm(){

    super("Registration Form (Assignment One)");
    setLayout(new GridLayout(6,2));

    l[0] = new JLabel("Name: ");
    l[0].setFont(new Font("Calibri Head",Font.BOLD,12));
    add(l[0]);

    tf[0] = new JTextField();
    tf[0].setToolTipText("Enter Your Full Name");
    add(tf[0]);


    l[1] = new JLabel("Age: ");
    l[1].setFont(new Font("Calibri Head",Font.BOLD,12));
    add(l[1]);

    tf[1] = new JTextField();
    tf[1].setToolTipText("Enter Your Age");
    add(tf[1]);

    l[2] = new JLabel("Birthday: ");
    l[2].setFont(new Font("Calibri Head",Font.BOLD,12));
    add(l[2]);

    tf[2] = new JTextField();
    tf[2].setToolTipText("Enter Your Birthday");
    add(tf[2]);

    l[3] = new JLabel("Address: ");
    l[3].setFont(new Font("Calibri Head",Font.BOLD,12));
    add(l[3]);

    tf[3] = new JTextField();
    tf[3].setToolTipText("Enter Your Address");
    add(tf[3]);

    l[4] = new JLabel("Contact Number: ");
    l[4].setFont(new Font("Calibri Head",Font.BOLD,12));
    add(l[4]);

    tf[4] = new JTextField();
    tf[4].setToolTipText("Enter Your Contact Number");
    add(tf[4]);

    b[0] = new JButton("Submit");
    b[0].addActionListener(this);
    add(b[0]);

    b[1] = new JButton("Clear");
    b[1].addActionListener(this);
    add(b[1]);
}

So When I input a value to all and press "Submit" the previous class will close and another class will open and there it will show the value of the things I inputted from the previous class. . .

There is no default value to JTextfields, I'm going to enter the value myself.

How can i throw(I mean pass) a value to the other class?

Here is the code i have so far:

This is my method:

public String name(){
return tf[0].getText();
}

This is from my Other class:

public Form{
RegistrationForm form = new RegistrationForm();

JTextField name = form.name();
add(name);
}
user2226703
  • 9
  • 1
  • 4

1 Answers1

3

You don't need to throw anything. Whatever class that displays this dialog will hold a reference to the instance of this class and can simply query the state of the fields once the dialog returns. This is much easier if the dialog window is a modal dialog such as a modal JDialog or a JOptionPane.

For instance, please look at my code in this example.

Edit
Also, this confuses me:

public Form{
  RegistrationForm form = new RegistrationForm();

  JTextField name = form.name();
  add(name);
}

Does this code display the RegistrationForm object? Is RegistrationForm in fact a modal JDialog? It is very unusual to extract a JTextField from one GUI and add it to another, and I'm pretty sure that you don't want to do this. Again, what you want to do is:

  • Display your RegistrationForm as a modal JDialog.
  • After it returns, call getter methods on the RegistrationForm object that extracts the Strings held by the text fields of the object.

For more details, you'll still need to tell us a lot more about your code and your problem.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • But can you pass the value to the other class? I'm trying to do a method and it keep showing a null value . . . – user2226703 Mar 30 '13 at 23:32
  • @user2226703: Swing GUI objects can get information from each other same as non-Swing objects. You've likely got a bug in your program, and you'll need to tell us or show us more if we're to be able to help you. – Hovercraft Full Of Eels Mar 30 '13 at 23:33
  • There it is . . . Can you help me? Can you give substitute to my code or fix it? – user2226703 Mar 30 '13 at 23:42
  • @user2226703: you need to show us where do you display the registration window? Where do you try to extract information out of it. You're showing us bits and pieces and hoping that we can guess the rest, but we're not quite as powerful or magical as you might think we are. – Hovercraft Full Of Eels Mar 30 '13 at 23:50