PDA

View Full Version : Java Help-Action Listener/File Writing/Arraylist


Kura_kai
04-29-2005, 01:23 AM
I am having problems with this method in my java program.

public void actionPerformed(ActionEvent event)
{
switch(event.getSource())
{
case add:
data.add(name.getText());
data.add(lname.getText());
data.add(phonenum.getText());
b=b+3;
break;
case next:
if (a!=b)
{
a=a+3;
}
cur = ""+data.get(a);
name.setText(cur);
cur = ""+data.get(a+1);
lname.setText(cur);
cur = ""+data.get(a+2);
phonenum.setText(cur);
break;
case back:
if(a!=0)
{
a=a-3;
}
cur = ""+data.get(a);
name.setText(cur);
cur = ""+data.get(a+1);
lname.setText(cur);
cur = ""+data.get(a+2);
phonenum.setText(cur);
break;
case edit:
data(a,name.getText());
FileWriter writer = new FileWriter("foo.txt",false);
for(int i=0;i<=b;i++)
{
cur = data.get(i)+"\n";
writer.write(cur);
}
writer.close();
}
}

It keeps coming up with errors on the switch.
Also i can't figure out how to set data,which is a arraylist, to where at the part a of data to something else.
The next post is the whole file.

Kura_kai
04-29-2005, 01:24 AM
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;
private static ArrayList data;
String cur;
private static 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.
cur = ""+data.get(0);
name = new JTextField(cur);
cur = ""+data.get(1);
lname = new JTextField(cur);
cur = ""+data.get(2);
phonenum = new JTextField(cur);
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);
edit.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)
{
switch(event.getSource())
{
case add:
data.add(name.getText());
data.add(lname.getText());
data.add(phonenum.getText());
b=b+3;
break;
case next:
if (a!=b)
{
a=a+3;
}
cur = ""+data.get(a);
name.setText(cur);
cur = ""+data.get(a+1);
lname.setText(cur);
cur = ""+data.get(a+2);
phonenum.setText(cur);
break;
case back:
if(a!=0)
{
a=a-3;
}
cur = ""+data.get(a);
name.setText(cur);
cur = ""+data.get(a+1);
lname.setText(cur);
cur = ""+data.get(a+2);
phonenum.setText(cur);
break;
case edit:
data(a,name.getText());
FileWriter writer = new FileWriter("foo.txt",false);
for(int i=0;i<=b;i++)
{
cur = data.get(i)+"\n";
writer.write(cur);
}
writer.close();
}
}

private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

Contacts converter = new Contacts();
}

public static 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 static 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);
setB(b++);
}
reader.close();
}
public static void setB(int e)
{
b = e;
}
}

Kura_kai
04-29-2005, 02:34 AM
Figure the problem with the arraylist tried something and it worked. Just now with the switch problem. Is it possible to use a switch for see if a button was pushed using action listener?

Kura_kai
04-29-2005, 02:47 AM
Wow fast...decided to use if statments but still one problem :confused:
I made another method to write to the file but now it says "unreported exception java.lang.Exception; must be caught or declared to be thrown" for where it goes to the method but method actionPerformed can not have a throws exception. Help?