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 7 of 7

Thread: Application Continued (problem)

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

    Question Application Continued (problem)

    Hey, i am having a problem with a function in my program to add new contacts to an existing array list i have created (name Record). Basically i am unsure how to enter a persons name,phonenum,mobnum and address into textfields of a GUI then press an ADD button which should store the contact in the array list. I am using a seprate class containg get and set methods, however i am unsure how i would implement these into my actionlistener & if i would need to create an actionlistener on each textfield?. Here is my code so far:
    any help would be greatly appreciated, thanks.
    [B]public class address extends JFrame[/B]
    {
        JLabel lblhomeTel,lblmobileTel,lblName,lblAddress;
        JTextField txtAddress,txthomeTel, txtmobileTel,txtName;
        JPanel panel,panel2,panel3;
        JFrame frame;
        JButton buttonnew,buttondelete,buttonedit,buttonnext,buttonprevious,
        buttonFind;
        JMenu menuList;
        String fileInput,readline;
        ArrayList<String> arrayOfFile = new ArrayList<String>();
        ArrayList<Record> records = new ArrayList<Record>();
        int index = 0;
     
        public static void main(String[] argStrings)
        {
     
     
        	address GUI= new address();
            GUI.address();
     
        }
     
        public void address ()
        {	
            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 Addresses");
            menuList.add(importFile);
            importFile.addActionListener(new ActionListener()
            {
            public void actionPerformed(ActionEvent event) 
            {
                         try {
                	BufferedReader fileStream = new BufferedReader(new FileReader("src/contacts.buab"));
                    while (true)
                    {
                    String fileInput = fileStream.readLine();
                    if(fileInput==null)
                        break;
     
           		Record a = new Record();
                    a.setname(fileInput);
                    a.setphone(fileStream.readLine());
                    a.setmphone(fileStream.readLine());
                    a.setaddress(fileStream.readLine());
                    records.add(a);
           		System.out.println(a.getname());
     
                    }fileStream.close();
                           }catch (Exception ex) { JOptionPane.showMessageDialog(rootPane, ex);
                    }displaycontact();
                           }});
     
                    	JMenuItem exportFile=new JMenuItem("Export Addresses to file");
                    	menuList.add(exportFile);
                    	JMenuItem exitProg= new JMenuItem("Exit Program");
                    	menuList.add(exitProg);
                    	exitProg.addActionListener(new ActionListener() {
     
                           public void actionPerformed(ActionEvent event) 
                           {
                           System.exit(0);
             	       }
     
     
             		});
                    	lblName= new JLabel("Name:");
                    	panel3.add(lblName);
     
                    	txtName= new JTextField("");
                    	txtName.setText("");
                    	panel3.add(txtName);
     
                    	lblhomeTel= new JLabel("Home Telephone:");
                    	panel3.add(lblhomeTel);
     
                    	txthomeTel= new JTextField("");
                    	txthomeTel.setText("");
                    	panel3.add(txthomeTel);
     
            		lblmobileTel = new JLabel("Mobile Telephone:");
            		panel3.add(lblmobileTel);
     
                    	txtmobileTel = new JTextField("");
                    	txtmobileTel.setText("");
            		panel3.add(txtmobileTel);
     
                    	lblAddress= new JLabel("Address:");
                    	panel3.add(lblAddress);
     
                    	txtAddress= new JTextField("");
                    	txtAddress.setText("");
                    	panel3.add(txtAddress);
     
                            buttonnext= new JButton(">>");
                    	panel2.add(buttonnext);
                            buttonnext.addActionListener(new ActionListener()
                            {
                                public void actionPerformed(ActionEvent event)
                                {
                                    index ++;
                                    displaycontact();
                                }
                            });
     
     
                            buttonprevious= new JButton("<<");
                    	panel2.add(buttonprevious);
     
                            buttonprevious.addActionListener(new ActionListener()
                            {
                            public void actionPerformed(ActionEvent event)
                            {
                                index--;
                                displaycontact();
     
                            }});
                    	buttonnew= new JButton("Add Contact");
                    	panel2.add(buttonnew);
                    	buttonnew.addActionListener(new ActionListener() 
     
                    	{
                     	public void actionPerformed(ActionEvent event)
                     	{
     
     
     
     
     
                            }});
     
     
                   		buttonFind= new JButton("Search Contact");
                    	panel2.add(buttonFind);
     
                            buttonedit = new JButton("Edit Contact");
                            panel2.add(buttonedit);
     
                    	buttondelete= new JButton("Delete Record");
                    	panel2.add(buttondelete);
                    	buttondelete.addActionListener(
                		new ActionListener()
     
     
     
     
                	{
                    public void actionPerformed(ActionEvent event)
                    {
    			String temp=txtName.getText();
                            for(Record Record:records)
                            {
                                if(temp.equals(Record.getname()));
                                {
                                    txtName.setText("");
                                    txthomeTel.setText("");
                                    txtmobileTel.setText("");
                                    txtAddress.setText("");
                                    records.remove(Record);
                                    records.remove(Record);
                                    records.remove(Record);
                                    records.remove(Record);
     
                                }
                            }
     
     
                    }
                });
     
     
    			frame.setJMenuBar(menuBar);
    			panel.add(panel3);
    			panel.add(panel2);
    			frame.add(panel);
    			frame.setVisible(true);
     
     
     
    }
            public void displaycontact()
            {
                txtName.setText(records.get(index).name);
                txthomeTel.setText(records.get(index).phone);
                txtmobileTel.setText(records.get(index).mphone);
                txtAddress.setText(records.get(index).address);
            }
     
     
    }
     
     
    [B]public class Record[/B]{
     
        static void add(String text) {
        throw new UnsupportedOperationException("Not Done Yet");
        }
    	public String name;
    	public String phone;
    	public String mphone;
    	public String address;
     
     
    	public Record(){}
     
    	public Record(String name, String phone, String mphone, String address)
            {
     
    		this.name = name;
    		this.phone = phone;
    		this.mphone = mphone;
    		this.address = address;		
    	}
     
            public String getname()
            {
                return this.name;
            }
            public String getphone()
            {
                return this.phone;
            }
            public String getmphone()
            {
                return this.mphone;
            }
            public String getaddress()
            {
                return this.address;
            }
     
            public void setname(String name)
            {
                this.name = name;
            }
            public void setphone(String phone)
            {
                 this.phone = phone;
            }
            public void setmphone(String mphone)
            {
                 this.mphone = mphone;
            }
            public void setaddress(String address)
            {
                 this.address = address;
            }
    }
    Last edited by helloworld922; November 25th, 2009 at 09:25 PM. Reason: Please use [code] tags!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Application Continued (problem)

    mmm... long code

    I'll tell you how you can do this, but I'll leave the implementation up to you.

    Add your class to the ActionListener list of the button, and then when that ActionEvent is triggered, retrieve the text in the fields you want:

    public class address extends JFrame implements ActionListener
    {
         public address()
         {
              // have the constructor as you have it, just add this line of code to the end
              addButton.addActionListener(this);
         }
     
         public void actionPerformed(ActionEvent e)
         {
              if (e.getActionCommand().equals("ADD"))
              {
                   // extract the info from the text fields you want
              }
         }
    }

    If you're using the JTextField object, you can just use the getText() method and get the text. If you're using TextField objects, the job of getting text out is a bit trickier.

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

    Question Re: Application Continued (problem)

    thanks very much, I've created the action listener , then i've tried to get the text using the constructor and put it in the arraylist i created. When i put text into the fields and add i can't see any changes. here is my code:
    buttonnew= new JButton("Add Contact");
                    	panel2.add(buttonnew);
                    	buttonnew.addActionListener(new ActionListener() 
     
                    	{
                     	public void actionPerformed(ActionEvent event)
                     	{
                                if (event.getActionCommand().equals("ADD"))
     
                                records.add(new Record(txtName.getText(), txthomeTel.getText(), txtmobileTel.getText(), txtAddress.getText()));
             }
    will i need to use bufferedwriter, and if so how do i implement this? im having trouble with this function, doesn't seem to be working. Thanks again
    Last edited by helloworld922; November 26th, 2009 at 11:53 AM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Application Continued (problem)

    Ah. The getActionCommand() for the JButton class returns the caption that you have the button under. I just put ADD because I thought that's what the button said. You'll need to change it to "Add Contact" (without the quotes). An alternative is to use the getSource() method and see if it's the buttonnew object.

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

    Default Re: Application Continued (problem)

    oh yeah! thanks for that, its saying '.equals() on incompatible types' when i put the name of the button into the getActionCommand() like this:
    if (event.getActionCommand().equals(Add Contact))
     
    have also tried:
     
    if (event.getActionCommand().equals(buttonnew))
    thanks
    Last edited by helloworld922; November 26th, 2009 at 12:55 PM.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Application Continued (problem)

    Oops, my bad. the "Add Contact" needs to be in quotes to denote that it's a string (the getActionCommand() method returns a String, so you must compare it to a String).

    If you want to compare with buttonnew (no quotes here), use the getSource() method.

    // use either solution 1 or solution 2
    // solution1:
    if (event.getActionCommand().equals("Add Contact"))
    {
        // stuff
    }
     
    // solution2:
    if (event.getSource().equals(buttonnew))
    {
         // stuff
    }

    FYI: the first solution will make expanding your program easier because you may want to have several locations that you can add a new contact(a menu item, a shortcut, etc.). If you have them all pass the same action command, you can have only one if statement to handle them all the same, while by using getSource() you'll have to use multiple if statements/or conditionals to achieve the same goal.
    Last edited by helloworld922; November 26th, 2009 at 01:01 PM.

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

    Question Re: Application Continued (problem)

    thankyou very much, i can now create a contact putting them into the arraylist when the "Add Contact" button is pressed, using the 1st solution

    one more quick question:
    when i delete a contact from the arraylist, i am using the following, basically im adding an actionlistener to the button, and saying get the selected text from the arraylist using the get method. Clearing the fields and then removing each line from the arraylist. However i think the last part may be wrong, as it will not know what position to delete from in the arraylist, when i run the program if i go next & previous the details appear again. how would i specify this? thankyou.

     
      	buttondelete= new JButton("Delete Record");
                    	panel2.add(buttondelete);
                    	buttondelete.addActionListener(
                		new ActionListener()
     
                	{
                    public void actionPerformed(ActionEvent event)
                    {
    			String temp=txtName.getText();
                            for(Record Record:records)
                            {
                                if(temp.equals(Record.getname()));
                                {
                                    txtName.setText("");
                                    txthomeTel.setText("");
                                    txtmobileTel.setText("");
                                    txtAddress.setText("");
                                    records.remove(Record);
                                    records.remove(Record);
                                    records.remove(Record);
                                    records.remove(Record);
     
                                }
                            }

Similar Threads

  1. application id not found
    By pradeepsetty in forum Java IDEs
    Replies: 3
    Last Post: June 2nd, 2010, 03:37 AM
  2. Help with Java Application
    By Riston in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 19th, 2009, 07:17 PM
  3. RMI over SSL Client application
    By boilerchicken in forum Java Networking
    Replies: 0
    Last Post: November 10th, 2009, 07:52 AM
  4. application Task problem - updating JTree
    By idandush in forum AWT / Java Swing
    Replies: 2
    Last Post: June 18th, 2009, 04:15 AM
  5. How to know number of user downloading an application?
    By jazz2k8 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 3rd, 2008, 05:34 AM