2

I have a problem when storing data (written to a GUI) in a JTable I have already created. The problem is, when I click the add button in the GUI, instead of displaying it in the JTable, it opens another window and displays it there. So it adds, but it doesnt display it to the table. Heres the part of the code that display the info in another window:

import java.lang.Object;
import java.io.*;
import java.util.*;

public class MemberManager {
    File custFile;

    private Member m1 = 
        new Member("M0001", "Mark", "Abela", "13/05/90", "121 Kent Street", "21469432", "99456209");
    private Member m2 = 
        new Member("M0002", "John", "Sant", "25/11/84", "55 Golden Road", "21226932", "79513625");

    private ArrayList<Member> memMemb = new ArrayList<Member>();

    public MemberManager(){
        memMemb.add(m1);
        memMemb.add(m2);
}

    public String[][] getMembers(){
        int pos = 0;

        //Creating the table with the players data
        String data [][] = new String[memMemb.size()][8];

        for (Object obj : memMemb){
            Member mem = (Member)(obj);
            data[pos][0] = mem.getID();
            data[pos][1] = mem.getName();
            data[pos][2] = mem.getSurname();
            data[pos][3] = mem.getDob();
            data[pos][4] = mem.getAddress();
            data[pos][5] = mem.getTel();
            data[pos][6] = mem.getMob();

        }
        return data;
    }

    public void addMember(String id, String name, String surname, String dob, String address, String tel, String mob){
        Member tempMem = new Member();

        tempMem.setID(id);
        tempMem.setName(name);
        tempMem.setSurname(surname);
        tempMem.setDob(dob);
        tempMem.setAddress(address);
        tempMem.setTel(tel);
        tempMem.setMob(mob);

            memMemb.add(tempMem);


           FileInputStream fis = null;
           FileOutputStream fos = null;
        try { 
            fos = new FileOutputStream("Member Data");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(memMemb);
            oos.flush();
        }
         catch(IOException e) {
                e.printStackTrace();
        }
       finally {
            try {
        fos.close();
        }
            catch(IOException e) {
                e.printStackTrace();
            }
        }

        try {
        // read back
        fis = new FileInputStream("Member Data");
         ObjectInputStream ois = new ObjectInputStream(fis);
          Object obj = ois.readObject();
            memMemb = (ArrayList<Member>) obj;
        ArrayList<Member> listFromFile = (ArrayList) obj;
           for (Member member: listFromFile) {
            System.out.println(member.toString());
        }      
    }catch (IOException io){
        System.err.println("Cannot access or write to file");    
    }catch (Exception e){
         e.printStackTrace(); 
        }
     finally { 
         try {
              fis.close();
            }   
            catch (IOException e) {
                e.printStackTrace();
            }             
       }
   }      
}

The JTable im using is the default table model thats in a different package. I used mouseListener on the button to add the data... here's part of the code...:-

    memAddOk.addMouseListener(this);
    memAddExit.addMouseListener(this);
}

public void mouseClicked(MouseEvent e){ 
    if (e.getSource().equals(memAddOk)){
        id = textBox[0].getText();
        name = textBox[1].getText();
        surname = textBox[2].getText();
        dob = textBox[3].getText();
        address = textBox[4].getText();
        tel = textBox[5].getText();
        mob = textBox[6].getText();

        MemberManager tempData = new MemberManager();
        tempData.addMember(id, name, surname, dob, address, tel, mob);
    } else 
    if (e.getSource().equals(memAddExit)) {
        this.dispose();
    }
}

How do I get this code to store in the JTable?

2 Answers2

5

The problem is, when I click the add button in the GUI, instead of displaying it in the JTable, it opens another window and displays it there.

It's just doing what you tell it to do:

public void mouseClicked(MouseEvent e){ 
    if (e.getSource().equals(memAddOk)){
        id = textBox[0].getText();
        // .... etc..


        // !!!! *** Here you create a new MemberManager object *** !!!!
        MemberManager tempData = new MemberManager();


        tempData.addMember(id, name, surname, dob, address, tel, mob);
    } else 
    //... etc..

Solution:

  • Don't create a new MemberManager object in your listener.
  • Instead make sure that your listener class has a reference to the currently displayed MemberManager object so you can add a Patient to it.
  • You often get this reference by passing a MemberManager into the listener's constructor or via a setter method parameter.
  • Again, don't use MouseListeners on JButtons but instead use ActionListeners as that's what they're specifically built for. MouseListeners can cause strange behaviors including but not limited to a button that still works despite being disabled.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
4

1) use SwingWorker or Runnable#Thread for loading value on the background from File IO or Database

2) define JTable and DefaultTableModel once time, start background task for loading data from File or Database, simple example

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319