Results 1 to 3 of 3

Thread: Collections

  1. #1
    tfunk92 is offline Member
    Join Date
    Oct 2013
    Rep Power

    Default Collections

    Basically i have a challange to create a jlist and display elements in a collection within it. I am Pretty new to java and have searched everywhere for help with this as it is not a guided course apart from some documentation. Ta.

    Here is the documention:

    The purpose of this exercise is to create a chooser such as the SchoolChooser and DepartmentChooser that are required for the coursework. We can illustrate the approach using the example of SchoolChooser.

    The essence of the idea is that

    a SchoolChooser would be created and displayed by the GUI and,
    when the user selected a school from the list, the SchoolChooser would return the corresponding School object.
    However, SchoolChooser would have to run asynchronously; i.e. we cannot call a method on SchoolChooser that would return a value. Consequently we need a different approach: the callback mechanism (as described at 3 below).

    Add a new class SchoolChooser and make it a sub-class of JPanel.

    Thinking ahead, we realize two requirements.

    When the user selects a school from the list, the SchoolChooser is going to have to supply the corresponding School object, so we need an attribute that is an array of all the School objects
    private School[] schools;
    We need to know which object has invoked the SchoolChooser and is to be told which school the user selects. The usual mechanism is called callback so the corresponding attribute might be declared
    private Object callback;

    Therefore, the SchoolChooser constructor must have the callback as a parameter.

    In the constructor, because SchoolChooser is a sub-class (of JPanel) the first thing to do is to invoke the constructor of the super-class, i.e.


    We can then complete the constructor by storing the parameter and setting up the JList.

    Get the collection of School objects and save it.

    You can then apply the Collection.size() method for the sizes of schools (the array of School objects) and for the array of strings that will be used to construct the JList.

    I could not get the Collection.toArray() method to work but had no trouble with the iterator.

    Iterate through the collection and, for each entry, store the school in its array and the school's name in a string array.

    Now create the JList using the string array.

    We want to be sure that the user can select just one school from the list so set the selection mode to SINGLE_SELECTION.

    We could add the JList to the SchoolChooser at this point. However, we do not know how many school might be in the list so it would be well first to put the JList in a scroll pane. The two steps can be combined by

    add(new JScrollPane(list));

    This would be a good point at which to test what you have already achieved. I recommend your creating a new class for testing SchoolChooser.

    Your test class should

    create a window,
    create a SchoolChooser object and pass itself (the test class object) as the callback parameter,
    add the SchoolChooser to the window and

    display the window.

    The next stage is to add a selection listener to the JList.

    Which class is going to listen for the selection event? That class has to be able to get the corresponding School object.

    The other thing it has to do is tell its callback by invoking some method on the callback object, e.g.



    The Schools class:

    import java.util.*;
    * Each instance of class School represents an University School that
    * has a number of departments/job roles available that new staff
    * may be registered with. A school is identified by a school code.
    * The class holds the collection of all academic schools.
    * @author Rod Fretwell (modified by Paul Trundle)
    * @version Septmeber 2011
    public class School {
    // class attribute
    private static Map<String,School> allSchools
    = new TreeMap<String,School>();
    // instance variables
    private String code;
    private String name;
    private Map<String,Department> set; // list of departments/job roles within this school

    * Creates a School object with the given attributes and adds that
    * object to the collection of schools provided that there is not
    * already a school recorded with the given school code. In that
    * case the return value is null.
    * A new School record is not created if there is already a record with
    * the given school code. In that case the method returns the
    * previously stored School object for the user's information.
    * @param code the school code
    * @param name the name of the school
    * @return null for a new school, otherwise previously stored
    * School object for the given department code
    public static School add(String code, String name) {
    School retValue = allSchools.get(code);
    if (retValue==null)
    allSchools.put(code, new School(code, name));
    return retValue;

    private School(String code, String name) {
    this.code = code; = name;
    set = new TreeMap<String,Department>();

    * Returns the school for a given code or null if there is no
    * school for the code
    * @param code the school code
    * @return the school for the given code or null if there is no such
    * school
    public static School get(String code) { return allSchools.get(code); }

    * Returns the collection of all schools.
    * The collection's iterator returns the schools in ascending order
    * of school code.
    * @return the collection of all schools
    public static Collection<School> getCollection() {
    return allSchools.values();

    * Returns the code for the school
    * @return the code for the school
    public String getCode() { return code; }

    * Returns the name of the school
    * @return the name of the school
    public String getName() { return name; }

    * Adds a Department object to the collection of departments/job roles available
    * for new staff members within the school
    * @param course the Department to be added
    public void add(Department dept) {

    * Returns the collection of all departments/job roles within the school.
    * The collection's iterator returns the departments/job roles in ascending order
    * of department/job role code.
    * @return the collection of all departments/job roles offered by the school
    public Collection<Department> getDepartments() { return set.values(); }

    Cheers. Im Really confused.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Collections

    When posting code please wrap it in [code] tags [/code], unformatted code is hard to read.

    Since that's a rather long set of requirements, break it down into smaller and simpler elements.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Rep Power

    Default Re: Collections

    The assignment has given you a structure for an application and is asking you to supply the relevant code. If we fill it all in for you, you will not learn. Try it one step at a time. The comments are directly above the stub function. If I were you I'd start by writing the "school" class, then create an instance of it in the main function and run it.

Similar Threads

  1. Collections
    By in forum New To Java
    Replies: 9
    Last Post: 08-20-2013, 01:32 PM
  2. Collections
    By NatureFreshMilk in forum New To Java
    Replies: 12
    Last Post: 05-23-2011, 08:12 AM
  3. Collections help.
    By YoungJavaBoy in forum New To Java
    Replies: 3
    Last Post: 03-14-2011, 11:14 PM
  4. Help collections
    By nikosv in forum New To Java
    Replies: 8
    Last Post: 12-13-2010, 05:29 PM
  5. Collections
    By Cbani in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 02:46 PM

Posting Permissions

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