🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.language
4 messages
4 total messages Started by Richard M. Ulrich Wed, 18 May 2005 20:15
Editing elements in an ArrayList
#383
Author: Richard M. Ulrich
Date: Wed, 18 May 2005 20:15
35 lines
2402 bytes
Hello,

I have a JTable whose TableModel stores data in an ArrayList of ArrayLists. 
Perhaps because I am more used to C++ (where I know exactly where my data 
resides), I am having trouble editing cell entries.

Stripped to its essentials, the code to edit an entry is as follows, where 
ds is an instance of an implemented AbstractTableModel

            Element elem = (Element) ds.getObjectAt(gridrow, gridcol);
            eresults.copy(elem.results); 
//puts new results into elem.results
            ds.setValueAt((Object) elem,gridrow,gridcol);

Again stripping out all of the code to handle error checking and such, 
getObjectAt is equivalent to two lines:

            ArrayList aRow = (ArrayList) table.get(gridrow);
            return ( aRow.get(columnIndex) );

and setValueAt to two lines

            ArrayList aRow = (ArrayList) table.get(gridrow);
            aRow.set(gridcol, elem);


Now for the dumb questions.  When I have ds.getObjectAt(gridrow, gridcol), 
am I getting a new copy of the data, or a reference to the existing data? 
When I have ds.setValueAt((Object) elem,gridrow,gridcol), is Java creating a 
new copy of elem, or is the ArrayList now pointing to the local object?  If 
it is the second case, then it would seem like I don't really need to call 
setValueAt, except for the side-effect of calling fireTableCellUpdated().

Thanks,
Richard
Re: Editing elements in an ArrayList
#384
Author: Alexey N. Solofnenko
Date: Wed, 18 May 2005 20:20
47 lines
2741 bytes
Java works with whatever was returned by your table model. If you do not 
want for the value to be copied, do not copy it.

- Alexey.

Richard M. Ulrich wrote:
> Hello,
> 
> I have a JTable whose TableModel stores data in an ArrayList of ArrayLists. 
> Perhaps because I am more used to C++ (where I know exactly where my data 
> resides), I am having trouble editing cell entries.
> 
> Stripped to its essentials, the code to edit an entry is as follows, where 
> ds is an instance of an implemented AbstractTableModel
> 
>             Element elem = (Element) ds.getObjectAt(gridrow, gridcol);
>             eresults.copy(elem.results); 
> //puts new results into elem.results
>             ds.setValueAt((Object) elem,gridrow,gridcol);
> 
> Again stripping out all of the code to handle error checking and such, 
> getObjectAt is equivalent to two lines:
> 
>             ArrayList aRow = (ArrayList) table.get(gridrow);
>             return ( aRow.get(columnIndex) );
> 
> and setValueAt to two lines
> 
>             ArrayList aRow = (ArrayList) table.get(gridrow);
>             aRow.set(gridcol, elem);
> 
> 
> Now for the dumb questions.  When I have ds.getObjectAt(gridrow, gridcol), 
> am I getting a new copy of the data, or a reference to the existing data? 
> When I have ds.setValueAt((Object) elem,gridrow,gridcol), is Java creating a 
> new copy of elem, or is the ArrayList now pointing to the local object?  If 
> it is the second case, then it would seem like I don't really need to call 
> setValueAt, except for the side-effect of calling fireTableCellUpdated().
> 
> Thanks,
> Richard
> 
> 

-- 
Alexey N. Solofnenko {trelony at gmail.com}
home: http://trelony.cjb.net/
Re: Editing elements in an ArrayList
#385
Author: Richard M. Ulrich
Date: Wed, 18 May 2005 20:36
9 lines
1290 bytes
"Alexey N. Solofnenko" <trelony@mail.ru> wrote in message 
news:428ba393$1@newsgroups.borland.com...
> Java works with whatever was returned by your table model. If you do not 
> want for the value to be copied, do not copy it.

I don't wish to be ungrateful, but that is just a tad too cryptic for me to 
understand.

R 
Re: Editing elements in an ArrayList
#386
Author: Alexey N. Solofnenko
Date: Wed, 18 May 2005 21:11
26 lines
1946 bytes
I am sorry. Java works with Objects, ds.getObjectAt() returns an object 
reference from table model. You do not need to call setObjectAt() after 
that (unless this is your way to notify JTable that the value is 
changed, but it is possible to do by executing table.tableChanged(new 
TableModelEvent(this, gridrow, gridrow, gridcol)); ).

- Alexey.

Richard M. Ulrich wrote:
> "Alexey N. Solofnenko" <trelony@mail.ru> wrote in message 
> news:428ba393$1@newsgroups.borland.com...
> 
>>Java works with whatever was returned by your table model. If you do not 
>>want for the value to be copied, do not copy it.
> 
> 
> I don't wish to be ungrateful, but that is just a tad too cryptic for me to 
> understand.
> 
> R 
> 
> 

-- 
Alexey N. Solofnenko {trelony at gmail.com}
home: http://trelony.cjb.net/
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads