如何根据JComboBox选择项对JTable进行排序

时间:2018-05-31 00:14:15

标签: java jtable text-files jcombobox

我有这样的java代码:

private ArrayList<Room> loadRoom() {
            ArrayList<Room> rooms = new ArrayList<Room>();
            try {
                File roomsFile = new File("src/txt/rooms");
                BufferedReader br = new BufferedReader(new FileReader(roomsFile));
                String line = null;
                while ((line = br.readLine()) != null) {
                    String[] split = line.split("\\|");
                    int number = Integer.parseInt(split[0]);
                    String type = split[1];
                    String name = split[2];
                    int beds = Integer.parseInt(split[3]);
                    Boolean tv = Boolean.parseBoolean(split[4]);
                    Boolean miniBar = Boolean.parseBoolean(split[5]);
                    Boolean ocupied = Boolean.parseBoolean(split[6]);
                    Boolean deleted = Boolean.parseBoolean(split[7]);
                    Room newRoom = new Room(number, type, name, beds, tv, miniBar, ocupied, deleted);
                    rooms.add(newRoom);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return rooms;

    }

    private void initGUI() {

        tbToolBar = new JToolBar();
        btnAdd = new JButton();
        tbToolBar.add(btnAdd);

        spScroll = new JScrollPane();

        lblSort = new JLabel("Sort by ");
        cbSort = new JComboBox();
        btnSort = new JButton("Sort");
        cbSort.addItem("Number");
        cbSort.addItem("Type");

        tbToolBar.add(lblSort);
        tbToolBar.add(cbSort);
        tbToolBar.add(btnSort);

        ArrayList<Room> rooms = loadRoom();
        String[] header = new String[] { "Number", "Type", "Name", "No. beds", "TV", "Mini bar", "Occupation" };
        Object[][] show = new Object[rooms.size()][header.length];
        for (int i = 0; i < rooms.size(); i++) {
            Room r = rooms.get(i);
            show[i][0] = r.getNumber();
            show[i][1] = r.getType();
            show[i][2] = r.getName();
            show[i][3] = r.getBeds();
            show[i][4] = r.getTv().toString();
            show[i][5] = r.getMiniBar().toString();
            show[i][6] = r.getOcupied().toString();
            // show[i][7] = r.getDeleted();
        }

        DefaultTableModel tableModel = new DefaultTableModel(show, header);
        tblRooms = new JTable(tableModel);
        tblRooms.setRowSelectionAllowed(true);
        tblRooms.setColumnSelectionAllowed(true);
        tblRooms.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        tblRooms.setDefaultEditor(Object.class, null);

        JScrollPane tableScroll = new JScrollPane(tblRooms);
        add(spScroll);
        add(tbToolBar, BorderLayout.NORTH);
        add(tableScroll, BorderLayout.CENTER);



    }

    private void initAction() {
        btnAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RoomAddWindow addRoom = new RoomAddWindow();
                addRoom.setVisible(true);
            }
        });;
    }

和这样的文本文件:

13|family room|name apartman|4|true|true|empty|false
16|superior room|super room|2|true|false|empty|false
15|room|room for one|1|false|false|full|false

文本文件在JTable中按照文本文件的顺序正常显示。但我有JComboBox有两个过滤器:Number(房间号码 - 文本文件中的split [0])和Room Type split [2]。

问题是: 通过其中一个参数对JTable进行排序的最简单方法是什么?

修改  我想使用tblRooms.setAutoCreateRowSorter(true);,但是profesor说他希望我们以更难的方式做到这一点,你只能通过这两个参数进行排序。 :d 此外,即使我尝试它,数字只按第一个数字排序。 例如,如果我想按降序排序,它将按如下方式排序: 41 21 15 13 1234 123 12 0 代替: 1234 123 41 21 15 13 12 0

0 个答案:

没有答案