从JList获取多个对象并将它们添加到数组?

时间:2018-02-06 19:00:49

标签: java arrays jlist

我在执行这部分工作时遇到了麻烦。

  

此面板还包含一个JList对象,其中包含以下10所加拿大大学:多伦多,约克,西部,布洛克,圭尔夫,滑铁卢,麦吉尔,康考迪亚,拉瓦尔和马克思。从该列表中,用户将选择3所大学。面板底部显示的标有“提交”的按钮允许用户将来自文本字段和JList对象的输入数据输入到具有最多100个学生对象的数组中。

我为10所大学创建了一个字符串数组。

  String uniNames[] = {"Toronto", "York", "Western", "Brock", 
     "Guelph", "Waterloo", "McGill", "Concordia", "Laval", "Macmaster"};

将字符串数组添加到JList

  JList<String> uniList = new JList<>(uniNames); //(2)
  uniList.setVisibleRowCount(10);
  uniList.setFixedCellHeight(34);
  uniList.setFixedCellWidth(300);
  uniList.setSelectionMode(
     ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  //Add to uniPanelList
  uniPanelList.add(uniList);
  //Add to uniPanel
  uniPanel.add(label3, BorderLayout.NORTH);
  uniPanel.add(uniPanelList, BorderLayout.CENTER);

我创建了“提交”按钮,该按钮用于输入数据

  submitButton.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent submit){
        String name = "", mark = "";
        int markNum;
        if (submit.getSource()==submitButton){ //If submit is pressed
           name = nameInput.getText(); 
           mark = markInput.getText();
           markNum = Integer.parseInt(mark); //Convert String to int


           //copyList.setListData(colorList.getSelectedValues());
           //String objs = uniList.getSelectedValuesList();


           //Add information to Array, count++ to keep track
           stuArray[count++] = new Student (name, markNum);
           nameInput.setText(""); //Resets input area
           markInput.setText(""); //Resets input area
           //Set label under submit button
           outputLabel.setText("Student " + count + 
              " out of 100 submitted.");
        }
     }
  });

我遇到程序必须从我的JList中选择3的部分出现问题,按下Submit并进入数组。我尝试过getSelectedValuesList(),但它返回一个错误。

列表无法转换为String                String objs = uniList.getSelectedValuesList();

我一直试图弄清楚从哪里开始。有什么提示吗?

2 个答案:

答案 0 :(得分:0)

错误消息表明您需要的所有内容:

列表无法转换为字符串

改为:

String objs = uniList.getSelectedValue().toString();

答案 1 :(得分:0)

toArray() function of java.util.List可能是您正在寻找的内容。根据Javadoc:

  

返回一个包含此列表中所有元素的数组   序列

import java.io.*;

public class BytePe1 {
   public static void main(String[] args) {
      try {
         FileInputStream fis = new  FileInputStream("ClassList.dat");
         BufferedInputStream bis = new BufferedInputStream( fis );
         DataInputStream dis = new DataInputStream(bis);

         String studentName;
         int studentNumber;

         //while(dis.readLine() != null) {
            System.out.println("Name");
            System.out.println(dis.readUTF());
            System.out.println(dis.readInt());
            System.out.println(dis.readDouble());
            System.out.println(dis.readDouble());
            System.out.println(dis.readDouble());
            //System.out.println(dis.readUTF());
            //And I would need to repeat these steps above but I don't know how many
            //Files there actually are, so I would like to not just spam this until I see errors
         //}
         dis.close();
      }
      catch(Exception e) {
         System.out.println("Exception: " + e.getMessage());
      }
   }
}