Q: How do I keep track of which ArrayList
element belongs to which element in a JTable
for when I later have to put it all together into one single ArrayList
?
DETAILED:
I have an interface Media
, and 3 classes implementing it: Book
, Movie
, Album
. Album
has an ArrayList
of Songs
(also a class I made, obviously).
I made a JTable
as per usual with Object[][]
where I can put in String
s, int
s and such for the time being and the user can add rows, remove rows, edit the cells, and then later I planned on going through the data and making each thing specified in the JTable
and putting them into an ArrayList
called ListOne of type Media
so I can use ObjectOutputStream
and save the List
(yes, this is a demand in the assignment, I would've kept it simple personally).
But the problem is Songs
are an ArrayList
in the class Album
, and so I made another JFrame
with a JTable
where one can similarly fill that one out, and then I thought I would store all of the songs the user provides into a temporary Album
-object. But where do I go from here? I can't keep track of them like this if the user deletes the corresponding Album
in the first JTable
.
If I store these Album
s in an ArrayList
called ListTwo, and if the user decides to delete no.6 in ListOne, and no.6 happens to be an Album
, I have to know which one to delete in ListTwo now and make sure the others' ID or key isn't affected as a result because I will need to pair them properly later. I need to know which Album
's songs in ListTwo to transfer to which of the Album
s in ListOne so I can save it later.
So how would I go about keeping track of this and connecting them later?
Preliminary code:
Media interface class:
public interface Media {
void setAuthor(String author);
String getAuthor();
}
Book class:
public class Book implements Media {
private String author;
Book(){
this.author = "";
}
@Override
public void setAuthor(String author) {
this.author = author;
}
@Override
public String getAuthor() {
return this.author;
}
}
CD(Album) class:
import java.util.ArrayList;
public class CD implements Media {
public class Song {
private int seconds;
private String songName;
private String artist;
Song(String name, int seconds, String artist){
this.songName = name;
this.seconds = seconds;
this.artist = artist;
}
public String getName(){
return this.songName;
}
public void setName(String name){
this.songName = name;
}
}
private ArrayList<Song> songs;
private String artist;
CD(){
songs = new ArrayList<Song>();
this.artist = "";
}
public void addSong(String name, int seconds, String artist){
this.songs.add(new Song(name, seconds, artist));
}
public void removeSong(int position){
this.songs.remove(position);
}
@Override
public void setAuthor(String author){
this.artist = author;
}
@Override
public String getAuthor(){
return this.artist;
}
}
Movie class:
public class Movie implements Media {
private String producer;
Movie(){
this.producer = "";
}
@Override
public void setAuthor(String author){
this.producer = author;
}
@Override
public String getAuthor(){
return this.producer;
}
}
TABLE: http://pastebin.com/ETJGuQhD