带有Arraylist错误的Java Combobox

时间:2015-05-15 01:47:12

标签: java swing combobox

我是使用摇摆和使用Jcombo盒子的新手,我试图创建一个下拉列表,其中列出了火车线路,站点,并且我收到了错误,我感觉我可能正在导入数据进入数组列表错误,但程序吐出火车线阵列,到控制台,但我已注释掉println命令,因为我现在使用GUI。

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.ArrayList cannot be cast to javax.swing.ComboBoxModel
at TouchOn.setPanels(TouchOn.java:63)
at TouchOn.<init>(TouchOn.java:52)
at GUI$2.actionPerformed(GUI.java:51)

我从以下代码收到以上错误。

import javax.swing.*;

import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class TouchOn extends JDialog {
private JPanel mainPanel;


public ArrayList Reader() {
try {

    ArrayList<String> Trains = new ArrayList<String>();
    int count = 0;
    String testing = "";
    File file = new File("Trainlines.txt");
    FileReader fileReader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    StringBuffer stringBuffer = new StringBuffer();
    String line;
    while ((line = bufferedReader.readLine()) != null) 
    {
        stringBuffer.append(line);
        count += count;
        Trains.add(line + "\n");
        stringBuffer.append("\n");



    }
    fileReader.close();
    //Arrays.asList(Trains).stream().forEach(s -> System.out.println(s));
    return Trains;
} catch (IOException e) {
    e.printStackTrace();
}
//return toString();
return null;


}


public TouchOn()
{
    setPanels();

    setModalityType(ModalityType.APPLICATION_MODAL);
    setSize(200, 200);
    setVisible(true);
}
public void setPanels()
{
    mainPanel = new JPanel(new GridLayout(0, 2));
    JPanel containerPanel = new JPanel(new GridLayout(0, 1));
    ArrayList stations = Reader();
    JComboBox<ArrayList> cb = new JComboBox<ArrayList>((ComboBoxModel<ArrayList>) stations);
    JPanel lowerPanel = new JPanel(new FlowLayout());
    JButton apply = new JButton("Touch on ?");
    JButton cancel = new JButton("Cancel");
    cancel.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            dispose();
        }
    });
    apply.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("HellO");
        }
    });

    JLabel touchOnDate = new JLabel("Date: ");
    JLabel touchOnTimehr = new JLabel("Time Hour: ");
    JLabel touchOnTimem = new JLabel("Time Minute:");
    JLabel station = new JLabel("Station: ");

    JTextField touchOnFieldDate = new JTextField();
    JTextField touchOnTimeFieldhour = new JTextField();
    JTextField touchOnTimeFieldminute = new JTextField();
    //JTextField touchOnStation = new JTextField();

     cb.setVisible(true);

    mainPanel.add(touchOnDate);
    mainPanel.add(touchOnFieldDate);
    mainPanel.add(touchOnTimehr);
    mainPanel.add(touchOnTimeFieldhour);
    mainPanel.add(touchOnTimem);
    mainPanel.add(touchOnTimeFieldminute);
    mainPanel.add(station);
    mainPanel.add(cb);
    //mainPanel.add(touchOnStation);
    lowerPanel.add(apply);
    lowerPanel.add(cancel);
    touchOnTimeFieldhour.setSize(10,10);
    containerPanel.add(mainPanel);
    containerPanel.add(lowerPanel);

    add(containerPanel);
}

}

善待我是新的,任何阅读的文章或教程都将受到高度赞赏,学习java是相当困难的。

1 个答案:

答案 0 :(得分:1)

不应该(ComboBoxModel<ArrayList>) stationsstations吗?似乎不需要演员

请注意......

ArrayList stations = Reader();
JComboBox<ArrayList> cb = new JComboBox<ArrayList>((ComboBoxModel<ArrayList>) stations);

应该更像......

ArrayList<String> stations = Reader();
JComboBox<String> cb = new JComboBox<>();
for (String value : stations) {
    cb.addItem(value);
}

ArrayList<String> stations = Reader();
JComboBox<String> cb = new JComboBox<>(stations.toArray(new String[stations.size()]));

如果你感到懒惰......

仔细查看How to Use Combo Boxes了解更多详情