PDA

View Full Version : Can anyone explain this in simple coding?


cranford07
02-22-2006, 04:04 AM
Can anyone state this coding without all the applet stuff and still make it work?

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;

import java.util.*;
import java.io.*;

public class ContactProgram implements ActionListener{

public static void main(String[] args) {
new ContactProgram();
}

public ContactProgram(){

loadFile();
makeFrame();

}

public void loadFile(){

if(!(new File("Contacts.data").exists())){
contacts = new Contacts();
} else {

try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Contacts.data"));
contacts = (Contacts)ois.readObject();
ois.close();

System.out.println("Contacts Loaded: " + (contacts.people.size()) + " Entries");

} catch(Exception e){
contacts = new Contacts();
e.printStackTrace();
}

}

}

public void makeFrame(){

namelist.addActionListener(this);
name.addActionListener(this);
number.addActionListener(this);
saveinfo.addActionListener(this);
savenew.addActionListener(this);
savefile.addActionListener(this);

panel.setBackground(Color.white);
//panel.setLayout(new FlowLayout());

panel.add(namelist);
panel.add(labelName);
panel.add(name);
panel.add(labelNumber);
panel.add(number);
panel.add(saveinfo);
panel.add(savenew);
panel.add(savefile);

frame.setContentPane(panel);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == savenew){
saveNew();
}

if(e.getSource() == savefile){
saveFile();
}

}

public void updateNameList(){

if(contacts.people.size() == 0){
namelist.setSelectedIndex(-1);
} else {

for(int i = 0; i < contacts.people.size(); i++){
Person person = (Person)contacts.people.get(i);
names.add(person.name);
}

namelist.setSelectedIndex(contacts.people.size()-1);

}

}

public void saveNew(){

try{

Person person = new Person();
person.name = name.getText();
person.number = number.getText();

contacts.people.add(person);
updateNameList();

} catch(Exception e){
System.out.println("Error: Contact Info not Added");
e.printStackTrace();
}

}

public void saveFile(){

try{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Contacts.data"));
oos.writeObject(contacts);
oos.close();

} catch(Exception e){
System.out.println("Error: File Not Saved");
e.printStackTrace();
}


}


JFrame frame = new JFrame("Contact Program");
JPanel panel = new JPanel();

Contacts contacts;

Vector names = new Vector(10);
JComboBox namelist = new JComboBox(names);

JLabel labelName = new JLabel("<html><b>NAME:</b></html>");
JTextField name = new JTextField("ENTER NAME");

JLabel labelNumber = new JLabel("<html><b>NUMBER:</b></html>");
JTextField number = new JTextField("ENTER PHONE NUMBER");

JButton saveinfo = new JButton("Save Info");
JButton savenew = new JButton("Save New");
JButton savefile = new JButton("Save File");

}

/*

// SAVE AND COMPILE THIS FILE SEPARATELY

import java.util.*;
import java.io.*;

public class Contacts implements Serializable{

ArrayList people = new ArrayList(10);

}

// SAVE AND COMPILE THIS FILE SEPARATELY

import java.util.*;
import java.io.*;

public class Person implements Serializable{

String name = "";
String number = "";

}

*/