public AddressBookApp(){
frame = new JFrame("Address Book");
frame.setSize(500, 400);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setBackground(Color.gray);
panel.setLayout(null);
frame.add(panel);
frame.setVisible(true);
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem insert = new JMenuItem("Import");
file.add(insert);
insert.addActionListener(this);
JMenuItem export = new JMenuItem("Export");
file.add(export);
export.addActionListener(this);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
exit.addActionListener(this);
Font f = new Font("Helvetica", Font.BOLD, 10);
btnadd = new JButton("Add");
btnadd.setFont(f);
btnadd.setBounds(200, 250, 80, 20);
panel.add(btnadd);
btnprev = new JButton("Previous");
btnprev.setBounds(40, 250, 80, 20);
btnprev.setFont(f);
btnprev.addActionListener(this);
panel.add(btnprev);
btnnxt = new JButton("Next");
btnnxt.setBounds(120, 250, 80, 20);
btnnxt.setFont(f);
btnnxt.addActionListener(this);
panel.add(btnnxt);
btndel = new JButton("Delete");
btndel.setBounds(280, 250, 80, 20);
btndel.setFont(f);
panel.add(btndel);
btnclear = new JButton("Clear");
btnclear.setBounds(360, 250, 80, 20);
btnclear.setFont(f);
btnclear.addActionListener(this);
panel.add(btnclear);
txtname = new JTextField("");
txtname.setBounds(210, 40, 160, 20);
txtname.setFont(f);
panel.add(txtname);
txtnum = new JTextField("");
txtnum.setBounds(210, 70, 160, 20);
txtnum.setFont(f);
panel.add(txtnum);
txtmob = new JTextField("");
txtmob.setBounds(210, 100, 160, 20);
txtmob.setFont(f);
panel.add(txtmob);
txtadd1 = new JTextField("");
txtadd1.setBounds(210, 130, 160, 20);
txtadd1.setFont(f);
panel.add(txtadd1);
lblname = new JLabel("Name");
lblname.setBounds(160, 40, 160, 20);
lblname.setFont(f);
panel.add(lblname);
lblnum = new JLabel("Number");
lblnum.setBounds(160, 70, 160, 20);
lblnum.setFont(f);
panel.add(lblnum);
lblmob = new JLabel("Mobile");
lblmob.setBounds(160, 100, 160, 20);
lblmob.setFont(f);
panel.add(lblmob);
lbladd1 = new JLabel("Address ");
lbladd1.setBounds(160, 130, 160, 20);
lbladd1.setFont(f);
panel.add(lbladd1);
}
public static void main(String[] args)
{
AddressBookApp ab = new AddressBookApp();
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Exit"))
System.exit(0);
else if (e.getActionCommand().equals("Import"))
{
importContacts();
}
else if (e.getActionCommand().equals("Export"));
{
exportContacts();
}
if (e.getSource() == btnnxt)
{
nextContact();
}
else if (e.getSource() == btnprev)
{
prevContact();
}
}
public void importContacts()
{
try{
BufferedReader fileSize = new BufferedReader(new FileReader("../files/example.buab"));
BufferedReader importContacts = new BufferedReader(new FileReader("../files/example.buab"));
int i = 0;
String contacts;
while (( fileSize.readLine()) !=null)
{
details.add(importContacts.readLine());
i++;
}
fileSize.close();
int x = 0;
int y = 0;
for (x = 0, y = 0; x < details.size(); x++, y++)
{
if (y == 4)
{
y = 0;
}
if (y == 0)
{
name.add(details.get(x));
}
if (y == 1)
{
phone.add(details.get(x));
}
if (y == 2)
{
mobile.add(details.get(x));
}
if (y == 3)
{
address.add(details.get(x));
}
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
txtname.setText(name.get(0));
txtnum.setText(phone.get(0));
txtmob.setText(mobile.get(0));
txtadd1.setText(address.get(0));
}
public void exportContacts()
{
FileOutputStream file;
PrintStream out;
try { file = new FileOutputStream("../files/example.buab", true);
out = new PrintStream(file);
out.println(txtname.getText());
out.println(txtnum.getText());
out.println(txtmob.getText());
out.println(txtadd1.getText());
System.err.println ("");
out.close();
}
catch (Exception e)
{
System.err.println ("Error in writing to file");
}
}
public void nextContact()
{
if(index < details.size() - 1)
{
index++;
txtname.setText(name.get(index));
txtnum.setText(phone.get(index));
txtmob.setText(mobile.get(index));
txtadd1.setText(address.get(index));
}
importContacts();
}
public void prevContact()
{
if (index > 0)
{
index--;
txtname.setText(name.get(index));
txtnum.setText(phone.get(index));
txtmob.setText(mobile.get(index));
txtadd1.setText(address.get(index));
}
importContacts();
}
}