Code:
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
public class addressbook extends contact
{
public JFrame frame;
public JButton btnadd, btndelete, btnsearch, btnprev, btnnext;
public JPanel panel;
public JTextField txtname, txtaddress, txthomeno, txtmobno;
public JLabel JlbName , JlbHtn, JlbMtn, JlbAddress;
String storeName, storeAddress, storeHomePhone, storeMobilePhone;
int index = 0;
ArrayList<contact> contactList = new ArrayList<contact>();
ArrayList<String> importingfile = new ArrayList<String>();
ArrayList<String> i = new ArrayList<String>();
public addressbook() {
//sets window
frame = new JFrame();
frame.setTitle("Address Book");
frame.setSize(800, 640);
//sets up panel
panel = new JPanel();
panel.setLayout(null);
frame.getContentPane().add(panel);
//Menu Bar
JMenuBar mb = new JMenuBar();
frame.setJMenuBar(mb);
JMenu file = new JMenu("File");
mb.add(file);
//Adds Exit to file menu bar and exits when clicked.
JMenuItem ReadFile = new JMenuItem("Read File");
file.add(ReadFile);
ReadFile.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
readContacts();
displayContact();
}
});
JMenuItem Clear = new JMenuItem("Clear Screen");
file.add(Clear);
Clear.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
clearfield();
}
});
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
exit.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
JMenu insert = new JMenu("Insert");
mb.add(insert);
JMenuItem imp = new JMenuItem("Import");
insert.add(imp);
imp.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
//chooses file to open
JFileChooser fileopen = new JFileChooser();
int ret = fileopen.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
//File file = fileopen.getSelectedFile();
//for (String file : importingfile)
{
System.out.println("" + importingfile);
}
}
}
});
JMenuItem exp = new JMenuItem("Export");
insert.add(exp);
exp.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
JFileChooser dialog = new JFileChooser();
dialog.setCurrentDirectory(new File("."));
dialog.setMultiSelectionEnabled(false);
dialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
dialog.showSaveDialog(frame);
FileOutputStream fileoutput = null;
FileInputStream fileinput = null;
String filename=null;
int temp;
try
{
filename = dialog.getSelectedFile().getPath();
}
catch(Exception e)
{
}
try
{
fileoutput = new FileOutputStream(filename +
"address.txt");
}
catch(Exception e)
{
}
try
{
fileinput = new FileInputStream("address.txt");
}
catch(Exception e)
{
}
try
{
do
{ temp = fileinput.read();
if(temp!=-1)
fileoutput.write(temp);
}while(temp!=-1);
}
catch(Exception e)
{
}
}
});
//Labels
JlbName = new JLabel("Name:");
JlbName.setBounds(10, 50, 100, 20);
panel.add(JlbName);
JlbHtn = new JLabel("Home Number:");
JlbHtn.setBounds(10, 90, 150, 20);
panel.add(JlbHtn);
JlbMtn = new JLabel("Mobile Number:");
JlbMtn.setBounds(10, 130, 200, 20);
panel.add(JlbMtn);
JlbAddress = new JLabel("Address:");
JlbAddress.setBounds(10, 170, 250, 20);
panel.add(JlbAddress);
//Text Fields
txtname = new JTextField("");
txtname.setBounds(120, 50, 200, 20);
panel.add(txtname);
txthomeno = new JTextField("");
txthomeno.setBounds(120, 90, 200, 20);
panel.add(txthomeno);
txtmobno = new JTextField("");
txtmobno.setBounds(120, 130, 200, 20);
panel.add(txtmobno);
txtaddress = new JTextField("");
txtaddress.setBounds(120, 170, 250, 20);
panel.add(txtaddress);
//Buttons && Button Functions
btnadd = new JButton("Add");
btnadd.setBounds(10, 250, 65, 50);
btnadd.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
addContacts();
}
});
panel.add(btnadd);
btndelete = new JButton("Delete");
btndelete.setBounds(80, 250, 65, 50);
btndelete.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
deletecontact();
}
});
panel.add(btndelete);
btnsearch = new JButton("Search");
btnsearch.setBounds(150, 250, 65, 50);
btnsearch.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
searchContacts();
}});
panel.add(btnsearch);
btnprev = new JButton("Prev");
btnprev.setBounds(60, 400, 65, 50);
btnprev.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
index--;
readContacts();
displayContact();
}});
panel.add(btnprev);
btnnext = new JButton("Next");
btnnext.setBounds(60, 450, 65, 50);
btnnext.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
index++;
readContacts();
displayContact();
}});
panel.add(btnnext);
frame.setVisible(true);
panel.setVisible(true);
}
public void displayContact()
{
contact c = contactList.get(index);
txtname.setText (c.getName());
txtaddress.setText (c.getAddress());
txthomeno.setText(c.getTelephone());
txtmobno.setText(c.getMobile());
}
public void clearfield()
{
txtname.setText("");
txtaddress.setText("");
txthomeno.setText("");
txtmobno.setText("");
}
public void deletecontact()
{
{
FileOutputStream fileoutput = null;
contactList.remove(index);
System.out.println(contactList);
txtname.setText("");
txtaddress.setText("");
txthomeno.setText("");
txtmobno.setText("");
try {
fileoutput = new FileOutputStream("address1.buab");
} catch (FileNotFoundException ex)
{
Logger.getLogger(addressbook.class.getName()).log(Level.SEVERE, null, ex);
}
for (contact c : contactList)
{
PrintStream p = new PrintStream(fileoutput);
contact temp = c;
p.println(temp.getName());
p.println(temp.getTelephone());
p.println(temp.getMobile());
p.println(temp.getAddress());
}
}}
public void searchContacts(){}
public void addContacts()
{
try {
ArrayList<String> Contacts = new ArrayList<String>();
storeName = txtname.getText();
storeAddress = txtaddress.getText();
storeHomePhone = txthomeno.getText();
storeMobilePhone = txtmobno.getText();
Contacts.add(storeName);
Contacts.add(storeHomePhone);
Contacts.add(storeMobilePhone);
Contacts.add(storeAddress);
System.out.println(Contacts);
BufferedWriter out = new BufferedWriter(new FileWriter("address.txt",true));
for(String writeToFile : Contacts){
out.write(writeToFile);
out.newLine();
out.write(" ");
}
out.close();
} catch (IOException ex) {
System.err.println("ERROR:" + ex);
}
}
public void readContacts()
{
try {
BufferedReader file_in = new BufferedReader(new FileReader(
"address.txt"));
String name = "", address = "", telephone = "", mobile = "";
int counter = 0;
while (true) {
String line = file_in.readLine();
if (line == null)
break;
counter++;
if (counter == 1) {
name = line;
} else if (counter == 2) {
address = line;
} else if (counter == 3) {
telephone = line;
} else if (counter == 4) {
mobile = line;
contact c = new contact(name, address, telephone, mobile);
contactList.add(c);
counter = 0;
}
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "File Not Found",
"File Not Found", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
new addressbook();
}
}
address.txt contains this below