Passing ActionListeners in Java, pack()
Posted
by Crystal
on Stack Overflow
See other posts from Stack Overflow
or by Crystal
Published on 2010-06-07T04:45:59Z
Indexed on
2010/06/07
4:52 UTC
Read the original article
Hit count: 210
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.
© Stack Overflow or respective owner