Help with Java Application
hi, i have been given the task to create an address book in java using swing components. The address book will need to read in/out from a file, displaying, name, mobTel, homeTel & address. Other functions include searching through records, adding new data, and deleting entries. Here is my code so far. I am using a text editor and CMD. when i run the application the addresses in the file are read in using Buffereader & stored in the array list created, which works. However i am unsure how i would read each line of the file (as there are 4) into the GUI. I know it would involve a method of getting the text from the arraylist and storing it into the correct textfield. Any help would be greatly appreciated :)
Code :
import java.lang.*;
import java.util.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class address extends JFrame
{
JLabel lblhomeTel,lblmobileTel,lblName,lblAddress;
JTextField txtAddress,txthomeTel, txtmobileTel,txtName;
JPanel panel,panel2,panel3;
JFrame frame;
JButton buttonnew,buttondelete,buttonnext,buttonprevious,
buttonFind;
JMenu menuList;
String arrayOfFile,fileInput,readline;
public static void main(String[] argStrings)
{
address GUI= new address();
GUI.address();
}
public void address ()
{
final ArrayList<String> arrayOfFile = new ArrayList<String>();
try {
BufferedReader fileStream = new BufferedReader(new FileReader("C://Users/Me/Documents/JavaProjects/Address/Src/contacts.txt"));
String fileInput = "";
while ((fileInput = fileStream.readLine()) != null)
{
arrayOfFile.add(fileInput);
}
for (String readline : arrayOfFile)
{
System.out.println(readline);
}
fileStream.close();
}
catch (Exception ex) { System.out.println("Exception: " + ex.getMessage());
}
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 new Addresses");
menuList.add(importFile);
JMenuItem exitProg= new JMenuItem("Exit Program");
menuList.add(exitProg);
exitProg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
ArrayList<String> contacts = new ArrayList<String>();
lblName= new JLabel("Name:");
panel3.add(lblName);
txtName= new JTextField("");
panel3.add(txtName);
lblhomeTel= new JLabel("Home Telephone:");
panel3.add(lblhomeTel);
txthomeTel= new JTextField("");
panel3.add(txthomeTel);
lblmobileTel = new JLabel("Mobile Telephone:");
panel3.add(lblmobileTel);
txtmobileTel = new JTextField("");
panel3.add(txtmobileTel);
lblAddress= new JLabel("Address:");
panel3.add(lblAddress);
txtAddress= new JTextField("");
panel3.add(txtAddress);
buttonnext= new JButton("<<");
panel2.add(buttonnext);
buttonprevious= new JButton(">>");
panel2.add(buttonprevious);
buttonnew= new JButton("Add Contact");
panel2.add(buttonnew);
buttonnew.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
txtName.getText();
arrayOfFile.add(txtName.getText());
}});
buttonFind= new JButton("Search Contact");
panel2.add(buttonFind);
buttondelete= new JButton("Delete Contact");
panel2.add(buttondelete);
frame.setJMenuBar(menuBar);
panel.add(panel3);
panel.add(panel2);
frame.add(panel);
frame.setVisible(true);
}
}
Re: Help with Java Application
If I understand correctly, one way would be rather than reading each line of the file into an ArrayList of Strings, create a new class (perhaps name it Contact?) that will hold the appropriate info, and parse each line of the file into a new object of class Contact (adding that to a List). You can then set the appropriate GUI display text based upon a user selection and fetching the data from the List.