Feb 3, 2018 01:39 PM
Main Class:
Spoiler (Click to View)
myHero Class:
Spoiler (Click to View)
When I add a hero to the list it adds correctly but when I display the list it doesn't print as the 2 strings but as some random characters with the class name.
Picture of what is happening:
i.imgur.com/PpFDf73.png
Spoiler (Click to View)
Code
import java.awt.*;
import java.awt.event.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Iterator;
public class HeroList extends JFrame
{
JButton addHero = new JButton("Add Hero");
JButton removeHero = new JButton("Remove Hero");
JButton displayList = new JButton("Display Heroes");
List<myHero> myList = new LinkedList<>();
public static void main(String [] args)
{
HeroList gui = new HeroList();
gui.setSize(400,150);
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
gui.setTitle("Heroes");
gui.createGUI();
gui.setVisible(true);
}
public void createGUI()
{
Container window = getContentPane();
window.setLayout(new FlowLayout());
addHero.addActionListener(new AddHeroAction());
window.add(addHero);
removeHero.addActionListener(new RemoveHeroAction());
window.add(removeHero);
displayList.addActionListener(new DisplayListAction());
window.add(displayList);
}
private class AddHeroAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String name1 = JOptionPane.showInputDialog("Enter your heroes name:");
String date1 = JOptionPane.showInputDialog("Enter your heroes birthday (dd-mm-yyyy):");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date2 = null;
try
{
date2 = dateFormat.parse(date1);
}
catch (ParseException e)
{
e.printStackTrace();
}
myHero hero = new myHero(name1, date1);
myList.add(hero);
}
}
private class RemoveHeroAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String remove = JOptionPane.showInputDialog("Enter the name of the hero you wish to delete:");
for(Iterator<myHero> iter = myList.iterator(); iter.hasNext();)
{
myHero data = iter.next();
if (data.getName() == null ? remove == null : data.getName().equals(remove))
{
iter.remove();
}
}
}
}
private class DisplayListAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Iterator myIterator = myList.iterator();
String result = "";
while(myIterator.hasNext())
{
result += "\n " + myIterator.next();
}
JOptionPane.showMessageDialog(null, "List: " + result);
}
}
}myHero Class:
Spoiler (Click to View)
Code
public class myHero
{
public String name;
public String date;
public myHero(String name, String date)
{
this.name = name;
this.date = date;
}
public String getName()
{
return name;
}
public String getDate()
{
return date;
}
public void setName(String name)
{
this.name = name;
}
public void setDate(String date)
{
this.date = date;
}
public String toString(String name, String date)
{
return (name + "" + date);
}
}When I add a hero to the list it adds correctly but when I display the list it doesn't print as the 2 strings but as some random characters with the class name.
Picture of what is happening:
i.imgur.com/PpFDf73.png

