Application Continued (problem)
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.
Code :
[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;
}
}
Re: Application Continued (problem)
mmm... long code :P
I'll tell you how you can do this, but I'll leave the implementation up to you.
Add your class to the ActionListener list of the button, and then when that ActionEvent is triggered, retrieve the text in the fields you want:
Code :
public class address extends JFrame implements ActionListener
{
public address()
{
// have the constructor as you have it, just add this line of code to the end
addButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("ADD"))
{
// extract the info from the text fields you want
}
}
}
If you're using the JTextField object, you can just use the getText() method and get the text. If you're using TextField objects, the job of getting text out is a bit trickier.
Re: Application Continued (problem)
thanks very much, I've created the action listener , then i've tried to get the text using the constructor and put it in the arraylist i created. When i put text into the fields and add i can't see any changes. here is my code:
Code :
buttonnew= new JButton("Add Contact");
panel2.add(buttonnew);
buttonnew.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (event.getActionCommand().equals("ADD"))
records.add(new Record(txtName.getText(), txthomeTel.getText(), txtmobileTel.getText(), txtAddress.getText()));
}
will i need to use bufferedwriter, and if so how do i implement this? im having trouble with this function, doesn't seem to be working. Thanks again :)
Re: Application Continued (problem)
Ah. The getActionCommand() for the JButton class returns the caption that you have the button under. I just put ADD because I thought that's what the button said. You'll need to change it to "Add Contact" (without the quotes). An alternative is to use the getSource() method and see if it's the buttonnew object.
Re: Application Continued (problem)
oh yeah! thanks for that, its saying '.equals() on incompatible types' when i put the name of the button into the getActionCommand() like this:
Code :
if (event.getActionCommand().equals(Add Contact))
have also tried:
if (event.getActionCommand().equals(buttonnew))
thanks
Re: Application Continued (problem)
Oops, my bad. the "Add Contact" needs to be in quotes to denote that it's a string (the getActionCommand() method returns a String, so you must compare it to a String).
If you want to compare with buttonnew (no quotes here), use the getSource() method.
Code :
// use either solution 1 or solution 2
// solution1:
if (event.getActionCommand().equals("Add Contact"))
{
// stuff
}
// solution2:
if (event.getSource().equals(buttonnew))
{
// stuff
}
FYI: the first solution will make expanding your program easier because you may want to have several locations that you can add a new contact(a menu item, a shortcut, etc.). If you have them all pass the same action command, you can have only one if statement to handle them all the same, while by using getSource() you'll have to use multiple if statements/or conditionals to achieve the same goal.
Re: Application Continued (problem)
thankyou very much, i can now create a contact putting them into the arraylist when the "Add Contact" button is pressed, using the 1st solution :)
one more quick question:
when i delete a contact from the arraylist, i am using the following, basically im adding an actionlistener to the button, and saying get the selected text from the arraylist using the get method. Clearing the fields and then removing each line from the arraylist. However i think the last part may be wrong, as it will not know what position to delete from in the arraylist, when i run the program if i go next & previous the details appear again. how would i specify this? thankyou.
Code :
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);
}
}