I'm trying to implement an AbstractTableModel
for a collection named "clients" but I keep receiving the error "required variable found value" for the "add" method.
Here is my code:
I'm sorry for the confusion created. The add method is meant to add a new client in the table (by that I mean a new row). I don't want to add a new client to the collection.
class ModelTabel extends AbstractTableModel{
public int getRowCount() {
return clients.size();
}
public int getColumnCount() {
return 4;
}
public Object getValueAt(int row, int column) {
Client c = clients.get(row-1);
switch(column){
case 0: return c.getName();
case 1: return c.getSurname();
case 2: return c.getID();
case 3: return c.getPhone();
default:return "ERROR";
}
}
public void add(Client c) {
clients.get(clients.size()++) = a;
fireTableDataChanged();
}
}
I believe this is the same problem as this question... your variable assignment is reversed.
It should be (although this code is still incorrect - see below):
a = clients.get(clients.size()++);
EDIT: this was already answered by Prabhakaran, but apparently people felt the need to downvote my answer.. I think I did address the original question, but I appreciate that my code sample was still incorrect, so I will try to provide a more complete answer:
First of all, as to the "required variable not found" error, if you google it you will see that other SO question as the first hit. clients.get(clients.size()++)
is not a variable, so you can't assign things to it. I am not sure where a
is declared in your code, but assume that it is a variable and thus my suggestion of reversing the assignment.
Next, for the clients.get(clients.size()++)
line, as others have mentioned or alluded to - VAR++
is equivalent to VAR = VAR + 1
, so again is an assignment operation going on. In this case, clients.size()
is not a variable, so you can not increment it. If you wanted the clients.size() + 1
index, you could have written: a = clients.get(clients.size() + 1)
... but that will throw an ArrayIndexOutOfBoundsException
, because you're trying to access an element of clients
beyond its current size!
That is why Prabhakaran rewrote your method as they did, changing that line to a clients.add(c)
call - as it seemed to fit the original intent of the method.
You can't ++
the return value from a method, because the argument of ++
has to be something that is valid on the left hand side of an assignment. If you want to add something to the end of a Collection
then the correct way to do that is to use the add
method.
clients.add(a);
Also, you should fire a more specific modification event than simply "table changed". Calling fireTableDataChanged
essentially tells listeners "the data in this model has changed beyond recognition, throw away your current visual representation and build a completely new one instead". It would be much more efficient and provide a better user experience if instead you used
fireTableRowsInserted(clients.size() - 1, clients.size() - 1);
which specifically says "one new row has been added to the end of this model, but the rest of the data is unchanged".
do like this
change
clients.get(clients.size()++) = a;
to
clients.add(c);
Now your method looks like
public void add(Client c) {
clients.add(c);
fireTableDataChanged();
}
}
Not sure how you getting clients.size(); believing clients Collection present in ModelTabel class.
Just keep it simple. You have an arraylist of clients right?
class ModelTabel extends AbstractTableModel{
ArrayList<Client> clients = new ArrayList<Client>();
public int getRowCount() {
return clients.size();
}
public int getColumnCount() {
return 4;
}
public Object getValueAt(int row, int column) {
Client c = clients.get(row);
switch(column){
case 0: return c.getName();
case 1: return c.getSurname();
case 2: return c.getID();
case 3: return c.getPhone();
default:return "ERROR";
}
}
public void add(Client c) {
clients.add(c);
fireTableDataChanged();
}
}
Please show the full error message, not a paraphrasing of the error. Please show the line of text that causes the error.
Having said that, where is your clients variable? Where is it declared? Initialized? I think that your model needs this to work and to make sense.
It would help if you show how your collection (I'm assuming it's a List? is being declared.
That being said, your add method doesn't make sense: I think you mean:
{
clients.add( c );
fireTableDataChanged()
}