filechooser

importing a text file to Jtextfields

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1997 Views - Last Post: 25 November 2009 - 04:28 AM Rate Topic: -----

#16 Addio569   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: filechooser

Posted 25 November 2009 - 03:50 AM

View Postpbl, on 24 Nov, 2009 - 04:39 PM, said:

View PostAddio569, on 24 Nov, 2009 - 03:04 AM, said:

thanks got it working! my next question is a lot harder, and I dont even think its possible.

when my program loads, it loads up contacts, when i click on import it loads different contacts, i want the imported contacts to merge with the existing contacts, is this possible??

thanks

Everything is possible in code :)

Just appends the new contacts in the ArrayList


sorry can you explain a bit further? i only want them to show up, not merge into the textfile

thanks

This post has been edited by Addio569: 25 November 2009 - 03:51 AM

Was This Post Helpful? 0
  • +
  • -

#17 Addio569   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: filechooser

Posted 25 November 2009 - 04:28 AM

ok, i've got to make an address book gui in java. to get the contacts my program reads them in from a file, one line at a time, like so:

name
phone number
mobile
address

when there read in I want to be able to cycle through them, which i can with this code:

public class AddressBook extends JFrame implements ActionListener
{

	FlowLayout leftLayout;
	JFrame winNew; 

		JTextField txtName, txtPhone, txtMobile, txtAddress;
		JButton btnAdd, btnNext, btnPrevious, btnSave, btnDelete;
		JLabel lblTitle, lblName, lblPhone, lblMobile, lblAddress;
		JPanel pTitle, pName, pPhone, pMobile, pAddress, pButtons;
		
		 ArrayList<String> Name = new ArrayList<String>();
		 ArrayList<String> Phone = new ArrayList<String>();
		 ArrayList<String> Mobile = new ArrayList<String>();
		 ArrayList<String> Address = new ArrayList<String>();
		 ArrayList<String> temp = new ArrayList<String>();

	
	 	int i = 0;
	

		public static void main(String[] args) throws IOException
		{
		new AddressBook();
		}
   
	   	public AddressBook()
	   	{
		...
			{
			  File file = fileopen.getSelectedFile();
			  String s1 = "", s2 = "", s3 = "", s4 = "";
				  try 
				  {
				  Scanner fileIn = new Scanner(file);
				  s1 = fileIn.nextLine();
				  s2 = fileIn.nextLine();
				  s3 = fileIn.nextLine();
				  s4 = fileIn.nextLine();
				  {
				  txtName.setText(s1);
				  txtPhone.setText(s2);
				  txtMobile.setText(s3);
				  txtAddress.setText(s4);
				  }
   
		  }
			catch(IOException e) 
			
			{
			   System.out.println("");
			}
			  
 	  
			}
		  }
		});
 

			 
		}


		public void actionPerformed(ActionEvent e)
			{
				if (e.getSource() == btnAdd)
				{
					clearScreen();
				}
		   
					if (e.getSource() == btnPrevious)
				{
						Previous(); 
				}
		
					if (e.getSource() == btnSave)
					
						try
			{
			BufferedWriter fileOut = new BufferedWriter(new FileWriter("../files/Contacts.txt", true)); 
			fileOut.append(txtName.getText());
			fileOut.append("\n");		
			fileOut.append(txtPhone.getText());
			fileOut.append("\n");		
			fileOut.append(txtMobile.getText());
			fileOut.append("\n");	
			fileOut.append(txtAddress.getText());
						
			fileOut.close(); 
						}
			catch (IOException ioe)
			{
			JOptionPane.showMessageDialog(null, ioe.getMessage());
					}
					
					
			 
		   		else if (e.getSource() == btnNext)
				{
					Next();
				}
			   
			}
   
			public String readLine(BufferedReader pReader) 
			{
				try 
			
				{
				return pReader.readLine();
				} catch(IOException IOE) 
			
				{
		
				return null;
				}
		
		}

		public void importContacts()
				{
					try
						{
								BufferedReader infoReader = new BufferedReader(new FileReader("../files/contacts.txt"));
								int i = 0;
								String loadContacts;
 
								while ((loadContacts = infoReader.readLine()) !=null)
										{
												temp.add(loadContacts);
												i++;
										}
 
								int a = 0;
								int b = 0;
 
								for (a = 0, b = 0; a < temp.size(); a++, b++)
								{
										if (b == 4)
										{
												 b = 0;
										}
			   
										if (b == 0)
										{
												Name.add(temp.get(a));	
										}
 
										if (b == 1)
										{
												Phone.add(temp.get(a));	
										}
 
										if (b == 2)
										{
												Mobile.add(temp.get(a));	
										}
 
										if (b == 3)
										{
												Address.add(temp.get(a));	
										}		
								}
						}
 
						catch (IOException ioe)
						{
								JOptionPane.showMessageDialog(null, ioe.getMessage());
 
						}
 
						txtName.setText(Name.get(0));
						txtPhone.setText(Phone.get(0));
						txtMobile.setText(Mobile.get(0));
						txtAddress.setText(Address.get(0));
						
				}

				 

		 public void Previous()
				{
						if (i > 0)
						{
								i--;
						}
						txtName.setText(Name.get(i));
						txtPhone.setText(Phone.get(i));
						txtMobile.setText(Mobile.get(i));
						txtAddress.setText(Address.get(i));
 
				}
 
				public void Next()
				{
						if(i < temp.size() - 1)
						{
						i++;
						}
						txtName.setText(Name.get(i));
						txtPhone.setText(Phone.get(i));
						txtMobile.setText(Mobile.get(i));
						txtAddress.setText(Address.get(i));
 
				}



now, i also have to import a new file, which i also have to be able to cycle through. this is through the filechooser menu. I tried to use the importContacts method to do this, but changed the file name to the new file, which I thought would allow me to cycle through the new contacts, but it wont!

I'm a real novice at java, I've tried reading the api's and books and searching for sample code but I'm really stuck, what would be the easiest way of doing this??

thanks

This post has been edited by Addio569: 25 November 2009 - 04:29 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2