Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Java Help

  1. #1
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java Help

    I am having problems with a program i am making the program coding is
    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
    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));
                    ^

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    The indicated lines all call some non-existing constructors, like:
    name = new JTextField(data.get(0));
    data.get(0) returns an object, the constructor for a JTextField accepting an object does not exist. You should try to call the toString() method on that object or do some other form of manipulating to make sure the arguments passed in the constructor are accepted
    I am the luckiest man in the world

  • #3
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts
    like adding blank fields?not entirely sure what you meant by that

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    you are passing a java.lang.Object into the constructor, it accepts a java.lang.String, so you have to change the object-type of your argument
    I am the luckiest man in the world

  • #5
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts
    which means? Just a exmaple line please

  • #6
    New Coder
    Join Date
    Mar 2005
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    String foo = "This is a String object";
    JTextField bar = new JTextField( foo );

  • #7
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i want it to come directly from the container any way to make it string like or something?

  • #8
    New Coder
    Join Date
    Mar 2005
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    either call a method that returns a string, cast the Object to string, or call the .toString() method (which may or may not give you what you are looking for)


  •  

    Posting Permissions

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