Java - 如何用txt文件的某些部分填充组合框

时间:2017-08-28 04:29:40

标签: java combobox text-files jcombobox

我有这样的文本文件:

   Jordan|Turner|jotu|12345|Department of Complaints|Belgrade|
   Travis|Bickle|trabic|54321|Driving Department|Novi Sad|

这样的代码:

    package gui;
    public class ReservationWindow extends JFrame {

        private JComboBox cbDispatcher;
        private JLabel lblDispatcherecer;

        public ReservationWindow() {
            setTitle("Reservations");
            ImageIcon ikonica = new ImageIcon
                    (LoginProzor.class.getResource("/ikonice/faketaxi.png"));
            setIconImage(ikonica.getImage());
            setSize(400, 350);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setLocationRelativeTo(null);
            setResizable(false);
            initGUI();
            initActions();
        }

        private void initActions() {
 //here is everything fine :D
        }

        private void initGUI() {
            MigLayout mig = new MigLayout("wrap 2", "[][]", "[]10[][]10[]");
            setLayout(mig);

            cbDispatcher = new JComboBox();
            lblDispatcher = new JLabel("Dispatcher with whom you talked");

            add(lblDispatcher);
            add(cbDispatcher);

        }
    }

现在,如何使用linse的某些部分填充ComboBox? (在这种情况下,第四部分 - 投诉和驾驶部门)

Apache JMeter Functions - An Introduction

2 个答案:

答案 0 :(得分:3)

如果您在文件中使用public function getCategoryTree($level = 0, $prefix = '') { $rows = $this->db ->select('id,parent_id,name') ->where('parent_id', $level) ->order_by('id','asc') ->get('category') ->result(); $json_response = array(); foreach ($rows as $row) { $row_array = array(); $row_array['text'] = $row->name; $row_array['nodes'] = array(); $newlevel = $row->id; $childs = $this->db ->select('id,parent_id,name') ->where('parent_id', $newlevel) ->order_by('id','asc') ->get('category') ->result(); if(count($childs) > 0){ foreach ($childs as $row) { $row_array['nodes'][] = array( 'text' => $row->name, ); } } array_push($json_response, $row_array); //push the values in the array } return $json_response; } 仅用于分隔符,那么为了简单起见,我建议删除并保留每个新行的每个值,java 8简单的读取文件的方法是:

|

所以现在List<String> list = new ArrayList<>(); try (Stream<String> stream = Files.lines(Paths.get(fileName))) { list = stream.collect(Collectors.toList()); } catch (IOException e) { e.printStackTrace(); } 将包含文件中的所有值。现在,您必须使用list将其循环并添加到JComboBox。您可以使用cbDispatcher.addItem(value)过滤(阅读文件的特定部分)并将其提交给stream.filter

答案 1 :(得分:-1)

将文本文件解析并存储到ArrayList。您可以使用String的split方法。然后 使用ArrayList类的toArray()方法并将其传递给JComboBox

的构造函数

有关详细信息,请参阅JavaDoctutorial