Hi, I have a slight well big problem with my program;
heres a quick insight, I need to create an application which will let users Add a new recipe, which consists of ingredients. Recepie is a LinkedList of Ingredient Objects.
I have one JList which displays a string of the names of each recepie, and the other JList displays the ingredients of that recepie depending on what is selected in the first JList.
The only problem is each time i add a new recepie i have to add an if statement into the code (which isnt really efficient because the user is dumb!)
Any help would be readily welcomed and much appreciated!
Heres my code so far:
public class Ingredient implements Cloneable{ private static int objectID = 001; private int id; String name; private String amount; public Ingredient (String name, String amount){ id = objectID++; this.name = name; this.amount = amount; } public Ingredient (){ id = objectID++; name = "Ingredient"; amount = "000"; } public Ingredient(Ingredient ing){ this.name = ing.name; this.amount = ing.amount; id = objectID++; } public Object clone(){ Ingredient deepCopy = new Ingredient (this); return deepCopy; } public int getID(){ return id; } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAmount(String amount){ this.amount = amount; } public String getAmount(){ return amount; } public String toString(){ return id+" - "+name+" ("+amount+")" +"\n"; } }
import java.awt.List; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import javax.swing.JList; import javax.swing.ListModel; public class Recepie implements Comparable{ private String name; LinkedList <Ingredient> list = new LinkedList <Ingredient> (); public Recepie (String name){ this.name=name; } public String getName (){ return name; } public void addnew(Ingredient ing){ list.add(ing); } public void display(){ System.out.println(name); for (int i = 0; i < list.size(); i++) { System.out.print( (Ingredient)list.get(i)); } } //public String display(String s){ //System.out.println(name); //for (int i = 0; i < list.size(); i++) { // s += (Ingredient)list.get(i); //s += "\n"; // } //return s; //} public String toString(){ return name; } public void displayJList (JList display, ArrayList <String> names){ Collections.sort(names); display = new JList(names.toArray()); display.setListData(names.toArray()); } @Override public int compareTo(Object o) { // TODO Auto-generated method stub return 0; } }import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Collections; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.ListModel; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class Gui_Test extends JFrame{ private Recepie recepie = new Recepie("CAKE"); private Recepie recepie2 = new Recepie("CHILLI CONKANI"); private Recepie recepie3 = new Recepie("MEAT & POTATO PIE"); private Ingredient ing1 = new Ingredient( "Butter","200g"); private Ingredient ing2 = new Ingredient( "Sugar","100g"); private Ingredient ing3 = new Ingredient( "Chicken","2"); private Ingredient ing4 = new Ingredient( "Chillie","50g"); private Ingredient ing5 = new Ingredient( "Meat","1kg"); private Ingredient ing6 = new Ingredient( "Mash Potato","500g"); private JList listrec = new JList(); private JPanel panel = new JPanel(); private ArrayList <String> list = new ArrayList(); private String output, output2; public void frame(){ setLayout(null); setSize(500,500); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); panel.setLayout(null); panel.setVisible(true); panel.setBounds(0,0,500,500); add(panel); } public void tester(){ recepie.addnew(ing1); recepie.addnew(ing2); listrec.setBounds(10,10,200,200); listrec.setVisible(true); panel.add(listrec); repaint(); panel.repaint(); list.add(recepie.getName()); list.add(recepie2.getName()); list.add(recepie3.getName()); Collections.sort(list); //recepie.displayJList(listrec, list); listrec.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listrec.setListData(list.toArray()); listrec.repaint(); final JList listing = new JList(); listing.setBounds(10,220,200,200); listing.setVisible(true); panel.add(listing); final ArrayList<String> ingrlist = new ArrayList<String> (); String s1 = ing1.toString(); String s2 = ing2.toString(); ingrlist.add(s1); ingrlist.add(s2); listing.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listing.setListData(ingrlist.toArray()); listing.repaint(); listrec.addListSelectionListener(new ListSelectionListener() {//Create&add this action Listener to the add button. public void valueChanged(ListSelectionEvent e) { if (listrec.getSelectedIndex() == 0){ ingrlist.removeAll(ingrlist); String s1 = ing1.toString(); String s2 = ing2.toString(); ingrlist.add(s1); ingrlist.add(s2); listing.setListData(ingrlist.toArray()); listing.repaint(); } if (listrec.getSelectedIndex() == 1){ ingrlist.removeAll(ingrlist); String s3 = ing3.toString(); String s4 = ing4.toString(); ingrlist.add(s3); ingrlist.add(s4); listing.setListData(ingrlist.toArray()); listing.repaint(); } if (listrec.getSelectedIndex() == 2){ ingrlist.removeAll(ingrlist); String s5 = ing5.toString(); String s6 = ing6.toString(); ingrlist.add(s5); ingrlist.add(s6); listing.setListData(ingrlist.toArray()); listing.repaint(); } } }); } public static void main (String [] args){ Gui_Test test = new Gui_Test(); test.frame(); test.tester(); } }