When trying to identify if duplicates exist inside of the arrayList it doesn't seem to check, I am trying to override equals but the statement will just always pass as false and add the object to the list. In the action listener what is wrong with my statement?
The issue is in the action listener statement.
public class addTrackLayout extends JFrame{
private JTextField artist;
private JTextField trackTitle;
private JTextField genre;
private JTextField duration;
private JTextField year;
private JLabel artistLabel;
private JLabel titleLabel;
private JLabel genreLabel;
private JLabel durationLabel;
private JLabel yearLabel;
private JLabel addLabel;
private String addArtist;
private String addTitle;
private String addGenre;
private double addDuration;
private int addYear;
private static final long serialVersionUID = 1L;
public addTrackLayout(String title){
super (title);
JPanel contentPane = new JPanel();
contentPane.setLayout(new GridBagLayout());
addLabel = new JLabel("Add Record");
artistLabel = new JLabel("Artist Name:");
titleLabel = new JLabel("Track Title:");
genreLabel = new JLabel("Music Genre:");
durationLabel = new JLabel("Duration:");
yearLabel = new JLabel("Release Year:");
artist = new JTextField(15);
trackTitle = new JTextField(15);
genre = new JTextField(15);
duration = new JTextField(15);
year = new JTextField(15);
ArrayList<Music> music = TimelineLayout.music;
JButton addButton = new JButton("Add");
addButton.setActionCommand("add");
//Actions for buttons.
addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
addArtist = artist.getText();
addTitle = trackTitle.getText();
addGenre = genre.getText();
addDuration = Double.parseDouble(duration.getText());
addYear = Integer.parseInt(year.getText());
Music m = new Music(addArtist, addTitle, addGenre, addDuration, addYear);
if(music.contains(m)){
JOptionPane.showMessageDialog(null, "already exists");
}
else{
music.add(m);
JOptionPane.showMessageDialog(null,"added to db");
}
}
});
Music Class:
public class Music {
private String artist;
private String title;
private String genre;
private double duration;
private int year;
public Music(String initArtist, String initTitle, String initGenre, double initDuration, int initYear)
{
artist = initArtist;
title= initTitle;
genre = initGenre;
duration = initDuration;
year = initYear;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getDuration() {
return duration;
}
public void setDuration(double duration) {
this.duration = duration;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
Override and hash.
public boolean equals(Object obj) {
if (this == obj)
{
return true;
}
else if (obj == null)
{
return false;
}
else if (obj instanceof Music)
{
Music music = (Music) obj;
if (music.getArtist().equals(this.getArtist()) && (music.getTitle().equals(this.getTitle())))
{
return true;
}
}
return false;
}
@Override
public int hashCode()
{
int hash = 7;
hash = 31 * hash + Objects.hashCode(this.getArtist());
hash = 31 * hash + Objects.hashCode(this.getTitle());
return hash;
}