Results 1 to 2 of 2
  1. #1
    cachi is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with program coding

    Hi, I am having problems with a program i am making the program coding is

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Contacts implements ActionListener {
    	JFrame contactsFrame;
        JPanel contactsPanel;
        JTextField name, lname, phonenum;
        JLabel names, lnames, phonenums;
        JButton next, back, add, edit, save;
        List data = new ArrayList();
        int a=0, b=0;
        
            public Contacts() {
            //Create and set up the window.
            contactsFrame = new JFrame("Contacts Manager Program");
            contactsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            contactsFrame.setSize(3020, 40);
    
            //Create and set up the panel.
            contactsPanel = new JPanel(new GridLayout(5, 2));
    
            //Add the widgets.
            addWidgets();
    
            //Add the panel to the window.
            contactsFrame.getContentPane().add(contactsPanel, BorderLayout.CENTER);
    
            //Display the window.
            contactsFrame.pack();
            contactsFrame.setVisible(true);
        }
        
        private void addWidgets() {
            //Create widgets.
            name = new JTextField(data.get(0));
            lname = new JTextField(data.get(1));
            phonenum = new JTextField(data.get(2));
            names = new JLabel("Name:");
            lnames = new JLabel("Last Name:");
            phonenums = new JLabel("Phone Number:");
            next = new JButton("Next");
            back = new JButton("Back");
            add = new JButton("Add new Contact");
            edit = new JButton("Save Changes");
    
            //Listen to events from the Convert button.
            next.addActionListener(this);
            back.addActionListener(this);
            add.addActionListener(this);
    
            //Add the widgets to the container.
            contactsPanel.add(names);
            contactsPanel.add(name);
            contactsPanel.add(lnames);
            contactsPanel.add(lname);
            contactsPanel.add(phonenums);
            contactsPanel.add(phonenum);
            contactsPanel.add(back);
            contactsPanel.add(next);
            contactsPanel.add(add);
            contactsPanel.add(edit);
        }
        
        
    	public void actionPerformed(ActionEvent event) {
    		if(event.getSource()==add) {
        	data.add(name.getText());
        	data.add(lname.getText());
        	data.add(phonenum.getText());
        	b=b+3;
        	}
        	if(event.getSource()==next) {
        		if (a!=b) {
        			a=a+3;
        	}
        	name.setText(data.get(a));
        	lname.setText(data.get(a+1));
        	phonenum.setText(data.get(a+2));
        	}
        	if(event.getSource()==back) {
        		if(a!=0){
        			a=a-3;
        		}
        	name.setText(data.get(a));
        	lname.setText(data.get(a+1));
        	phonenum.setText(data.get(a+2));
        	}
        	//if(event.getSource()==edit) {
        	//data[a]=name.getText();
    		//data[a+1]=lname.getText();
    		//data[a+2]=phonenum.getText();
    		//data2[a]=name.getText();
    		//data2[a+1]=lname.getText();
    		//data2[a+2]=phonenum.getText();
        	//}
    		}
    	
    	
        private static void createAndShowGUI() {
            //Make sure we have nice window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);
    
            Contacts converter = new Contacts();
        }
    
        public void main(String[] args)
        throws Exception
        {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            read();
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
        public void write()
        throws Exception
        {
    	
        }
        public void read()
        throws Exception
        {
        	data = new ArrayList();
        	BufferedReader reader= new BufferedReader(new FileReader("foo.txt"));
    		String textline = null;
    		while((textline = reader.readLine()) != null){
    		data.add(textline);
    		b++;
    		}
    		reader.close();
        }
    }
    The problems i am having with are
    Java Code:
    E:\Contacts.java:39: cannot resolve symbol
    symbol  : constructor JTextField (java.lang.Object)
    location: class javax.swing.JTextField
            name = new JTextField(data.get(0));
                   ^
    E:\Contacts.java:40: cannot resolve symbol
    symbol  : constructor JTextField (java.lang.Object)
    location: class javax.swing.JTextField
            lname = new JTextField(data.get(1));
                    ^
    E:\Contacts.java:41: cannot resolve symbol
    symbol  : constructor JTextField (java.lang.Object)
    location: class javax.swing.JTextField
            phonenum = new JTextField(data.get(2));
                       ^
    E:\Contacts.java:80: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (java.lang.Object)
            name.setText(data.get(a));
                ^
    E:\Contacts.java:81: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (java.lang.Object)
            lname.setText(data.get(a+1));
                 ^
    E:\Contacts.java:82: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (java.lang.Object)
            phonenum.setText(data.get(a+2));
                    ^
    E:\Contacts.java:88: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (java.lang.Object)
            name.setText(data.get(a));
                ^
    E:\Contacts.java:89: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (java.lang.Object)
            lname.setText(data.get(a+1));
                 ^
    E:\Contacts.java:90: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (java.lang.Object)
            phonenum.setText(data.get(a+2));
                    ^
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    List data = new ArrayList();
    // What does get return?
    System.out.println("data.get(0) = " + data.get(0).getClass().getName());
    Java has no idea what is in this list. The programmer should know but the jvm cannot know. So you must cast the object that the get method returns. If there are Strings in the list then
    Java Code:
    String s = (String)list.get(0);
    This is acceptable as the argument for the JTextComponent.setText method.

Similar Threads

  1. Coding an FTP server in java
    By Zucheto in forum Networking
    Replies: 3
    Last Post: 06-22-2008, 04:24 AM
  2. Help On Coding problem
    By mandrake446 in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 07:01 AM
  3. Error in my coding
    By one198 in forum New To Java
    Replies: 2
    Last Post: 10-13-2007, 05:07 AM
  4. Cannot solve the coding problem of my assignment
    By elimmom in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 11:33 AM
  5. Problem in my coding
    By one198 in forum New To Java
    Replies: 9
    Last Post: 08-09-2007, 10:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •