...

Java Help

Kura_kai
03-22-2005, 02:32 PM
I am having problems with a program i am making the program coding is

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

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));
^

Roelf
03-23-2005, 09:44 AM
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

Kura_kai
03-24-2005, 01:25 AM
like adding blank fields?not entirely sure what you meant by that

Roelf
03-24-2005, 06:27 AM
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

Kura_kai
03-24-2005, 09:49 PM
which means? Just a exmaple line please

hinokata
03-25-2005, 01:01 AM
String foo = "This is a String object";
JTextField bar = new JTextField( foo );

Kura_kai
03-25-2005, 01:12 AM
i want it to come directly from the container any way to make it string like or something?

hinokata
03-25-2005, 02:24 AM
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)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum