Hey, i am having a problem with a function in my program to add new contacts to an existing array list i have created (name Record). Basically i am unsure how to enter a persons name,phonenum,mobnum and address into textfields of a GUI then press an ADD button which should store the contact in the array list. I am using a seprate class containg get and set methods, however i am unsure how i would implement these into my actionlistener & if i would need to create an actionlistener on each textfield?. Here is my code so far:
any help would be greatly appreciated, thanks.
[B]public class address extends JFrame[/B] { JLabel lblhomeTel,lblmobileTel,lblName,lblAddress; JTextField txtAddress,txthomeTel, txtmobileTel,txtName; JPanel panel,panel2,panel3; JFrame frame; JButton buttonnew,buttondelete,buttonedit,buttonnext,buttonprevious, buttonFind; JMenu menuList; String fileInput,readline; ArrayList<String> arrayOfFile = new ArrayList<String>(); ArrayList<Record> records = new ArrayList<Record>(); int index = 0; public static void main(String[] argStrings) { address GUI= new address(); GUI.address(); } public void address () { panel = new JPanel(); panel.setLayout(new GridLayout(2,1)); panel.setBackground(Color.LIGHT_GRAY); panel2 = new JPanel(); panel2.setBackground(Color.LIGHT_GRAY); panel2.setLayout(new GridLayout(3,4,3,1)); panel3= new JPanel(); panel3.setBackground(Color.LIGHT_GRAY); panel3.setLayout(new GridLayout(6,1)); frame=new JFrame(); frame.setTitle("Address Book"); frame.setSize(350,350); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); JMenu menuList= new JMenu("File"); menuBar.add(menuList); JMenuItem importFile=new JMenuItem("Import Addresses"); menuList.add(importFile); importFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { try { BufferedReader fileStream = new BufferedReader(new FileReader("src/contacts.buab")); while (true) { String fileInput = fileStream.readLine(); if(fileInput==null) break; Record a = new Record(); a.setname(fileInput); a.setphone(fileStream.readLine()); a.setmphone(fileStream.readLine()); a.setaddress(fileStream.readLine()); records.add(a); System.out.println(a.getname()); }fileStream.close(); }catch (Exception ex) { JOptionPane.showMessageDialog(rootPane, ex); }displaycontact(); }}); JMenuItem exportFile=new JMenuItem("Export Addresses to file"); menuList.add(exportFile); JMenuItem exitProg= new JMenuItem("Exit Program"); menuList.add(exitProg); exitProg.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); lblName= new JLabel("Name:"); panel3.add(lblName); txtName= new JTextField(""); txtName.setText(""); panel3.add(txtName); lblhomeTel= new JLabel("Home Telephone:"); panel3.add(lblhomeTel); txthomeTel= new JTextField(""); txthomeTel.setText(""); panel3.add(txthomeTel); lblmobileTel = new JLabel("Mobile Telephone:"); panel3.add(lblmobileTel); txtmobileTel = new JTextField(""); txtmobileTel.setText(""); panel3.add(txtmobileTel); lblAddress= new JLabel("Address:"); panel3.add(lblAddress); txtAddress= new JTextField(""); txtAddress.setText(""); panel3.add(txtAddress); buttonnext= new JButton(">>"); panel2.add(buttonnext); buttonnext.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { index ++; displaycontact(); } }); buttonprevious= new JButton("<<"); panel2.add(buttonprevious); buttonprevious.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { index--; displaycontact(); }}); buttonnew= new JButton("Add Contact"); panel2.add(buttonnew); buttonnew.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { }}); buttonFind= new JButton("Search Contact"); panel2.add(buttonFind); buttonedit = new JButton("Edit Contact"); panel2.add(buttonedit); buttondelete= new JButton("Delete Record"); panel2.add(buttondelete); buttondelete.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { String temp=txtName.getText(); for(Record Record:records) { if(temp.equals(Record.getname())); { txtName.setText(""); txthomeTel.setText(""); txtmobileTel.setText(""); txtAddress.setText(""); records.remove(Record); records.remove(Record); records.remove(Record); records.remove(Record); } } } }); frame.setJMenuBar(menuBar); panel.add(panel3); panel.add(panel2); frame.add(panel); frame.setVisible(true); } public void displaycontact() { txtName.setText(records.get(index).name); txthomeTel.setText(records.get(index).phone); txtmobileTel.setText(records.get(index).mphone); txtAddress.setText(records.get(index).address); } } [B]public class Record[/B]{ static void add(String text) { throw new UnsupportedOperationException("Not Done Yet"); } public String name; public String phone; public String mphone; public String address; public Record(){} public Record(String name, String phone, String mphone, String address) { this.name = name; this.phone = phone; this.mphone = mphone; this.address = address; } public String getname() { return this.name; } public String getphone() { return this.phone; } public String getmphone() { return this.mphone; } public String getaddress() { return this.address; } public void setname(String name) { this.name = name; } public void setphone(String phone) { this.phone = phone; } public void setmphone(String mphone) { this.mphone = mphone; } public void setaddress(String address) { this.address = address; } }