Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-24-2009, 02:49 PM
Member
 
Join Date: Nov 2009
Posts: 5
Rep Power: 0
Ekul is on a distinguished road
Default java project help, reading in from a file and adding data to JTable
Hey guys iv got this project to make an address book that has all kinds of functions and iv got them all working apart from one bit, it needs to be able to read a file when it opens as well as import other data from other files.

The data comes in the following format:
Tony Hancock
01202719029
07676101393
1 Charminster Road, Bournemouth, Dorset, BH8 8UE
Auberon Waugh
01823810098
None
Combe Florey House, Combe Florey, Somerset, TA4 3JD

so name, home number, mobile number and then address.

i have been trying to bufferedread the file in and then add the details to an arrayList which i then need to set up a loop to catch the first index which would be the name then link that to the Jtable name section (textFirstName and textLastName) somehow, then repeat that for another 3 index's till index 3 and start again, BUT how do i do that????

any help would be great guys, im so close to finishing and got very little time left.

Thanks

Code:
public class Q3 implements ActionListener
{
	
   	JFrame addressBook;   	
   	JFrame contacts;
   	JList theList;
   	DefaultTableModel tableModel;
   	
   	
      	
   	JLabel labFirstName, labLastName, labAddress, labPhone, labMob, searchTitle;
   	JTextField textFirstName, textLastName, textAddress, textPhone, textMob, contactsTextArea;
   	JButton buttonSave, buttonDelete, buttonImport, buttonSearch, buttonExit, buttonReset;
    
    
   	
   	String name, address, allContacts;
	
   	int phone, mob;
   	int recordNumber;	 // used to naviagate using >> and << buttons 
   	Container container1, container2;   	
   	JPanel panel1;
   	JMenuBar menu_bar;
   	JMenu file;
   	JMenuItem exit;
   	JMenuItem open;
   	JScrollPane scroller;
   	JTable table;
   	
   	ArrayList<contact> people = new ArrayList<contact>();
   	TableRowSorter <DefaultTableModel> sorter;
   	
   	
   	
   	
   	
   	   	
   	
   	
   	public static void main(String args[]) throws Exception
   	
   	{
      new Q3(); 	  
    }
   	
   	public Q3() throws Exception
    { 	
 	    name    = "";
 	    address = "";
 	    phone   = -1 ;		//Stores 0 to indicate no Phone Number
 	    mob = -1;
 	    recordNumber = -1;
 	
 	    createGUI();
 	    createContactsFrame();
 	    createTable();
 	    readFromFile();
 	  	
 	  	
 	}
   	
   	public void createTable()
   	{
   		tableModel = new DefaultTableModel();
   		table = new JTable (tableModel);
   		tableModel.addColumn("Name");
   		tableModel.addColumn("Address");
   		tableModel.addColumn("Home Number");
   		tableModel.addColumn("Mobile Number");
   		table.setAutoCreateRowSorter(true);
   		table.setVisible(true);
   		
   		tableModel.addRow(new Object[] 
   				{"Mary Robinson", "85 Brick Road", "0123481244", "07813538241"});
   		
   		tableModel.addRow(new Object[]
   		        {"Alison Jones", "17 Lemar Street", "02084579121", "07753985712"});
   	               	            
   		table.setPreferredScrollableViewportSize(new Dimension(500, 100));
   	    table.setFillsViewportHeight(true);
   	    
   	    scroller = new JScrollPane(table);
   	    panel1.add(scroller);
   	  
   	    sorter = new TableRowSorter <DefaultTableModel>(tableModel);
   	    table.setRowSorter(sorter);
   	    
   	    
   	 
   	}
   	
   
   		
   	
   	
   	public void createContactsFrame()
   	{
   				
   		//set frame characteristics
   		contacts = new JFrame("Contacts");        
   		contacts.setSize(525,300);                            
   		contacts.setVisible(true);
   		contacts.setResizable(false);
   		contacts.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   		
   		//set panel to hold components   		
   		panel1 = new JPanel();
   		contacts.getContentPane().add(panel1);
   		panel1.setBackground(Color.BLACK);
   		
   		
   		menu_bar = new JMenuBar();
   		contacts.setJMenuBar(menu_bar);
   		file = new JMenu ("File");
   		menu_bar.add(file);
   		exit = new JMenuItem ("Exit");
   		file.add(exit);
   		exit.addActionListener(this);
   		open = new JMenuItem ("Open");
   		file.add(open);
   		open.addActionListener(this);
   		   		
   	}
   	
	

	public void createGUI()
   	{

   		/*Create a frame, get its contentpane and set layout*/
   		addressBook = new JFrame("Address Book");
   		addressBook.setSize(300,300);
   		addressBook.setResizable(false);
   		addressBook.setVisible(true);
   		addressBook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 		   		
   		container1 = addressBook.getContentPane();
   		container1.setLayout(new GridBagLayout());
   		container1.setBackground(Color.black);
   		container1.setSize(300, 300);
   		
   		menu_bar = new JMenuBar();
   		addressBook.setJMenuBar(menu_bar);
   		file = new JMenu ("File");
   		menu_bar.add(file);
   		exit = new JMenuItem ("Exit");
   		file.add(exit);
   		exit.addActionListener(this);
   	
   		
   		  		
   		//Arrange components on contentPane and set Action Listeners to each JButton
   		arrangeComponents();  
   		
   		
   		addressBook.setVisible(true);

   	}
   	public void arrangeComponents()
   	{
   		
   		
   		labFirstName = new JLabel("First Name");
   		labFirstName.setForeground(Color.white);
   		
   		labLastName = new JLabel("Last Name");
   		labLastName.setForeground(Color.white);
   		   		
   		labAddress = new JLabel("Address");
   		labAddress.setForeground(Color.white);
   		
   		labPhone = new JLabel("Home Number");
   		labPhone.setForeground(Color.white);
   		
   		labMob = new JLabel("Mobile Number");
   		labMob.setForeground(Color.white);   		
   		
   		textFirstName    = new JTextField(20);
   		textLastName = new JTextField(20);
   		textAddress = new JTextField(20);
   		textPhone   = new JTextField(20);
   		textMob   = new JTextField(20);

   		buttonSave   = new JButton("Save");
   		buttonDelete = new JButton("Delete");
   		buttonSearch = new JButton("Search");
   		buttonImport = new JButton("Import");

   		buttonReset = new JButton("Reset");
   		buttonExit    = new JButton("Exit");

   		/*add all initialized components to the container*/
   		GridBagConstraints gridBagConstraintsx01 = new GridBagConstraints();
        gridBagConstraintsx01.gridx = 0;
        gridBagConstraintsx01.gridy = 0;
        gridBagConstraintsx01.insets = new Insets(5,5,5,5); 
        container1.add(labFirstName, gridBagConstraintsx01);
        
        GridBagConstraints gridBagConstraintsx02 = new GridBagConstraints();
        gridBagConstraintsx02.gridx = 1;
        gridBagConstraintsx02.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx02.gridy = 0;
        gridBagConstraintsx02.gridwidth = 2;
        gridBagConstraintsx02.fill = GridBagConstraints.BOTH;
        container1.add(textFirstName, gridBagConstraintsx02);
        
        GridBagConstraints gridBagConstraintsx03 = new GridBagConstraints();
        gridBagConstraintsx03.gridx = 0;
        gridBagConstraintsx03.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx03.gridy = 2;
        container1.add(labAddress, gridBagConstraintsx03);
        
        GridBagConstraints gridBagConstraintsx04 = new GridBagConstraints();
        gridBagConstraintsx04.gridx = 1;
        gridBagConstraintsx04.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx04.gridy = 2;
        gridBagConstraintsx04.gridwidth = 2;
        gridBagConstraintsx04.fill = GridBagConstraints.BOTH;
        container1.add(textAddress, gridBagConstraintsx04);
        
        GridBagConstraints gridBagConstraintsx05 = new GridBagConstraints();
        gridBagConstraintsx05.gridx = 0;
        gridBagConstraintsx05.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx05.gridy = 3;
        container1.add(labPhone, gridBagConstraintsx05);
        
        GridBagConstraints gridBagConstraintsx06 = new GridBagConstraints();
        gridBagConstraintsx06.gridx = 1;
        gridBagConstraintsx06.gridy = 3;
        gridBagConstraintsx06.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx06.gridwidth = 2;
        gridBagConstraintsx06.fill = GridBagConstraints.BOTH;
        container1.add(textPhone, gridBagConstraintsx06);
        
        GridBagConstraints gridBagConstraintsx07 = new GridBagConstraints();
        gridBagConstraintsx07.gridx = 0;
        gridBagConstraintsx07.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx07.gridy = 4;
        container1.add(labMob, gridBagConstraintsx07);
        
        GridBagConstraints gridBagConstraintsx08 = new GridBagConstraints();
        gridBagConstraintsx08.gridx = 1;
        gridBagConstraintsx08.gridy = 4;
        gridBagConstraintsx08.gridwidth = 2;
        gridBagConstraintsx08.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx08.fill = GridBagConstraints.BOTH;
        container1.add(textMob, gridBagConstraintsx08);
        
        GridBagConstraints gridBagConstraintsx15 = new GridBagConstraints();
        gridBagConstraintsx15.gridx = 0;
        gridBagConstraintsx15.gridy = 1;
        gridBagConstraintsx15.insets = new Insets(5,5,5,5); 
        container1.add(labLastName, gridBagConstraintsx15);
        
        GridBagConstraints gridBagConstraintsx12 = new GridBagConstraints();
        gridBagConstraintsx12.gridx = 1;
        gridBagConstraintsx12.gridy = 1;
        gridBagConstraintsx12.insets = new Insets(5,5,5,5); 
        gridBagConstraintsx12.gridwidth = 2;
        gridBagConstraintsx12.fill = GridBagConstraints.BOTH;
        container1.add(textLastName, gridBagConstraintsx12);
        
        GridBagConstraints gridBagConstraintsx09 = new GridBagConstraints();
        gridBagConstraintsx09.gridx = 0;
        gridBagConstraintsx09.gridy = 5;
        gridBagConstraintsx09.insets = new Insets(5,5,5,5); 
        container1.add(buttonSave, gridBagConstraintsx09);
        
        GridBagConstraints gridBagConstraintsx10 = new GridBagConstraints();
        gridBagConstraintsx10.gridx = 1;
        gridBagConstraintsx10.gridy = 5;
        gridBagConstraintsx10.insets = new Insets(5,5,5,5); 
        container1.add(buttonDelete, gridBagConstraintsx10);
        
        GridBagConstraints gridBagConstraintsx11 = new GridBagConstraints();
        gridBagConstraintsx11.gridx = 2;
        gridBagConstraintsx11.gridy = 5;
        gridBagConstraintsx11.insets = new Insets(5,5,5,5); 
        container1.add(buttonImport, gridBagConstraintsx11);
                
        
        
        GridBagConstraints gridBagConstraintsx13 = new GridBagConstraints();
        gridBagConstraintsx13.gridx = 0;
        gridBagConstraintsx13.gridy = 6;
        gridBagConstraintsx13.insets = new Insets(5,5,5,5); 
        container1.add(buttonSearch, gridBagConstraintsx13);
        
        GridBagConstraints gridBagConstraintsx14 = new GridBagConstraints();
        gridBagConstraintsx14.gridx = 1;
        gridBagConstraintsx14.gridy = 6;
        gridBagConstraintsx14.insets = new Insets(5,5,5,5); 
        container1.add(buttonReset, gridBagConstraintsx14);
                
        GridBagConstraints gridBagConstraintsx16 = new GridBagConstraints();
        gridBagConstraintsx16.gridx = 2;
        gridBagConstraintsx16.gridy = 6;
        gridBagConstraintsx16.insets = new Insets(5,5,5,5); 
        container1.add(buttonExit, gridBagConstraintsx16);
        
        
        
       
        
        buttonExit.addActionListener(this);
        buttonSave.addActionListener(this);
        buttonDelete.addActionListener(this);
        buttonSearch.addActionListener(this);
        buttonReset.addActionListener(this);
        
        
   	}
   	
   	public void saveToFile()
   	{
   		FileWriter output;
   	 
		try
		{
			output = new FileWriter(new File( "contactFile.txt" ) );
			//output.writeObject(people);
			for (int i = 0; i < people.size(); i++)
			{
				contact c = people.get(i);
				output.write(c.getFirstName() + " " + c.getLastName() + "\n");
				output.write(c.getHomeNumber() + "\n");
				output.write(c.getMobileNumber() + "\n");
				output.write(c.getAddress() + "\n");
			}
			output.close();
		} 
		catch (IOException ioException)
		{
			System.err.println( "Error." );
		} 

   	}
   	
   	
	public void readFromFile () throws Exception
   	{
   		// open file with bufferedreader
   		// while ()
   		//   check 
   		// counter....... number of lines per contact is 4, name,home number, mobile number, address.
   		
   		ArrayList<String> fileRead = new ArrayList<String>();
   		
   		try 
   		{
   			String lineread = "";
   			
   			
   			BufferedReader bufferedReadIn = new BufferedReader(new FileReader("H:\\Copy of address book//a.txt"));
			while ((lineread = bufferedReadIn.readLine()) != null)
			
			{
				fileRead.add(lineread);
			}
			System.out.println(fileRead.get(3));
			
		for (int fileRead = 0; fileRead <=3; fileRead++)
			System.out.println(fileRead);
			
			
			
			
			//contact a = new contact(textFirstName.getText(), textLastName.getText(),textPhone.getText(), textMob.getText(), textAddress.getText());
   			//people.add(a);
   			
   			//this.tableModel.addRow(new Object[]{a.getFirstName() + " " + a.getLastName(),a.getAddress(),
   			//		a.getHomeNumber(), a.getMobileNumber()});
			
			

			bufferedReadIn.close();
		}	
		
		catch (IOException ex)
		{
			System.out.println("error");
		}
   		
   		
   	}
   	
   	

	public void actionPerformed (ActionEvent e)
   	{
   		
   		if (e.getSource() == buttonExit)
   		{			
   			System.exit(0);
   		}
   		else if (e.getActionCommand().equals("Exit"))
   		{	
   			System.exit(0);
   		}	
   		else if (e.getSource() == buttonSave)
   		{
   			contact c = new contact(textFirstName.getText(), textLastName.getText(),textPhone.getText(), textMob.getText(), textAddress.getText());
   			people.add(c);
   			
   			this.tableModel.addRow(new Object[]{c.getFirstName() + " " + c.getLastName(),c.getAddress(),
   					c.getHomeNumber(), c.getMobileNumber()});
   			saveToFile();
   			
   		}
   		else if (e.getSource() == buttonDelete)
   		{
   			tableModel.removeRow(table.getSelectedRow());
   		} 
   		else if(e.getSource() == buttonSearch)
   		{
   			String text = textFirstName.getText() + textLastName.getText() + textAddress.getText() + textPhone.getText() + textMob.getText();
   			if (text.length() == 0)
   			{
   				sorter.setRowFilter(null);
   			}
   			else
   			{
   				sorter.setRowFilter(RowFilter.regexFilter(text));
   			}
   			
   		}
   		else if (e.getActionCommand().equals("Open"))
   		{
   			JFileChooser chooser = new JFileChooser();
   			if (chooser.showOpenDialog(container1) == JFileChooser.APPROVE_OPTION)
   			{
   				try
   				{
   					BufferedReader file_in = new BufferedReader(
   							new FileReader (chooser.getSelectedFile()));
   				
   					while (true)
   	   				{
   	   					String line = file_in.readLine();
   	   					if(line == null)
   	   						break;
   	   				
   	   				
   	   					this.people.toArray();
   	   				
   	   				}
   				}
   				catch (FileNotFoundException fnfe)
   				{
   					
   				}
   				catch (IOException ioe)
   				{
   					
   				}
   			}
   		}
   		else if (e.getSource() == buttonReset)
   			{
   				this.textFirstName.setText("");
   				this.textLastName.setText("");
   				this.textAddress.setText("");
   				this.textPhone.setText("");
   				this.textMob.setText("");
   			}	
   	}
 }
Code:
public class contact 
{
	String firstName;
	String lastName;
	String homeNumber;
	String mobileNumber;
	String address;
	
	public contact (){}
	
	public contact (String fn, String ln, String hn, String mn, String a)
	{
		firstName = fn;
		lastName = ln;
		homeNumber = hn;
		mobileNumber = mn;
		address = a;
		
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getHomeNumber() {
		return homeNumber;
	}

	public void setHomeNumber(String homeNumber) {
		this.homeNumber = homeNumber;
	}

	public String getMobileNumber() {
		return mobileNumber;
	}

	public void setMobileNumber(String mobileNumber) {
		this.mobileNumber = mobileNumber;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	

	
	
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading data to file puk284 Advanced Java 1 04-28-2009 04:19 PM
reading data from text file .. help plz Thug heart New To Java 7 02-15-2009 08:29 PM
Reading and writing the contents of jtable into a text file Manfizy NetBeans 6 12-12-2008 04:35 PM
Reading Data from a file ramachandran New To Java 2 10-24-2007 08:22 AM
Reading file data that contains no spaces jdepue Advanced Java 1 08-01-2007 05:58 AM


All times are GMT +2. The time now is 08:30 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org