package now;

   import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Now extends JFrame {

JLabel FirstName;
JTextField jtfFirstName;
JLabel LastName;
JTextField jtfLastName;
JLabel Age;
JTextField jtfAge;


public Now(){

//Set FlowLayout    
  setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));

    //Add labels and text fields to the frame

    add (new JLabel("First Name"));
    add (new JTextField(15));
    add (new JLabel("Last Name"));
    add (new JTextField(15));
    add (new JLabel("Age"));
    add (new JTextField(2));

   JButton welcome = new JButton ("CLICK HERE");

    //Create Panel to hold button
    JPanel panel = new JPanel();
    panel.add(welcome);

    event e = new event();
    welcome.addActionListener(e);
}

        public class event implements ActionListener{
            public void actionPerformed(ActionEvent e){


          JOptionPane.showMessageDialog(null, "Welcome, " );

        }
        }

public static void main(String[] args) {
    Now frame = new Now();
    frame.setTitle("Welcome!");
    frame.setSize(200, 250);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

I am trying to have a user input their first name, last name, and age and when they click the JButton I want it to say "Welcome, First Name & Last name!!! Any assistance would be greatly appreciate it.

I know I need a string within this area

        public class event implements ActionListener{
            public void actionPerformed(ActionEvent e){


          JOptionPane.showMessageDialog(null, "Welcome, " );

I do not know how to set it up.

Where are the values that you want to add to the showMessageDialog() call? Where will the user input them to the program so you can get the values and build the message to be displayed?
Do you define varables that will have the values?

Edited 4 Years Ago by NormR1

package now;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Now extends JFrame {
JLabel FirstName;
JTextField jtfFirstName;
JLabel LastName;
JTextField jtfLastName;
JLabel Age;
JTextField jtfAge;
public Now(){
//Set FlowLayout
setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
//Add labels and text fields to the frame
add (new JLabel("First Name"));
add (new JTextField(15));
add (new JLabel("Last Name"));
add (new JTextField(15));
add (new JLabel("Age"));
add (new JTextField(2));
JButton welcome = new JButton ("CLICK HERE");
//Create Panel to hold button
JPanel panel = new JPanel();
panel.add(welcome);
event e = new event();
welcome.addActionListener(e);
}
public class event implements ActionListener{
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "Welcome, " );
}
}
public static void main(String[] args) {
Now frame = new Now();
frame.setTitle("Welcome!");
frame.setSize(200, 250);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Well I would first suggest that you actually add the panel for the button to the frame, dont know if you forgot or havent got their yet... Next you should declare 3 variables to hold the name, surname and age of the person. Most likely you'll have two strings and an int. To get the name and surname from the text field use the getText() method. and also just to add... you will in the action performed method get the text in the text fields and assign them to the appropiate variables then display them using the variable names

Edited 4 Years Ago by DavidKroukamp: clarification

package now;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class Now extends JFrame {

    //Create text fields for FirstName, LastName, and Age

    private JTextField jtfFirstName = new JTextField();
    private JTextField jtfLastName = new JTextField();
    private JTextField jtfAge = new JTextField();

    //Create a Welcome message button
    private JButton jbtWelcome = new JButton("Welcome!");

public Now(){
    //Panel p1 to hold labels and text fields
    JPanel p1 = new JPanel(new GridLayout(3, 2));
    p1.add(new JLabel("Enter First Name"));
    p1.add(jtfFirstName);
    p1.add(new JLabel("Enter Last Name"));
    p1.add(jtfLastName);
    p1.add(new JLabel("Enter Age"));
    p1.add(jtfAge);
    p1.setBorder(new
       TitleBorder("Enter surname, and age"));

    //Panel p2 to hold the button
    JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    p2.add(jbtWelcome);

    //Add the panels to the frame
    add(p1, BorderLayout.CENTER);
    add(p2, BorderLayout.SOUTH);

    //Register listener
    jbtWelcome.addActionListener(new ButtonListener());
        }

    //Handle the Welcome Message button
    private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            //Get value from text fields

        }
    }

public static void main(String[] args) {
    Now frame = new Now();
    frame.setTitle("Welcome!");
    frame.setSize(200, 250);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
}

Okay, I need help pulling the values from the FirstName and LastName textfields to output once the user hits the Welcome button. Can anyone demonstrate this code for me. :D

package now;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class Now extends JFrame {

    //Create text fields for FirstName, LastName, and Age

    private JTextField jtfFirstName = new JTextField();
    private JTextField jtfLastName = new JTextField();
    private JTextField jtfAge = new JTextField();

    //Create a Welcome message button
    private JButton jbtWelcome = new JButton("Welcome!");

public Now(){
    //Panel p1 to hold labels and text fields
    JPanel p1 = new JPanel(new GridLayout(3, 2));
    p1.add(new JLabel("Enter First Name"));
    p1.add(jtfFirstName);
    p1.add(new JLabel("Enter Last Name"));
    p1.add(jtfLastName);
    p1.add(new JLabel("Enter Age"));
    p1.add(jtfAge);
    p1.setBorder(new
            TitledBorder("Enter your first name, last name, and age."));

    //Panel p2 to hold the button
    JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    p2.add(jbtWelcome);

    //Add the panels to the frame
    add(p1, BorderLayout.CENTER);
    add(p2, BorderLayout.SOUTH);

    //Register listener
    jbtWelcome.addActionListener(new ButtonListener());
        }

    //Handle the Welcome Message button
    private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            //Get value from text fields
            double name =
                    Double.parseDouble(jtfFirstName.getText());
            double surname =
                    Double.parseDouble(jtfLastName.getText());

            Message message = new Message(name, surname);

            //Display first and last name
            jtfFirstName.setText(String.format("",
                    message.getFirstName()));

            jtfLastName.setText(String.format("",
                    message.getLastName()));

        }
    }

public static void main(String[] args) {
    Now frame = new Now();
    frame.setTitle("Welcome!");
    frame.setSize(200, 250);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
}

Okay, this is what I have now. I do not know if this is completely accurate but now I am having issues with line 55.

  Message message = new Message(name, surname);

Message - cannot find symbol.

The demonstration in my book does not have a separate class for Message. and I do not think the get text method for lines 47 - 63 is completely accurate for strings:

    //Handle the Welcome Message button
    private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            //Get value from text fields
            double name =
                    Double.parseDouble(jtfFirstName.getText());
            double surname =
                    Double.parseDouble(jtfLastName.getText());

            Message message = new Message(name, surname);

            //Display first and last name
            jtfFirstName.setText(String.format("",
                    message.getFirstName()));

            jtfLastName.setText(String.format("",
                    message.getLastName()));

Message - cannot find symbol

Where is the definition for the Message class? The compiler can not find it.

Yes, but it does not state making a definition for Message in the book. It is not displayed in the code at all for the example. so How would that work out?

and is the getText and setText method correct?

What purpose does the Message class have? Can you remove it and use the variables with the names directly in place of putting them into the Message class/

is the getText and setText method correct?

Does it compile and do what you want when you execute it?

Dude, you are not helping at all... Oh my goodness, I cannot get the First name, and last name to display. I need help with it, and you are not helping me at all.

Are you reading what I am writing?

Where are the values you want to display? Where does the user input the names? I imagine they are in some text field somewhere. Find the text fields that have the names and get the names from there.

You have some variables names jtfFirstName and jftLastName. Is this where the use hase typed the name?
I don't understand why the code gets the values from those text fields and tries to convert the values to doubles.
Why is that?

double name = Double.parseDouble(jtfFirstName.getText());

Maybe you should get the values into Strings:
String name = jtfFirstName.getText());

I got that....

    //Handle the Welcome Message button
    private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            //Get value from text fields
           String name = jtfFirstName.getText();
           String surName = jtfLastName.getText();

now what to do, I got that part of it.

If you have the names, what do you need now? I thought your problem was getting the names so you could use them to display. Now you have them, use them as you wanted in the "Welcome .." message.

Edited 4 Years Ago by NormR1

Here is the answer to my program.

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


public class JustinDillingerHW3 extends JFrame {

//Create text fields and labels
private JTextField jtfFirstName = new JTextField(15);
private JTextField jtfLastName = new JTextField(15);
private JTextField jtfAge = new JTextField(2);
private JLabel WelcomeMessage;


public static void main(String[] args){

    JustinDillingerHW3 frame = new JustinDillingerHW3();
    frame.setTitle("Hello User!!");
    frame.setSize(300,250);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);
}

public JustinDillingerHW3(){

    setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));

    //add labels and text fields to the frame
    add(new JLabel("First Name"));
    add(jtfFirstName);
    add(new JLabel("Last Name"));
    add(jtfLastName);
    add(new JLabel("Age"));
    add(jtfAge);

    //Create Welcome button
    JButton Welcome = new JButton("Click Here");
    add(Welcome);

    Welcome.addActionListener(new SubmitListener());
    WelcomeMessage = (new JLabel(" "));
    add(WelcomeMessage);

}
class SubmitListener implements ActionListener{
    public void actionPerformed(ActionEvent e){

        String FirstName = jtfFirstName.getText();
        String SurName = jtfLastName.getText();
        WelcomeMessage.setText("Welcome, " + FirstName + " " + SurName);
    }
}
}
This question has already been answered. Start a new discussion instead.