Example : Students and Grades

Student - name, grade (integer number) and toString() :

import java.io.*;
public class Student implements Serializable{
    private static final long serialVersionUID = 1L;
    protected String name;
    protected int note;
    public Student(String name, int note){this.name = name;this.note = note;}
    public String toString(){
        return name+"   "+note;
    }
}


Version
1: Creation of a list of students and grades : 

import javax.swing.*;

import java.awt.List;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;

import java.util.ArrayList;

public class Apl_v1 extends JFrame{
    private static final long serialVersionUID = 1L;
    protected JButton ad;
    protected JTextField tf[]= new JTextField[2];
    protected List lst;

    protected JPanel contr, plst;

    protected AdSt adSt;
    protected ArrayList<Student>  prs;

    Apl_v1(int x, int y, int ln, int ht){
        this.setLayout(new BorderLayout());
        this.setBounds(x, y, ln, ht);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Student's directory - version 1");

        contr = new JPanel(new FlowLayout());
        plst = new JPanel(new FlowLayout());

        ad = new JButton("add");
        contr.add(ad);
        tf[0]= new JTextField("name?",10);
        tf[1]= new JTextField("note?",3);
        contr.add(tf[0]);
        contr.add(tf[1]);
        ad.addActionListener(adSt=new AdSt());
        add("North",contr);

        prs=new ArrayList<Student>();
        lst = new List(10);
        plst.add(lst);
        add("Center",plst);     
        revalidate();
    }
    class AdSt implements ActionListener{
        public void actionPerformed(ActionEvent e ){
            Student s;
            int nt;
            String n=tf[0].getText();
            if(n.equals("name?")) {
                tf[1].setText("note?");
                return;
            }
               
            try{
                nt=Integer.parseInt(tf[1].getText());
            }
            catch(NumberFormatException ex){
                tf[1].setText("note?");
                return;
            }
            prs.add( s=new Student(n,nt));
            tf[0].setText("name?");tf[1].setText("note?");
            lst.add(""+s);
            revalidate();
        }
    }
    public static void main(String [] arg){
        new Apl_v1(20,20,400,300);
    }
}


Version  2: add save and load in a file:

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;

public class Apl_v2 extends Apl_v1{
    private static final long serialVersionUID = 1L;
    protected JButton load,save;
    protected Load ld;
    protected Save sv;
    Apl_v2(){
        super(20,20,500,250);
        load = new JButton("load");
        load.addActionListener(ld = new Load());
        contr.add(load);
        save = new JButton("save");
        save.addActionListener(sv=new Save());
        contr.add(save);
        setTitle("Student's directory - version 2");
        revalidate();
    }
    public static void main(String arg[]){
        new Apl_v2();    
    }
    class Save implements ActionListener  {
        public void actionPerformed(ActionEvent e ){
            ObjectOutputStream oos = null;
            try{
                oos = new ObjectOutputStream (
                    new FileOutputStream ("save.ser"));
                oos.writeObject(prs);
            }
            catch (IOException ex){
                System.out.println(ex);
            }
            try{
                if(oos!=null)oos.close();
            }
            catch (IOException ex){}
        }
    }
    class Load implements ActionListener  {
        @SuppressWarnings("unchecked")
        public void actionPerformed(ActionEvent e ){
            ObjectInputStream ios = null;
            try{
                ios = new ObjectInputStream (new FileInputStream ("save.ser"));
                prs= (ArrayList<Student>)ios.readObject();
            }
            catch (Exception ex){
                tf[0].setText("Error");
            }
            try{
                if(ios!=null)ios.close();
            }
            catch (IOException ex){}
            lst. removeAll();
            for(int i=0;i<prs.size();i++){
                lst.add(""+prs.get(i));
            }          
        }      
    }
}



Version 3: Clear text fields to introduce new information, add listener to tf[1 to put new student:

import java.awt.event.*;
public class Apl_v3 extends Apl_v2{
    private static final long serialVersionUID = 1L;
    protected InName in_f;
    protected InNote int_f;
    public static void main(String[] args) {
        new Apl_v3();  
    }
    Apl_v3(){
        tf[0].addFocusListener(in_f=new InName());
        tf[1].addFocusListener(int_f=new InNote());
        tf[1].addActionListener(adSt);
        this.setTitle("Student's directory - version 3");
    }
    class InName implements FocusListener{       
        public void focusGained(FocusEvent e) {
            if (tf[0].getText().equals("name?"))
                tf[0].setText("");
        }
        public void focusLost(FocusEvent e){
            if (tf[0].getText().equals(""))
                tf[0].setText("name?");           
        }       
    }
    class InNote implements FocusListener{       
        public void focusGained(FocusEvent e) {
            if (tf[1].getText().equals("note?"))
                tf[1].setText("");
        }
        public void focusLost(FocusEvent e){
            if (tf[1].getText().equals(""))
                tf[1].setText("note?");           
        }
    }
}