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

    Default Store and Retrieve Problem.

    I've written a program and i need to implement some features.
    These features are on another program that i was given as an example.I've attached the program into this thread.
    In the program attached there's a save,clear,showall, and showcurrent buttons. I'd like to have the same feature. I've already made the buttons for my program but i cant think of the code to make them do what they do on IDEWindow03. I know i have to make an arraylist to store the data but i cant figure out where to make this. Here's my code for my program.
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    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);
      }
    
    
      private class UserInputPanel extends JPanel {
          public UserInputPanel() {
            setLayout(new GridLayout(3, 2, 15, 15)); //horizontal/vertical distances
            JLabel label = new JLabel("First Name", JLabel.RIGHT); //R1/C1
            add(label);
            add(firstNameField);  //R1/C2
            label = new JLabel("Surname", JLabel.RIGHT);  //R2/C1
            add(label);
            add(surnameField);  //R2/C2
            label = new JLabel ("Account Number", JLabel.RIGHT);
            add(label);
            add (accountNumberField); // R2/C2
    
    
          }
        }
    
    
    
      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);
                }
    
            }
           }
        }
      private class MonthComboBoxListener implements ItemListener {
          public void itemStateChanged(ItemEvent event) {
           if(event.getStateChange() == ItemEvent.SELECTED) {
               int comboIndex = monthChoices.getSelectedIndex();
               month = translateMonth(months[comboIndex]);
            }
          }
    
          int translateMonth(String comboMonth) {
              int month = 0;
              if(comboMonth.equals("Jan"))
                  month = 1;
              else if(comboMonth.equals("Feb"))
                  month = 2;
              else if(comboMonth.equals("Mar"))
                  month = 3;
              else if(comboMonth.equals("Apr"))
                  month = 4;
              else if(comboMonth.equals("May"))
                  month = 5;
              else if(comboMonth.equals("Jun"))
                  month = 6;
              else if(comboMonth.equals("Jul"))
                  month = 7;
              else if(comboMonth.equals("Aug"))
                  month = 8;
              else if(comboMonth.equals("Sep"))
                  month = 9;
              else if(comboMonth.equals("Oct"))
                  month = 10;
              else if(comboMonth.equals("Nov"))
                  month = 11;
              else if(comboMonth.equals("Dec"))
                  month = 12;
              return month;
          }
      }
    
      private class DateComboBoxListener implements ItemListener {
          public void itemStateChanged(ItemEvent event) {
           if(event.getStateChange() == ItemEvent.SELECTED) {
               int comboIndex = dateChoices.getSelectedIndex();
               date = Integer.parseInt(dates[comboIndex]);
            }
          }
      }
    
      private class YearComboBoxListener implements ItemListener {
          public void itemStateChanged(ItemEvent event) {
           if(event.getStateChange() == ItemEvent.SELECTED) {
               int comboIndex = yearChoices.getSelectedIndex();
               year = Integer.parseInt(years[comboIndex]);
            }
          }
      }
     private class BankComboBoxListener implements ItemListener {
          public void itemStateChanged(ItemEvent event) {
           if(event.getStateChange() == ItemEvent.SELECTED) {
               int comboIndex = bankChoices.getSelectedIndex();
               bank = (banks[comboIndex]);
           }
          }
     }
      public static void main(String[] args)  throws Exception
      {
        Accountlog testWindow = new Accountlog();
        testWindow.pack();
        testWindow.setVisible(true);
        testWindow.setSize(FRAMEWIDTH, FRAMEHEIGHT);
        testWindow.setLocation(FRAMEX, FRAMEY);
        testWindow.setTitle("Account Manager");
        testWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    }
    PersonV4.
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package accountlog;
    
    /**
     *
     * @author RaFiKi
     */
    import java.util.*;
    import java.io.Serializable;
    
    public class PersonV4 implements Serializable{ //so objects can be written to file
        String forename = null;
        String surname = null;
        String bankname = "";
        String accountnumber = "";
        int age = 0;
        GregorianCalendar calendar = new GregorianCalendar();
    
        /** Creates a new instance of Person */
        public PersonV4() {
        }
    
        public PersonV4(String fnm, String snm,String gen, int date, int mnth, 
                int yr, String bnk, String acn) {
            forename = fnm;
            surname = snm;
            bankname = bnk;
            accountnumber = acn;
    
       
            setDOB(date, mnth, yr);
        }
    
        void setForename(String fnm){
            forename = fnm;
        }
    
        void setSurname(String snm){
            surname = snm;
        }
    
         void setBankname(String bnk){
            bankname = bnk;
        }
     void setAccountnumber(String acn){
            accountnumber = acn;
        }
    
        
    
        private void setAge(){
            GregorianCalendar currentDate = new GregorianCalendar();
            Date date = new Date(); //set to today's date
            currentDate.setTime(date);
            age = currentDate.get(currentDate.YEAR) - calendar.get(calendar.YEAR);
        }
    
        void setDOB(int date, int month, int year){
            calendar.set(year, --month, date);//MONTH is 0 - 11
            setAge(); //call private class
        }
    
        String getForename(){
            return forename;
        }
    
        String getSurname(){
            return surname;
        }
      String getBankname(){
            return bankname;
        }
      String getAccountnumber(){
            return accountnumber;
        }
    
    
        
    
        int getAge(){
            return(age);
        }
    
        int getYearOfBirth(){
            return(calendar.get(calendar.YEAR));
        }
    
        int getMonthOfBirth(){
            return(calendar.get(calendar.MONTH) + 1); //so user operates with 1 - 12
        }
    
        int getDateOfBirth(){
            return(calendar.get(calendar.DATE));
        }
    }
    Here's the code for IDEWindow03 program.
    Java Code:
    /*
     * IDEWindow03.java
     *
     * Created on 12 February 2008, 13:03
     */
    
    import javax.swing.*;
    import java.util.*; // for ArrayList
    
    /**
     * IDEWindow03.java replicates Interactive Window04 but the interface is
     * generated by NetBeans rather than being written entirely in java code. This
     * version also lets the user save data to an ArrayList whose contents
     * can also be retrieved and output.
     *
     * @author  jkerins
     */
    public class IDEWindow03 extends javax.swing.JFrame {
        PersonV4 person; // = new PersonV4();
        String genderString = "";
        int date = 0;
        int month = 0;
        int year = 0;
        ArrayList personList = new ArrayList(); // to store data
    
    
        /**
         * Creates new form IDEWindow03
         */
        public IDEWindow03() {
            initComponents();
            setTitle("IDE Window03");
        }
    
        // instantiate person object with data retrieved from window components
        private void updatePerson() {
            person = new PersonV4();
            person.setForename(firstNameField.getText());
            person.setSurname(surnameField.getText());
            person.setGender(genderString);
            person.setDOB(date, month, year);
        }
    
        // output contents of person object to textArea
        private void outputPerson(PersonV4 person) {
          textArea.append("Forename: ");
          textArea.append(person.getForename());
          textArea.append(" Surname: ");
          textArea.append(person.getSurname());
          textArea.append("\nDate of Birth: ");
          textArea.append(Integer.toString(person.getDateOfBirth()) + '/' +
                      Integer.toString(person.getMonthOfBirth()) + '/' +
                      Integer.toString(person.getYearOfBirth()));
          textArea.append("\nApproximate Age: " + Integer.toString(person.getAge()));
          textArea.append("\nGender : " + person.getGender());
          textArea.append("\n"); // new line
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
        private void initComponents() {
            buttonGroup1 = new javax.swing.ButtonGroup();
            jLabel1 = new javax.swing.JLabel();
            firstNameField = new javax.swing.JTextField();
            jLabel2 = new javax.swing.JLabel();
            surnameField = new javax.swing.JTextField();
            jScrollPane1 = new javax.swing.JScrollPane();
            textArea = new javax.swing.JTextArea();
            optionButtonPanel = new javax.swing.JPanel();
            exit = new javax.swing.JButton();
            showAll = new javax.swing.JButton();
            showCurrent = new javax.swing.JButton();
            save = new javax.swing.JButton();
            genderPanel = new javax.swing.JPanel();
            male = new javax.swing.JRadioButton();
            female = new javax.swing.JRadioButton();
            dobPanel = new javax.swing.JPanel();
            yearChoices = new javax.swing.JComboBox();
            jLabel5 = new javax.swing.JLabel();
            monthChoices = new javax.swing.JComboBox();
            jLabel4 = new javax.swing.JLabel();
            dateChoices = new javax.swing.JComboBox();
            jLabel3 = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            jLabel1.setText("First Name");
    
            jLabel2.setText("Surname");
    
            textArea.setColumns(20);
            textArea.setRows(5);
            jScrollPane1.setViewportView(textArea);
    
            optionButtonPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
            exit.setText("Exit");
            exit.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    exitActionPerformed(evt);
                }
            });
    
            showAll.setText("Show All");
            showAll.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    showAllActionPerformed(evt);
                }
            });
    
            showCurrent.setText("Show Current");
            showCurrent.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    showCurrentActionPerformed(evt);
                }
            });
    
            save.setText("Save");
            save.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    saveActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout optionButtonPanelLayout = new javax.swing.GroupLayout(optionButtonPanel);
            optionButtonPanel.setLayout(optionButtonPanelLayout);
            optionButtonPanelLayout.setHorizontalGroup(
                optionButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, optionButtonPanelLayout.createSequentialGroup()
                    .addContainerGap(59, Short.MAX_VALUE)
                    .addComponent(save)
                    .addGap(45, 45, 45)
                    .addComponent(showCurrent)
                    .addGap(55, 55, 55)
                    .addComponent(showAll)
                    .addGap(63, 63, 63)
                    .addComponent(exit)
                    .addGap(23, 23, 23))
            );
            optionButtonPanelLayout.setVerticalGroup(
                optionButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, optionButtonPanelLayout.createSequentialGroup()
                    .addContainerGap(21, Short.MAX_VALUE)
                    .addGroup(optionButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(exit)
                        .addComponent(showAll)
                        .addComponent(showCurrent)
                        .addComponent(save))
                    .addContainerGap())
            );
    
            genderPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Gender"));
            buttonGroup1.add(male);
            male.setText("Male");
            male.setBorder(javax.swing.BorderFactory.createTitledBorder("Gender"));
            male.setMargin(new java.awt.Insets(0, 0, 0, 0));
            male.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    maleActionPerformed(evt);
                }
            });
    
            buttonGroup1.add(female);
            female.setText("Female");
            female.setBorder(javax.swing.BorderFactory.createTitledBorder("Gender"));
            female.setMargin(new java.awt.Insets(0, 0, 0, 0));
            female.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    femaleActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout genderPanelLayout = new javax.swing.GroupLayout(genderPanel);
            genderPanel.setLayout(genderPanelLayout);
            genderPanelLayout.setHorizontalGroup(
                genderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(genderPanelLayout.createSequentialGroup()
                    .addGap(54, 54, 54)
                    .addComponent(female, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(24, 24, 24)
                    .addComponent(male, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(37, Short.MAX_VALUE))
            );
            genderPanelLayout.setVerticalGroup(
                genderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(genderPanelLayout.createSequentialGroup()
                    .addGroup(genderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(female, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(male, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            dobPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Date of Birth"));
            yearChoices.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2006", "2007", "2008" }));
            yearChoices.addItemListener(new java.awt.event.ItemListener() {
                public void itemStateChanged(java.awt.event.ItemEvent evt) {
                    yearChoicesItemStateChanged(evt);
                }
            });
    
            jLabel5.setText("Year");
    
            monthChoices.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec" }));
            monthChoices.addItemListener(new java.awt.event.ItemListener() {
                public void itemStateChanged(java.awt.event.ItemEvent evt) {
                    monthChoicesItemStateChanged(evt);
                }
            });
    
            jLabel4.setText("Month");
    
            dateChoices.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "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" }));
            dateChoices.addItemListener(new java.awt.event.ItemListener() {
                public void itemStateChanged(java.awt.event.ItemEvent evt) {
                    dateChoicesItemStateChanged(evt);
                }
            });
            dateChoices.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                public void propertyChange(java.beans.PropertyChangeEvent evt) {
                    dateChoicesPropertyChange(evt);
                }
            });
    
            jLabel3.setText("Date");
    
            javax.swing.GroupLayout dobPanelLayout = new javax.swing.GroupLayout(dobPanel);
            dobPanel.setLayout(dobPanelLayout);
            dobPanelLayout.setHorizontalGroup(
                dobPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, dobPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                    .addComponent(dateChoices, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(21, 21, 21)
                    .addComponent(jLabel4)
                    .addGap(18, 18, 18)
                    .addComponent(monthChoices, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(12, 12, 12)
                    .addComponent(jLabel5)
                    .addGap(17, 17, 17)
                    .addComponent(yearChoices, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
            dobPanelLayout.setVerticalGroup(
                dobPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, dobPanelLayout.createSequentialGroup()
                    .addGroup(dobPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(yearChoices, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5)
                        .addComponent(monthChoices, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4)
                        .addComponent(dateChoices, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))
                    .addContainerGap(39, Short.MAX_VALUE))
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(330, Short.MAX_VALUE)
                    .addComponent(dobPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
                .addGroup(layout.createSequentialGroup()
                    .addGap(83, 83, 83)
                    .addComponent(optionButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(71, 71, 71))
                .addGroup(layout.createSequentialGroup()
                    .addGap(191, 191, 191)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(190, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(genderPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(439, 439, 439)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(surnameField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                                .addComponent(firstNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE))))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(23, 23, 23)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(firstNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(28, 28, 28)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(surnameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(30, 30, 30)
                    .addComponent(genderPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(30, 30, 30)
                    .addComponent(dobPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(25, 25, 25)
                    .addComponent(optionButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(31, 31, 31)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            pack();
        }// </editor-fold>//GEN-END:initComponents
    
        private void showAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showAllActionPerformed
    // TODO add your handling code here:
            if(evt.getActionCommand().equals("Show All"))
                textArea.setText(""); //clear window contents
                for(int i = 0; i < personList.size(); i++){
                  PersonV4 person = (PersonV4) personList.get(i);
                  textArea.append("#" + (i+1) + " ");
                  outputPerson(person); //locally-defined person
                  // or, write as follows:
                  //outputPerson((PersonV4) personList.get(i));
                }
        }//GEN-LAST:event_showAllActionPerformed
    
        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
            }
        }//GEN-LAST:event_saveActionPerformed
    
        private void femaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_femaleActionPerformed
    // TODO add your handling code here:
            if(evt.getActionCommand().equals("Female"))
                genderString = "female";
        }//GEN-LAST:event_femaleActionPerformed
    
        private void maleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maleActionPerformed
    // TODO add your handling code here:
            if(evt.getActionCommand().equals("Male"))
                genderString = "male";
        }//GEN-LAST:event_maleActionPerformed
    
        private void yearChoicesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_yearChoicesItemStateChanged
    // TODO add your handling code here:
            if(evt.getStateChange() == evt.SELECTED) {
               int comboIndex = yearChoices.getSelectedIndex();
               String s = (String) yearChoices.getItemAt(comboIndex);
               year = Integer.parseInt(s);
               // or, could write command as follows:
               //year = Integer.parseInt((String) yearChoices.getItemAt(yearChoices.getSelectedIndex()));
            }
        }//GEN-LAST:event_yearChoicesItemStateChanged
    
        private void monthChoicesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_monthChoicesItemStateChanged
    // TODO add your handling code here:
             if(evt.getStateChange() == evt.SELECTED) {
               int comboIndex = monthChoices.getSelectedIndex();
               month = translateMonth((String) monthChoices.getItemAt(comboIndex));
            }
        }//GEN-LAST:event_monthChoicesItemStateChanged
    
        int translateMonth(String comboMonth) {
              int month = 0;
              if(comboMonth.equals("Jan"))
                  month = 1;
              else if(comboMonth.equals("Feb"))
                  month = 2;
              else if(comboMonth.equals("Mar"))
                  month = 3;
              else if(comboMonth.equals("Apr"))
                  month = 4;
              else if(comboMonth.equals("May"))
                  month = 5;
              else if(comboMonth.equals("Jun"))
                  month = 6;
              else if(comboMonth.equals("Jul"))
                  month = 7;
              else if(comboMonth.equals("Aug"))
                  month = 8;
              else if(comboMonth.equals("Sep"))
                  month = 9;
              else if(comboMonth.equals("Oct"))
                  month = 10;
              else if(comboMonth.equals("Nov"))
                  month = 11;
              else if(comboMonth.equals("Dec"))
                  month = 12;
              return month;
          }
    
        private void dateChoicesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_dateChoicesItemStateChanged
    // TODO add your handling code here:
            if(evt.getStateChange() == evt.SELECTED) {
               // could do it this way:
               //int comboIndex = dateChoices.getSelectedIndex();
               //String s = (String) dateChoices.getItemAt(comboIndex);
               //date = Integer.parseInt(s);
               // or this:
               date = Integer.parseInt((String) dateChoices.getItemAt(dateChoices.getSelectedIndex()));
            }
        }//GEN-LAST:event_dateChoicesItemStateChanged
    
    
        private void dateChoicesPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dateChoicesPropertyChange
    // TODO add your handling code here
        }//GEN-LAST:event_dateChoicesPropertyChange
    
        private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
    // TODO add your handling code here:
            String exitChoice = evt.getActionCommand();
            if(exitChoice.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);
                }
           }
        }//GEN-LAST:event_exitActionPerformed
    
        private void showCurrentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showCurrentActionPerformed
    // TODO add your handling code here:
            if(evt.getActionCommand().equals("Show Current")) {
              textArea.setText(""); // clear window
              // assign data extracted from window to person object
              updatePerson(); //class attribute person
              outputPerson(person); //class attribute person
           }
        }//GEN-LAST:event_showCurrentActionPerformed
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new IDEWindow03().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.ButtonGroup buttonGroup1;
        private javax.swing.JComboBox dateChoices;
        private javax.swing.JPanel dobPanel;
        private javax.swing.JButton exit;
        private javax.swing.JRadioButton female;
        private javax.swing.JTextField firstNameField;
        private javax.swing.JPanel genderPanel;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JRadioButton male;
        private javax.swing.JComboBox monthChoices;
        private javax.swing.JPanel optionButtonPanel;
        private javax.swing.JButton save;
        private javax.swing.JButton showAll;
        private javax.swing.JButton showCurrent;
        private javax.swing.JTextField surnameField;
        private javax.swing.JTextArea textArea;
        private javax.swing.JComboBox yearChoices;
        // End of variables declaration//GEN-END:variables
    
    }
    Is it possible to replicate the IDEWindow03 code to my program?
    thanks in advance.
    Attached Files Attached Files

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    tl;dr

    kind regards,

    Jos

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

    Default

    Anyone else?

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

    Default

    In short i'm trying to Implement this code from IDEWindow03.
    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
            }
    Into my Program, I'm guessing it would go somewhere 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've already created the save button i just need it to "save". How would i do this?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Why not put the following code fragment:

    Java Code:
     if(evt.getActionCommand().equals("Save")) {
                updatePerson(); //retrieve contents of window and store in person
                personList.add(person);  //always appending
            }
    ... in the actionPerformed( ... ) method of your OptionButtonListener class?
    Unless I'm missing something completely that should do it.

    kind regards,

    JOs

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

    Default

    Quote Originally Posted by JosAH View Post
    Why not put the following code fragment:

    Java Code:
     if(evt.getActionCommand().equals("Save")) {
                updatePerson(); //retrieve contents of window and store in person
                personList.add(person);  //always appending
            }
    ... in the actionPerformed( ... ) method of your OptionButtonListener class?
    Unless I'm missing something completely that should do it.

    kind regards,

    JOs
    I tried that it gives me an error on personlist
    and updateperson. I understand i need to create an arraylist first to identify personlist? where would i put this?
    Because i copied the array list from the IDEwinow code but that didnt work it gave me an error too.
    Last edited by MaN-GreaSe; 04-23-2010 at 10:00 PM.

Similar Threads

  1. JSP code to connect, store and retrieve from database
    By HarryJade in forum New To Java
    Replies: 1
    Last Post: 12-26-2009, 03:45 AM
  2. problem with store
    By dj kourampies in forum New To Java
    Replies: 5
    Last Post: 08-23-2009, 06:55 PM
  3. speeder way to store and retrieve data
    By Ms.Ranjan in forum New To Java
    Replies: 3
    Last Post: 04-21-2009, 05:54 PM
  4. How to store/retrieve PNG image in/from RMS
    By jason-nexFIT-mobileXware in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-10-2008, 04:01 AM
  5. Store retrieve and delete
    By on7june in forum New To Java
    Replies: 1
    Last Post: 03-08-2008, 05:57 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
  •