Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6177285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:14:49+00:00 2026-05-24T00:14:49+00:00

I’ve got one class that has two text fields in it: name and surname.

  • 0

I’ve got one class that has two text fields in it: “name” and “surname”. I need the information that someone typed in there for a text area in another class. So far I managed to write (Person class):

public class Person extends JPanel implements ActionListener {

TextField nameField;
JTextField surnameField;
public String name;
public String surname;
final static int BIG_BORDER = 75;
final static int SMALL_BORDER = 10;
final static int ELEMENTsLENGHT = 320;
final static int VERTICAL_SPACE = 10;
final static int VERTICAL_SPACE_PLUS = 25;
final static int HORIZONTAL_SPACE = 75;
final static int SPACEforELEMENT_LABEL = 50;
final static int SPACEforELEMENT_TEXT = 40;
final static int H_SPACEforBUTTON = 64;
final static int V_SPACEforBUTTON = 26; 

public Person() {
    init();
}

public void init() {

    JLabel nameLabel = new JLabel("Please enter your name:");
    JLabel surnameLabel = new JLabel("Please enter your surname:");

    nameField = new JTextField();
    nameField.addActionListener(this);
    surnameField = new JTextField();
    surnameField.addActionListener(this);

    nextButton = new JButton("NEXT");
    nextButton.setActionCommand(next);
    nextButton.addActionListener(this);

    JPanel panelButton = new JPanel();
    panelButton.add(nextButton);        

    double size[][] = {
            { BIG_BORDER, ELEMENTsLENGHT, HORIZONTAL_SPACE,
                    H_SPACEforBUTTON, SMALL_BORDER }, // Columns
            { BIG_BORDER, SPACEforELEMENT_LABEL, VERTICAL_SPACE,
                    SPACEforELEMENT_TEXT, VERTICAL_SPACE_PLUS,
                    SPACEforELEMENT_LABEL, VERTICAL_SPACE,
                    SPACEforELEMENT_TEXT, VERTICAL_SPACE_PLUS,
                    SPACEforELEMENT_LABEL, VERTICAL_SPACE,
                    V_SPACEforBUTTON, SMALL_BORDER } }; // Rows

    setLayout(new TableLayout(size));

    add(nameLabel, "1,1,1,1");
    add(nameField, "1,3,1,1");
    add(surnameLabel, "1,5,1,1");
    add(surnameField, "1,7,1,1");
    add(nextButton, "3,11,1,1");
}   

public static void createAndShowGUI() {
    JFrame frame = new JFrame("Identification");
    frame.getContentPane().add(new Person());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(550, 450);
    frame.setResizable(false);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {

    name = nameField.getText();
    surname = surnameField.getText();

    if (e.getActionCommand().equalsIgnoreCase(next)) {
        Person.showNextWindow();
    } 
}

public static void showNextWindow() {
    //cardLayout.next(this);
    System.out.println("go to the next window");
}

public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}

The other class now:

public class Greeting extends JPanel implements ActionListener {

Person person = new Person();   

String name = person.name;
String surname = person.surname;

public Greeting() {
    super(new BorderLayout());
    init();
}

public void init() {

    nextButton = new JButton("NEXT");
    nextButton.setActionCommand(next);      
    nextButton.addActionListener(this);     
    //nextButton.setMnemonic('rightArrow');     

    String q = "How are you today, "+name+" "+surname+"?";          

    JTextArea textArea = new JTextArea(q); 
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.setEditable(false);
    add(textArea, BorderLayout.NORTH);      

    JPanel btnPanel = new JPanel();     
    btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.LINE_AXIS));
    btnPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
    btnPanel.add(Box.createHorizontalGlue());                       
    btnPanel.add(nextButton);
        btnPanel.setAlignmentX(RIGHT_ALIGNMENT);
    add(btnPanel, BorderLayout.SOUTH);      

} // end init

public static void showNextWindow() {
    //cardLayout.next(this);
    System.out.println("go to the next window");
}

public void actionPerformed(ActionEvent e) {

}

public static void createAndShowGUI() {

    JFrame frame = new JFrame("How are you");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new Greeting());     
    frame.setSize(550, 450);
    frame.setResizable(false);
    //frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

public static void main(String[] args) {
    // Schedule a job for the event-dispatching thread:
    // creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

}

But what I see in the window is: “How are you today, null null?” Any fresh eyes to catch what’s wrong? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:14:50+00:00Added an answer on May 24, 2026 at 12:14 am

    You never show us that you’ve proven that the ActionListener’s are called after the text has been entered into the fields and before the String variables are used elsewhere. Regardless, it’s a bad design.

    I’d give the class with the JTextFields public getter methods, not public variables, and in the getter methods, I’d extract the text currently in the corresponding JTextField. For instance, something like:

    private JTextField nameField = new JTextField();
    private JTextField surnameField = new JTextField();
    
    public String getName() {
      return nameField.getText();
    }
    
    public String getSurname() {
      return surnameField.getText();
    }
    
    //... etc...
    

    For example, here is an SSCCE that demonstrates your problem and a solution:

    import java.awt.event.ActionEvent;
    import javax.swing.*;
    
    public class InfoFromTextFields {
       private static void createAndShowUI() {
          JFrame frame = new JFrame("InfoFromTextFields");
          frame.getContentPane().add(new MainGui());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    class NamePanel extends JPanel {
       private JTextField nameField = new JTextField(10);
       private JTextField surnameField = new JTextField(10);
    
       public NamePanel() {
          add(new JLabel("Name:"));
          add(nameField);
          add(Box.createHorizontalStrut(15));
          add(new JLabel("Surname:"));
          add(surnameField);
       }
    
       public String getNameText() {
          return nameField.getText();
       }
    
       public String getSurnameText() {
          return surnameField.getText();
       }
    }
    
    class MainGui extends JPanel {
       private JTextField nameField = new JTextField(10);
       private JTextField surnameField = new JTextField(10);
    
       public MainGui() {
          nameField.setEditable(false);
          surnameField.setEditable(false);
    
          add(new JLabel("Name:"));
          add(nameField);
          add(Box.createHorizontalStrut(15));
          add(new JLabel("Surname:"));
          add(surnameField);
    
          add(Box.createHorizontalStrut(15));
          add(new JButton(new AbstractAction("Get Names") {
             @Override
             public void actionPerformed(ActionEvent arg0) {
                NamePanel namePanel = new NamePanel();
                int result = JOptionPane.showConfirmDialog(nameField, namePanel,
                         "Get Names", JOptionPane.OK_CANCEL_OPTION);
                if (result == JOptionPane.OK_OPTION) {
                   nameField.setText(namePanel.getNameText());
                   surnameField.setText(namePanel.getSurnameText());
                }
             }
          }));
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm making a simple page using Google Maps API 3. My first. One marker
I need to clean up various Word 'smart' characters in user input, including but
I have just tried to save a simple *.rtf file with some websites and
I have a JSP page retrieving data and when single or double quotes are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.