Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package arrayofobjects;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JTextArea;
- /**
- *
- * @author jwporter3
- */
- public class MainArrayList extends JFrame {
- static final int MAX_NAMES = 5;
- JTextArea namesArea; // where a list of names is displayed
- JButton newNameButton; // button to create a new AName object
- ArrayList alNames; // collection of AName objects
- public MainArrayList() {
- createUserInterface();
- alNames = new ArrayList();
- }
- private void createUserInterface() {
- // get content pane and set layout to null
- Container contentPane = getContentPane();
- contentPane.setLayout(null);
- newNameButton = new JButton("New Name");
- newNameButton.setBounds(10, 10, 100, 20);
- contentPane.add(newNameButton);
- newNameButton.addActionListener(
- new ActionListener() // anonymous inner class
- {
- // event handler called when viewJButton is pressed
- public void actionPerformed(ActionEvent event) {
- newNameAction();
- }
- } // end anonymous inner class
- ); // end call to addActionListener
- namesArea = new JTextArea();
- namesArea.setBounds(10, 50, 200, 200);
- contentPane.add(namesArea);
- setTitle("Name App"); // set title bar string
- setSize(250, 300); // set window size
- setVisible(true); // display window
- }
- private void newNameAction() {
- String name = JOptionPane.showInputDialog("Please input a name");
- AName newName = new AName(name);
- alNames.add(newName);
- // pull names from ArrayList into a fixed array for easy iteration
- AName[] newNames = new AName[alNames.size()];
- alNames.toArray(newNames);
- // clear text area
- namesArea.setText("");
- // display all the AName names in the text area
- for (AName an : newNames) {
- namesArea.append(an.getName() + "\n");
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- MainArrayList mainClass = new MainArrayList();
- mainClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement