javafocuslistener

focusGained and focusLost don't work?


I wanted to make a program that allows the user to add to a file, search for, delete and load from a file animals in a zoo and information about them. So far the buttons delete and search don't work, so pay no attention to them. The problem is that focusGained and focusLost don't seem to work. I tried every solution I found so far, but I couldn't fix them. So, all help is appreciated. If you have any ideas about how to write the Search and Delete classes, please suggest!

This is the main program:

        package GUI_Try;

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


public class GUI_ZOO extends JFrame{

    ArrayList <ANIMAL> animal= new ArrayList<ANIMAL>();
    TextField name,ID,country;
    Button  add = new Button("add"),
            save = new Button("save"),
            //delete = new Button("delete"),
            //search = new Button("search"),
            load = new Button("load");
    String sName="",sID="",sCountry="";
    JPanel pc;
    JScrollPane scrPanName;
    JTextArea text; 


    GUI_ZOO(){
        super("Zoo \"Sofia\" ");
        setBounds(80, 80, 600, 500);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());

        GUI_DELETE delObj = new GUI_DELETE(); 
        GUI_SEARCH searchObj = new GUI_SEARCH();
        GUI_ADD addObj = new GUI_ADD(sName,name,sID,ID,text,animal,sCountry,country);

        pc = new JPanel();
        pc.setLayout(new FlowLayout());
        text = new JTextArea(20,10);

        scrPanName = new JScrollPane(text);

        name = new TextField("Name",15);
        country = new TextField("Country",15);
        ID = new TextField("ID",5);
        text.setEditable(false);

        pc.add(name);
        pc.add(ID);
        pc.add(country);
        pc.add(add);
        pc.add(save);
        pc.add(load);
        pc.add(search);
        pc.add(delete);

        ID.addActionListener(addObj);
        name.addActionListener(addObj);
        country.addActionListener(addObj);
        add.addActionListener(addObj);

        save.addActionListener(new Save());
        load.addActionListener(new Load());
        delete.addActionListener(delObj);
        search.addActionListener(searchObj);

        name.addFocusListener(new Focus()) ;   
        country.addFocusListener(new Focus()) ;  
        ID.addFocusListener(new Focus()) ;  

        add("North",pc);
        add("Center",scrPanName);
        setVisible(true);
    }

    class Focus implements FocusListener{

        public void focusGained (FocusEvent e){
            try{
                TextField src = (TextField)e.getSource();
                if(src.getText().equals("Name") ){
                    name.setText("");
                }
                if(src.getText().equals("ID") ){
                    ID.setText("");
                }
                if(src.getText().equals("Country") ){
                    country.setText("");
                }
            }

            catch(ClassCastException ignored){

            }
        }

        public void focusLost (FocusEvent e){
            try{
                TextField src = (TextField)e.getSource();
                if(src.getText().equals("") ){
                    name.setText("Name");
                }
                if(src.getText().equals("") ){
                    ID.setText("ID");
                }
                if(src.getText().equals("") ){
                    country.setText("Country");
                }
            }

            catch(ClassCastException ignored){

            }
        }
    }


            class Save implements ActionListener{
                public void actionPerformed(ActionEvent e){
                    try{
                        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("zoo.dat"));
                        os.writeObject(animal);
                        os.close();
                    }
                    catch(FileNotFoundException ex){
                        System.out.println("Can't save the file zoo.dat");
                    }
                    catch(IOException ex){}
                }
            }

            class Load implements ActionListener{
                public void actionPerformed(ActionEvent e){
                    try{
                        ObjectInputStream os = new ObjectInputStream(new FileInputStream("zoo.dat"));
                        animal = (ArrayList<ANIMAL>) os.readObject();
                        os.close();
                        text.setText("");
                        for(ANIMAL a:animal){
                            text.append(a.name+"\t"+a.ID+"\t"+a.country+"\n");
                        }
                    }
                    catch(FileNotFoundException ex){
                        System.out.println("Can't find the file zoo.dat");
                    }
                    catch(IOException ex){
                        ex.printStackTrace();//?
                    } 
                    catch (ClassNotFoundException e1) {
                        e1.printStackTrace();//?
                    }
                }
            }

            public static void main(String arg[]){
                new GUI_ZOO();
            }
        }

I divided the program, so the class GUI_ADD would be here:

 package GUI_Try;

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

public class GUI_ADD implements ActionListener{

    String sNamel;
    TextField namel;
    String sIDl;
    TextField IDl;
    String sCountryl;
    TextField countryl;
    JTextArea textl;
    ArrayList <ANIMAL> animall;

    public GUI_ADD(String sName,TextField name,String sNumber,TextField ID,
            JTextArea text,ArrayList <ANIMAL> animal,String sCountry,TextField country){

        sNamel = sName;
        sCountryl = sCountry;
        IDl = ID;
        namel = name;
        sIDl = sNumber;
        countryl = country;
        textl = text;
        animall = animal;
    }


    public void actionPerformed(ActionEvent e){
        sNamel=namel.getText(); 
        sIDl=IDl.getText();
        sCountryl=countryl.getText();
        if((sNamel.length()==0)||(sNamel=="Name")){
            JOptionPane.showMessageDialog(null, "Type a name, please.");
            return;
        }
        if(sIDl.length()==0){
            JOptionPane.showMessageDialog(null, "Type an ID, please.");
            return;
        }
        if(sCountryl.length()==0){
            JOptionPane.showMessageDialog(null, "Type a country of origin, please.");
            return;
        }
        animall.add(new ANIMAL(sNamel,sIDl,sCountryl));
        textl.append(sNamel+"\t"+sIDl+"\t"+sCountryl+"\n");
        namel.setText("");
        IDl.setText("");
        countryl.setText("");
    }
}

And this is the interface:

    package GUI_Try;

    import java.io.*;

    class ANIMAL implements Serializable {
        String name, ID, country;
        public ANIMAL(String name, String ID, String country){
            this.name = name;
            this.ID = ID;
            this.country = country;
        }
    }

Solution

  • You donot need to implements FocusListener in both classes. Its redundant here.

    public class GUI_ZOO extends JFrame implements FocusListener{
    
    class Focus implements FocusListener{
    

    And the reason for FocusGained and FocusLost did not work:

    TextField name,ID,country;
    

    You have used java.awt.TextField,

    JTextField src = (JTextField)e.getSource();
    

    But you are trying to get source from JTextField. In your program you donot have any JTextField's right!.

    Change it to:

    TextField src = (TextField)e.getSource();