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

  • SEARCH
  • Home
  • 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 849997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:11:35+00:00 2026-05-15T07:11:35+00:00

Two questions. First question is I’m trying to create a simple form that when

  • 0

Two questions. First question is I’m trying to create a simple form that when you press a button, it adds a Person object to the ArrayList. However, since I am not used to GUIs, I tried creating one and am first just trying to get the user input from the JTextField, create an ActionListener object of the appropriate type, so once that works, then I can pass in all the JTextField inputs to create my Person object. Unfortunately, I am not getting any data when I type in something to the firstName JTextField and was wondering if someone could look at my code below.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;

public class AddressBook
{
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                AddressBookFrame frame = new AddressBookFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JMenuBar menuBar = new JMenuBar();
                frame.setJMenuBar(menuBar);

                JMenu fileMenu = new JMenu("File");
                JMenuItem openItem = new JMenuItem("Open");
                JMenuItem saveItem = new JMenuItem("Save");
                JMenuItem saveAsItem = new JMenuItem("Save As");
                JMenuItem printItem = new JMenuItem("Print");
                JMenuItem exitItem = new JMenuItem("Exit");
                fileMenu.add(openItem);
                fileMenu.add(saveItem);
                fileMenu.add(saveAsItem);
                fileMenu.add(printItem);
                fileMenu.add(exitItem);
                menuBar.add(fileMenu);

                JMenu editMenu = new JMenu("Edit");
                JMenuItem newItem = new JMenuItem("New");
                JMenuItem editItem = new JMenuItem("Edit");
                JMenuItem deleteItem = new JMenuItem("Delete");
                JMenuItem findItem = new JMenuItem("Find");
                JMenuItem firstItem = new JMenuItem("First");
                JMenuItem previousItem = new JMenuItem("Previous");
                JMenuItem nextItem = new JMenuItem("Next");
                JMenuItem lastItem = new JMenuItem("Last");
                editMenu.add(newItem);
                editMenu.add(editItem);
                editMenu.add(deleteItem);
                editMenu.add(findItem);
                editMenu.add(firstItem);
                editMenu.add(previousItem);
                editMenu.add(nextItem);
                editMenu.add(lastItem);
                menuBar.add(editMenu);

                JMenu helpMenu = new JMenu("Help");
                JMenuItem documentationItem = new JMenuItem("Documentation");
                JMenuItem aboutItem = new JMenuItem("About");
                helpMenu.add(documentationItem);
                helpMenu.add(aboutItem);

                menuBar.add(helpMenu);

                frame.setVisible(true);

            }
        });
    }
}

class AddressBookFrame extends JFrame
{
    public AddressBookFrame() 
    {
        setLayout(new BorderLayout());
        setTitle("Address Book");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        AddressBookToolBar toolBar = new AddressBookToolBar();
        add(toolBar, BorderLayout.NORTH);
        AddressBookStatusBar aStatusBar = new AddressBookStatusBar("5");
        add(aStatusBar, BorderLayout.SOUTH);
        AddressBookForm form = new AddressBookForm();
        add(form, BorderLayout.CENTER);
    }
    public static final int DEFAULT_WIDTH = 500;
    public static final int DEFAULT_HEIGHT = 500;

}

/* Create toolbar buttons and add buttons to toolbar */
class AddressBookToolBar extends JPanel
{
    public AddressBookToolBar()
    {
        setLayout(new FlowLayout(FlowLayout.LEFT));
        JToolBar bar = new JToolBar();
        JButton newButton = new JButton("New");
        JButton editButton = new JButton("Edit");
        JButton deleteButton = new JButton("Delete");
        JButton findButton = new JButton("Find");
        JButton firstButton = new JButton("First");
        JButton previousButton = new JButton("Previous");
        JButton nextButton = new JButton("Next");
        JButton lastButton = new JButton("Last");
        bar.add(newButton);
        bar.add(editButton);
        bar.add(deleteButton);
        bar.add(findButton);
        bar.add(firstButton);
        bar.add(previousButton);
        bar.add(nextButton);
        bar.add(lastButton);
        add(bar);
    }
}

/* Creates the status bar string */
class AddressBookStatusBar extends JPanel
{
    public AddressBookStatusBar(String statusBarString)
    {
        setLayout(new FlowLayout(FlowLayout.LEFT));
        this.statusBarString = new JLabel("Total number of people: " + statusBarString);
        add(this.statusBarString);
    }
    private JLabel statusBarString;
    private int totalContacts;
}

class AddressBookForm extends JPanel
{
    public AddressBookForm()
    {   
        this.setLayout(new GridLayout(2, 1));
        JPanel formPanel = new JPanel();
        formPanel.setLayout(new GridLayout(4, 2));
        JTextField firstName = new JTextField(20);
        JTextField lastName = new JTextField(20);
        JTextField telephone = new JTextField(20);
        JTextField email = new JTextField(20);
        JLabel firstNameLabel = new JLabel("First Name: ", JLabel.LEFT);
        formPanel.add(firstNameLabel);
        formPanel.add(firstName);
        JLabel lastNameLabel = new JLabel("Last Name: ", JLabel.LEFT); 
        formPanel.add(lastNameLabel);
        formPanel.add(lastName);
        JLabel telephoneLabel = new JLabel("Telephone: ", JLabel.LEFT);
        formPanel.add(telephoneLabel);
        formPanel.add(telephone);
        JLabel emailLabel = new JLabel("Email: ", JLabel.LEFT);
        formPanel.add(emailLabel);
        formPanel.add(email);
        add(formPanel);

        JPanel buttonPanel = new JPanel();
        JButton insertButton = new JButton("Insert");
        JButton displayButton = new JButton("Display");

        // create button actions
        AddressBookManager insertAction = new AddressBookManager(firstName.getText());
        insertButton.addActionListener(insertAction);
        buttonPanel.add(insertButton);
        buttonPanel.add(displayButton);
        add(buttonPanel);
    }

    private List<Person> addressList = new ArrayList<Person>();

    private class AddressBookManager implements ActionListener
    {
        public AddressBookManager(String text)
        {
//          addressList.add(
            setName(text);
            System.out.println("Test" + text);
        }

        public void actionPerformed(ActionEvent e)
        {
            System.out.println("Hello" + name);
        }

        public void setName(String name)
        {
            this.name = name;
        }

        private String name;
    }

}

Second question is, how do I make my form not take up the whole center space. I don’t like the stretch look and was hoping the JTextFields could be just one line long, not a big box. Same thing with the buttons. Any thoughts? 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-15T07:11:35+00:00Added an answer on May 15, 2026 at 7:11 am

    I am not getting any data when I type in something to the firstName JTextField.

    You’re feeding AddressBookManager an empty string. As an expedient, let it see the fields of interest in the parent, AddressBookForm. Addendum: As your application evolves, consider the organizational benefits described in How to Use Actions.

    class AddressBookForm extends JPanel {
        JTextField firstName = new JTextField(20);
        JTextField lastName = new JTextField(20);
        JTextField telephone = new JTextField(20);
        JTextField email = new JTextField(20);
    
        public AddressBookForm() {
        ...
        }
    
        private class Person { ... }
    
        private List<Person> addressList = new ArrayList<Person>();
    
        private class AddressBookManager implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                addressList.add(new Person(...));
                System.out.println("Hello, " + firstName.getText());
            }
    
            public void setName(String name) {
                firstName.setText(name);
            }
        }
    }
    

    How do I make my form not take up the whole center space?

    See Using Layout Managers. GridLayout ignores preferred sizes; I’d consider a nested BoxLayout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 426k
  • Answers 426k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, the problem is not really fast typing, but actually… May 15, 2026 at 12:44 pm
  • Editorial Team
    Editorial Team added an answer In the model holding the attached file there's a method… May 15, 2026 at 12:44 pm
  • Editorial Team
    Editorial Team added an answer Currently in the JOptionPane source code (rather in its UI,… May 15, 2026 at 12:44 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.