Results 1 to 3 of 3

Thread: Arraylist

  1. #1
    MaN-GreaSe's Avatar
    MaN-GreaSe is offline Member
    Join Date
    Apr 2010
    Posts
    11
    Rep Power
    0

    Default Arraylist

    Where would it be safe to create and arraylist?
    Java Code:
    package accountlog;
    
    /**
     *
     * @author RaFiKi
     */
    import java.awt.*; // for Container
    import java.awt.event.*;
    import javax.swing.*;
    
    
    class Accountlog extends JFrame {
      private JTextField firstNameField = new JTextField(10);
      private JTextField surnameField = new JTextField(10);
      private JTextField accountNumberField = new JTextField (10);
      private JPanel dataPanel = new JPanel();
      private UserInputPanel userInputPanel = new UserInputPanel();
      private OptionButtonPanel optionButtonPanel = new OptionButtonPanel();
      private JPanel dobPanel = new JPanel();
      private JPanel outputPanel = new JPanel();
      private JLabel outputLabel = new JLabel("");
      private JComboBox monthChoices;
      private String[] months = { "", "Jan", "Feb", "Mar", "Apr", "May",
                                  "Jun", "Jul", "Aug", "Sep", "Oct",
                                  "Nov", "Dec" };
      private JComboBox dateChoices;
      private String[] dates = { "", "01", "02", "03", "04", "05",
                                 "06", "07", "08", "09", "10",
                                 "11", "12", "13", "14", "15",
                                 "16", "17", "18", "19", "20",
                                 "21", "22", "23", "24", "25",
                                 "26", "27", "28", "29", "30", "31" };
      private JComboBox yearChoices;
      private String[] years = { "", "1992", "1993", "1994", "1995", "1996",
                                 "1997", "1998", "1999", "2000", "2001",
                                 "2002", "2003", "2004", "2005", "2006", "2007" };
      private JComboBox bankChoices;
      private String [] banks = {"", "HSBC", "Loyds TSB", "Barclays", "RSB",
                                    "Natwest" };
      private JTextArea dataArea;
      private JScrollPane scrollPane;
      private int year = 0;
      private int month = 0;
      private int date = 0;
      private String bank = "";
    
      // window properties
      private static final int FRAMEWIDTH = 500;
      private static final int FRAMEHEIGHT = 450;
      private static final int FRAMEX = 350; // screen co-ordinates
      private static final int FRAMEY = 300;
      // PersonV4 object
      PersonV4 person = new PersonV4();
    
    
    
      public Accountlog() {
        Container guiContainer = getContentPane();
        guiContainer.setLayout(new BorderLayout());
        dataPanel.setLayout(new BorderLayout());
        dataPanel.add(userInputPanel, BorderLayout.NORTH);
        dobPanel.add(new JLabel("Date "));
        // instantiate combo box with dates array
        dateChoices = new JComboBox(dates);
        dateChoices.setSelectedIndex(0); // set default item
        // assign listener
        DateComboBoxListener dcbListener = new DateComboBoxListener();
        dateChoices.addItemListener(dcbListener);
        dobPanel.add(dateChoices);
        // process next two combo boxes and their listeners
        dobPanel.add(new JLabel("Month "));
        monthChoices = new JComboBox(months);
        monthChoices.setSelectedIndex(0);
        MonthComboBoxListener mcbListener = new MonthComboBoxListener();
        monthChoices.addItemListener(mcbListener);
        dobPanel.add(monthChoices);
        dobPanel.add(new JLabel("Year "));
        yearChoices = new JComboBox(years);
        yearChoices.setSelectedIndex(0);
        YearComboBoxListener ycbListener = new YearComboBoxListener();
        yearChoices.addItemListener(ycbListener);
        dobPanel.add(yearChoices);
        dataPanel.add(dobPanel, BorderLayout.SOUTH);
        dobPanel.add(new JLabel("Bank "));
        bankChoices = new JComboBox(banks);
        bankChoices.setSelectedIndex(0);
        BankComboBoxListener bcbListener = new BankComboBoxListener();
        bankChoices.addItemListener((ItemListener) bcbListener);
        dobPanel.add(bankChoices);
        // add remaining components
        guiContainer.add(dataPanel, BorderLayout.NORTH);
        guiContainer.add(optionButtonPanel, BorderLayout.CENTER);
        // initialise and assign text area
        dataArea = new JTextArea(10, 20);
        scrollPane = new JScrollPane(dataArea);
        outputPanel.add(scrollPane);
        guiContainer.add(outputPanel, BorderLayout.SOUTH);
      }

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    anywhere. creating ArrayLists is very safe.

    please be more specific - what are you trying to do?

  3. #3
    MaN-GreaSe's Avatar
    MaN-GreaSe is offline Member
    Join Date
    Apr 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by iluxa View Post
    anywhere. creating ArrayLists is very safe.

    please be more specific - what are you trying to do?
    Trying to implement this code
    Java Code:
    private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
    // TODO add your handling code here:
            if(evt.getActionCommand().equals("Save")) {
                updatePerson(); //retrieve contents of window and store in person
                personList.add(person);  //always appending
            }
    Safely into here
    Java Code:
     private class OptionButtonPanel extends JPanel {
          public OptionButtonPanel() {
              setLayout(new FlowLayout());
              OptionButtonListener optionButtonListener =
                      new OptionButtonListener();
              JButton displayButton = new JButton("Show Current");
              add(displayButton);
              displayButton.addActionListener(optionButtonListener);
              JButton saveButton = new JButton ("Save");
              add (saveButton);
              saveButton.addActionListener(optionButtonListener);
              JButton showButton = new JButton ("Show All");
              add(showButton);
              showButton.addActionListener(optionButtonListener);
              JButton clearButton = new JButton ("Clear");
              add (clearButton);
              clearButton.addActionListener(optionButtonListener);
              JButton quitButton = new JButton("Exit");
              add(quitButton);
              quitButton.addActionListener(optionButtonListener);
          }
       }
    
       private class OptionButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
          String command = event.getActionCommand();
           if(command.equals("Show Current")) {
              // assign data extracted from window to person object
              person.setForename(firstNameField.getText());
              person.setSurname(surnameField.getText());
              person.setAccountnumber (accountNumberField.getText());
              person.setDOB(date, month, year);
              // extract data from person object and write to text area
              dataArea.append("Forename: ");
              dataArea.append(person.getForename());
              dataArea.append(" Surname: ");
              dataArea.append(person.getSurname());
              dataArea.append ("\n Account Number: ");
              dataArea.append(person.getAccountnumber());
              dataArea.append("\nAcc. Start Date: ");
              dataArea.append(Integer.toString(person.getDateOfBirth()) + '/' +
                          Integer.toString(person.getMonthOfBirth()) + '/' +
                          Integer.toString(person.getYearOfBirth()));
              dataArea.append("\nAge of Account: " + Integer.toString(person.getAge()));
              dataArea.append ("\nBank: ");
              dataArea.append(bank);
              dataArea.append("\n"); // new line
           }
           if(command.equals("Exit")) {
               int option = JOptionPane.showConfirmDialog(null, "Do you really want to exit?",
                                   "Confirm", JOptionPane.YES_NO_OPTION);
                if(option == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
    
            }
           }
    I need to create the personlist array i was wondering where i would put it.

Similar Threads

  1. Regarding arrayList
    By kishan in forum Advanced Java
    Replies: 7
    Last Post: 08-07-2009, 01:48 PM
  2. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  3. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 09:48 PM
  4. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 11
    Last Post: 12-05-2007, 08:30 PM
  5. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 07:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •