Results 1 to 3 of 3
  1. #1
    D.Calladine is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default Collections, Collection From one class to another

    Hi, im trying to get a collection from one class Department to another Class DeptChooser, so i can put it into an array into a JList and then display it i can get it into a Jlist and display but can't get from Department class into DeptChooser.

    [code]
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.JFrame;
    import java.util.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    /**
    * allows user to choose from a variety of different departments.
    *
    * @author (Danny Calladine)
    * @version (1.0)
    */
    public class DeptChooser extends JPanel implements ActionListener
    {
    private Department[] dept;
    private JList deptlist;
    private JList courlist;
    private ChooserListener callback;
    GridBagConstraints Radio = new GridBagConstraints();
    GridBagConstraints Buttons = new GridBagConstraints();
    GridBagConstraints DeptPanel = new GridBagConstraints();
    GridBagConstraints InputPanel = new GridBagConstraints();
    GridBagConstraints CourPanel = new GridBagConstraints();
    JPanel panel = new JPanel(new GridBagLayout());
    JPanel inputPanel = new JPanel();
    JPanel selectPanel = new JPanel();

    DeptChooser(){
    super();

    JFrame frame = new JFrame("Dept");
    frame.setSize(560, 350);
    frame.setVisible(true);
    frame.add(panel);
    inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));

    Radio.gridx = 0;
    Radio.gridy = 1;
    Radio.weightx = 0.5;
    Buttons.gridx = 0;
    Buttons.gridy = 2;
    Buttons.weightx = 0.5;
    DeptPanel.gridx = 3;
    DeptPanel.gridy = 0;
    DeptPanel.weightx = 0.5;
    CourPanel.gridx = 3;
    CourPanel.gridy = 1;
    CourPanel.weightx = 0.5;
    InputPanel.gridx = 0;
    InputPanel.gridy = 0;
    InputPanel.weightx = 0.5;

    collection.toArray(Department[0]);
    JList deptList = new JList(dept);
    deptList.setSelectionMode(ListSelectionModel.SINGL E_SELECTION);
    panel.add(deptList, DeptPanel);
    JScrollPane deptListscroll = new JScrollPane(deptList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    panel.add(deptListscroll, DeptPanel);

    String [] courdata = {"Computer Science","Robotics","Internet Computing","Bussiness Computing","Artifical Intelligence"};
    JList courList = new JList(courdata);
    deptList.setSelectionMode(ListSelectionModel.SINGL E_SELECTION);
    panel.add(courList, CourPanel);
    JScrollPane courListscroll = new JScrollPane(courList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    panel.add(courListscroll, CourPanel);

    addField("Name",(new JTextField(20)));
    addField("UoB",(new JTextField(8)));

    panel.add(inputPanel, InputPanel);

    JRadioButton Stage1 = new JRadioButton("Stage 1");
    JRadioButton Stage2 = new JRadioButton("Stage 2");
    JRadioButton Stage3 = new JRadioButton("Stage 3");

    ButtonGroup group = new ButtonGroup();
    group.add(Stage1);
    group.add(Stage2);
    group.add(Stage3);

    JPanel radioPanel = new JPanel(new GridLayout(1, 0));
    radioPanel.add(Stage1);
    radioPanel.add(Stage2);
    radioPanel.add(Stage3);

    radioPanel.setBorder(BorderFactory.createTitledBor der(
    BorderFactory.createEtchedBorder(), "Stages"));

    panel.add(radioPanel, Radio);

    JButton Clear = new JButton("Clear");
    Clear.addActionListener(this);
    JButton Submit = new JButton("Submit");
    Submit.addActionListener(this);

    JPanel Button = new JPanel(new GridLayout(1, 0));

    Button.add(Clear);
    Button.add(Submit);

    panel.add(Button, Buttons);

    }
    public void addField(String label, JComponent field) {
    inputPanel.add(new JLabel(label));
    inputPanel.add(field);
    }
    public void actionPerformed(ActionEvent e) {
    }
    }

    [\code]
    this is the Deptchooser class code

    [code]
    import java.util.*;
    /**
    * Each instance of class Department represents an academic department that
    * offers taught courses. A department is identified by a department code.
    * The Class holds the collection of all academic departmnents
    * @author Rod Fretwell
    * @version October 2008
    */
    public class Department {
    // class attribute
    private static Map<String,Department> allDepts
    = new TreeMap<String,Department>();
    // instance variables
    private String code;
    private String name;
    private Map<String,Course> set; // list of courses offered by the department

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

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

    /**
    * Returns the department for a given code or null if there is no
    * department for the code
    * @param code the department code
    * @return the department for the given code or null if there is no such
    * department
    */
    public static Department get(String code) { return allDepts.get(code); }

    /**
    * Returns the collection of all departments.
    * The collection's iterator returns the departments in ascending order
    * of department code.
    * @return the collection of all departments
    */
    public static Collection<Department> getCollection() {
    return allDepts.values();
    }

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

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

    /**
    * Adds a Course object to the collection of courses offered by the
    * department
    * @param course the Course to be added
    */
    public void add(Course course) {
    set.put(course.getCode(),course);
    }

    /**
    * Returns the collection of all courses offered by the department.
    * The collection's iterator returns the courses in ascending order
    * of course code.
    * @return the collection of all courses offered by the department
    */
    public Collection<Course> getCourses() { return set.values(); }
    }
    [\code]

    there is a loader class that loads some department into it.

    im just wondering how i can get the collection and move it into an array in the deptchooser class so that i can then add it into a Jlist.

    Thank you
    Last edited by D.Calladine; 11-23-2010 at 11:44 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please edit your post to add well-formed code tags. The tags for this forum are

    [cod&#101;] -- above code block

    [/cod&#101;] -- below code block

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,957
    Rep Power
    19

    Default

    You have the slash the wrong way round...

    Anyway, there is a useful method in Collection you might want to use to get an array.

Similar Threads

  1. Collections, Collection From one class to another
    By D.Calladine in forum New To Java
    Replies: 2
    Last Post: 11-16-2010, 03:39 PM
  2. collection class in java
    By katturv in forum New To Java
    Replies: 5
    Last Post: 10-24-2010, 07:09 PM
  3. Java Collection class & methods
    By box2box in forum New To Java
    Replies: 2
    Last Post: 05-26-2010, 01:51 AM
  4. Collections
    By Cbani in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 02:46 PM
  5. know the class coming under the collection api in java
    By javastuden in forum New To Java
    Replies: 1
    Last Post: 11-24-2009, 07:10 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
  •