Results 1 to 2 of 2
  1. #1
    mib1bee is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Link one JList to another JList

    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();

    }


    }
    _______________________________________________

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

Similar Threads

  1. Link one JList to another JList
    By mib1bee in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 07:10 PM
  2. Link one JList to another JList
    By mib1bee in forum Advanced Java
    Replies: 1
    Last Post: 12-30-2010, 06:35 PM
  3. JList
    By nawl in forum New To Java
    Replies: 2
    Last Post: 05-21-2010, 06:37 AM
  4. How to use a JList?
    By glhansen in forum New To Java
    Replies: 3
    Last Post: 03-24-2009, 10:27 AM
  5. JList
    By pinks_70986 in forum New To Java
    Replies: 1
    Last Post: 02-12-2009, 08:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •