Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with Java Application

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Location
    Maldives
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile 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
    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);
    }
    }
    Last edited by helloworld922; November 19th, 2009 at 08:40 PM. Reason: Please use [code] tags!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

Similar Threads

  1. Replies: 24
    Last Post: August 4th, 2014, 12:49 PM
  2. Java program to Add a JMenu toolbar to a Java Swing application
    By JavaPF in forum Java Swing Tutorials
    Replies: 6
    Last Post: March 6th, 2012, 12:25 PM
  3. How to Sendkeys to an application in Java using the Robot Class
    By JavaPF in forum Java SE API Tutorials
    Replies: 6
    Last Post: August 4th, 2011, 12:13 AM
  4. RMI over SSL Client application
    By boilerchicken in forum Java Networking
    Replies: 0
    Last Post: November 10th, 2009, 07:52 AM
  5. java application connecting to a server
    By wildheart25c in forum Java Networking
    Replies: 2
    Last Post: September 17th, 2009, 07:22 AM