JTable 的 RowSorter 没有正确排序整数 (1,10,100...2,20...3) [英] RowSorter of JTable not sorting Integers correctly (1,10,100...2,20...3)

查看:41
本文介绍了JTable 的 RowSorter 没有正确排序整数 (1,10,100...2,20...3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Java 还很陌生,我一整天都在过度尝试自己解决问题,但它不起作用.我正在尝试编写一个与 Excel 工作表连接并允许通过 GUI 插入/保存/排序适合电影数据库的数据的 Java 应用程序.这是类中的代码,其中发生了 GUI 魔术和排序:

I'm pretty new to Java and I have tried excessively solving the problem on my own all day, but it doesn't to work. I'm trying to write a Java application that connects with an Excel sheet and allows via GUI to insert/save/sort data suitable for a movie database. Here is the code from the classe, where the GUI-magic and the sorting happens:

    package gui;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import object.Film;
import excel.Excelverbindung;

public class Tabelle extends JTable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    static String[] columnNames = { "Nr.", "Titel", "Jahr", "Regie",
            "Primäres Genre", "Sekundäres Genre", "Wertung", "Originaltitel" };
    static List<Film> rowlist = new ArrayList<Film>();
    static String[] rowData = {};
    JTable table = new JTable();
    Excelverbindung excelverb = new Excelverbindung();

    public Tabelle() {
    }

    private List<Film> getRowData() {
        Excelverbindung excelverb = new Excelverbindung();
        rowlist = excelverb.leseExcel();
        return rowlist;
    }

    public JTable fuellenTabelle() {
        TableModel tableModel = new DefaultTableModel(columnNames, 0);

        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>();
        table.setRowSorter(sorter);

        sorter.setModel(tableModel);
        table.setModel(tableModel);

        Tabelle.rowlist = getRowData();
        String[] film = new String[7];

        for (Film temp : rowlist) {
            film[0] = temp.getNummer();
            film[1] = temp.getName();
            film[2] = temp.getJahr();
            film[3] = temp.getRegie();
            film[4] = temp.getGenre1();
            film[5] = temp.getGenre2();
            film[6] = temp.getWertung();
            ((DefaultTableModel) tableModel).addRow(film);
        }

        System.out.println("Tabelle erzeugt");
        return table;
    }

    public void getRandomFilm() {
        Film film;
        excelverb.oeffneVerbindung();
        film = excelverb.holeZufaelligerFilm();
        excelverb.schließeVerbindung();

        film.getNummer();
        film.getName();
        film.getJahr();
        film.getRegie();
        film.getGenre1();
        film.getGenre2();
        film.getWertung();
        UIManager.put("OptionPane.cancelButtonText", "Ach, egal...");
        UIManager.put("OptionPane.okButtonText", "Nächster!");

        int input = JOptionPane.showOptionDialog(
                null,
                "Nr.:" + film.getNummer() + " Titel: " + film.getName()
                        + " Jahr: " + film.getJahr() + " Genres: "
                        + film.getGenre1() + " und " + film.getGenre2()
                        + " von " + film.getRegie(),
                "Schau einfach den Film...", JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.INFORMATION_MESSAGE, null, null, null);

        if (input == JOptionPane.OK_OPTION) {
            getRandomFilm();
        } else if (input == JOptionPane.CANCEL_OPTION) {
        }
    }
}

RowSorter 似乎无法对我得到的字符串值(数字也是字符串)进行排序.但是即使将字符串转换为整数(我今天将我的进度反转到代码的最后一个可运行"状态,因此是字符串数组)也不能解决它.我遇到了整个 getClass 并将它们设置为 Integer.Class 等等,但无法完全了解如何准确使用它.无论哪种方式,它所做的只是允许基于字符串进行排序 - 而不是单元格内容的数值.

It seems that RowSorter can't sort the String values (nummer is also a String), that I got. But even converting the Strings to Integer (I reversed my progress today to the last "runable" state of the code, hence the String Arrays) doesn't solve it. I came across the whole getClass and setting them to Integer.Class and so forth, but can't wrap my head around how to use it exactly. Either way, all it does is allow a sorting based on the String- not the nummeric value of the cell contents.

非常感谢任何帮助,我阅读的相关主题要么很难理解,要么没有涵盖我的具体问题.

Any help would be greatly appreciated, related topics I read were either very hard for me to follow or didn't cover my specific proble.

所以,请耐心等待,因为英语既不是我的母语,也不是我精通 Java.

So, please be patient since English is neither my native tongue, nor am I very skilled in Java.

非常感谢并致以最诚挚的问候.

Thanks a bunch and best regards.

推荐答案

您需要告诉表格每列中存储的是什么类型的数据,以便使用合适的 Comparator 进行排序.

You need to tell the table what type of data is being stored in each column so the proper Comparator can be used to do the sorting.

您可以通过覆盖 TableModel 的 getColumnClass() 方法(或 JTable,如果您在创建 TableModel 时无权访问它)来实现此目的.类似的东西:

You do this by overriding the getColumnClass() method of your TableModel (or JTable if you don't have access to the TableModel when it is created). Something like:

@Override
public Class getColumnClass(int column)
{
    if (column == 3)
        return Integer.class;
    else
        return String.class;
}

此外,请确保将 Integer 对象存储在 TableModel 中,而不仅仅是整数的字符串表示.

Also, make sure you store Integer objects in the TableModel, not just a String representation of an Integer.

这篇关于JTable 的 RowSorter 没有正确排序整数 (1,10,100...2,20...3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆