Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Linking two JLists

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Linking two JLists

    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();
     
            }
     
     
    }
    Last edited by helloworld922; December 30th, 2010 at 02:05 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Linking two JLists

    Please don't post the same question under multiple categories. Also, in the future please surround your code with highlight tags. I've modified your posts accordingly this time.

    I'm a little confused as to what it is you're question is. However, I'm guessing you're talking about the ListSelectionListener. Here are a few notes that should help:

    1. Recepie is actually spelled recipe
    2. You really should be initializing everything you need to create your Gui_Tester JFrame inside of the Gui_Tester constructor rather than having to call both the frame() and tester() methods from your main method.
    3. If you're going to use "hard-coded" recipes, then you're right, you will need to use if-statements to get the correct matching ingredients. However, I would recommend using a simple collection of recipes. Also, JLists can hold objects, not just Strings so you can put the recipe objects directly into the JList and update the second JList using the selected object.

Similar Threads

  1. Database connection using NetBeans
    By jcc285 in forum Java IDEs
    Replies: 6
    Last Post: June 9th, 2009, 03:23 AM
  2. [SOLVED] How to link two different class?
    By John in forum Object Oriented Programming
    Replies: 11
    Last Post: April 27th, 2009, 02:57 PM