1

Hey guys i doing my assignment and now i have the problem with non editable cells, actually it became editable, but the result of editing didn't set at arraylist, I tried many solution from internet, but it doesn't work. So my work like registration system which get information about guest, and then stored it into csv file. In additional function the program must let display, update, delete and searching function.

I finished all, without update,delete and searching. Can you please looking my code and help me or give the advice, link or something useful.

this is my abstract model:

public class ddispmodel extends AbstractTableModel {
private final String[] columnNames = { "FirstName", "SecondName", "Date of                                                     
birth", "Gender", "Email", "Address", "Number", "Attending","ID" };
private ArrayList<String[]> Data = new ArrayList<String[]>();
private boolean editable;


public void AddCSVData(ArrayList<String[]> DataIn) {
    this.Data = DataIn;
    this.fireTableDataChanged();
}

@Override
public int getColumnCount() {
    return columnNames.length;// length;
}

@Override
public int getRowCount() {
    return Data.size();
}

@Override
public String getColumnName(int col) {
    return columnNames[col];
}

@Override
public Object getValueAt(int row, int col) {
    return Data.get(row)[col];
}
public boolean isCellEditable(int row, int col) { 
    setValueAt(Data, row, col);
    this.fireTableCellUpdated(row, col);
    return true;
}
}

This is part of my main class It is action Listener of menu item witch activate displaying function (I didn't copy all class, because it nearly 1000 lines, but if it necessary, i can submit all code )

  dlog.addActionListener(new ActionListener (){

  public void actionPerformed(ActionEvent e){
      CSVFileDomestic Rd = new CSVFileDomestic();
         ddispmodel ddispm = new ddispmodel();
            ddisp.setModel(ddispm);
            File DataFile = new File("D:\\cdne4\\WorkPlace\\Domestic.csv");
            ArrayList<String[]> Rs2 = Rd.ReadCSVfile(DataFile);
            ddispm.AddCSVData(Rs2);
            System.out.println("Rows: " + ddispm.getRowCount());
            System.out.println("Cols: " + ddispm.getColumnCount());

      cl.show(cp, "dispDomPanel");
  }
  });

and File class which convert date from csv to arraylist

 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
 import java.util.ArrayList;
 import java.util.Arrays;

 public class CSVFileDomestic {
        private final ArrayList<String[]> Rs = new ArrayList<String[]>();
        private String[] OneRow;

        public ArrayList<String[]> ReadCSVfile(File DataFile) {
            try {
                BufferedReader brd = new BufferedReader(new              
            FileReader(DataFile));    
          while (brd.ready()) {
                    String st = brd.readLine();
                    OneRow = st.split(",");
                    Rs.add(OneRow);
                    System.out.println(Arrays.toString(OneRow));
                } 
            } 
            catch (Exception e) {
                String errmsg = e.getMessage();
                System.out.println("File not found:" + errmsg);
            } 
            return Rs;

I am new at Java and this is my first program , please can you explain more easily

mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

2

but the result of editing didn't set at arraylist,

You need to override the setValueAt(...) method of the TableModel to save the data.

It would be something like:

String[] row = data.get(row);
row[column] = value;
this.fireTableCellUpdated(row, col);

Also, the isCellEditable(...) method should NOT do any processing. It simply returns true/false for the given column. If you want all columns editable then it should just be:

public boolean isCellEditable(int row, int col) { 
    //setValueAt(Data, row, col);
    //this.fireTableCellUpdated(row, col);
    return true;
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you for your answer, I tried do like this before, and I received errors like row - is duplicate variable and the method .get() is not applicable for arraylist – Boris Vasilievich Avdeev May 25 '16 at 02:17
  • `row - is duplicate variable` - Yes, that is obviously a mistake on my part. Take the time to understand the concept of the code and give the variable a different name. `the method .get() is not applicable for arraylist` - if you fix the first problem the second problem should go away. Again if "row" is a duplicate variable the compiler doesn't know how to resolve the statement since it doesn't know what the proper data type of "row" is. – camickr May 25 '16 at 03:16
0

I finish this)) and go to do others options. camickr thank you for help, you give me the way, which bring me to answer)) I spend nearly 6 hours for trying to do it and finally I got it. If someone interesting, answer is

public boolean isCellEditable(int row, int col) { 
    return true;
}
@Override
public void setValueAt(Object aValue, int row, int col){
    Data.get(row)[col]= (String) aValue;
    fireTableCellUpdated(row,col);

so I just make new method setValueAT which said me camickr. than I went to Oracle site and read about it, and after try it to do, but it doesn't, because aValue can't be converted from object to String, and finally I initiate aValue as a String, so now it works.However it is not all, it only change arraylist. Needs new method to convert arraylist to csv file. I am working about it now, maybe after I will show it. Sorry for my theory, I am new at java, just learn how it works))