简体   繁体   English

jTable。 来自文本文件的row sorting()

[英]jTable. row sorting() from text file

I have a problem regarding sorting in jTable1 i looked at few already asked questions about this problem in this forum but no answer seem to work for me, how can i set up numerical sequence that is working in a regular order instead of going 1, 10, 11...2, 20, 21,... 3,.. and etc. How can i modify this piece of code jTable1.setAutoCreateRowSorter(true); 我在jTable1有一个排序问题,在这个论坛上我已经看过一些关于此问题的已经问过的问题,但是似乎没有答案适合我,我如何设置按常规顺序工作的数字序列而不是进行1,10 ,11 ... jTable1.setAutoCreateRowSorter(true); ,... 3等。如何修改这段代码jTable1.setAutoCreateRowSorter(true); since it doesn't seem to be working properly. 因为它似乎无法正常工作。 Here is the current code I am using. 这是我正在使用的当前代码。

try {
        FileReader fR = new FileReader("table.txt");
       BufferedReader br = new BufferedReader(fR);
        String firstLine = br.readLine().trim();
        String[] columnsName = firstLine.split(",");
        DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
        model.setColumnIdentifiers(columnsName);
        Object[] tableLines = br.lines().toArray();
        for(int i = 0; i < tableLines.length; i++)
        {
            String line = tableLines[i].toString().trim();
            String[] dataRow = line.split("/");
            model.addRow(dataRow);

        }
                    jTable1.setAutoCreateRowSorter(true);

    } catch (IOException ex) {
        Logger.getLogger(NewJFrame3.class.getName()).log(Level.SEVERE, null, ex);
    } 

@MadProgrammer left a comment that is the correct answer. @MadProgrammer留下的评论是正确的答案。 You need to insert a row of numerical values (ie - Integer[], Float[], Double[], etc.) as opposed to the String[] you're using right now. 您需要插入一行数值(即-Integer [],Float [],Double []等),而不是当前使用的String []。

For an example on how to make this conversion, here's how to convert a String[] into an Integer[] using some of your code as a starting point 有关如何进行此转换的示例,以下是使用一些代码作为起点将String []转换为Integer []的方法

String line = tableLines[i].toString().trim();
String[] dataRow = line.split("/");
int dataRowLength = dataRow.length;
Integer[] dataRowIntegers = new Integer[dataRowLength];
for(int i = 0; i < dataRowLength; i++){
    dataRowIntegers[i] = Integer.valueOf(dataRow[i]);
}
model.addRow(dataRowIntegers);

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不能从jtable中删除重复项。 - Cant remove duplicates from jtable. 从JTable的另一个类传递LinkedList。 自动更新表格 - Pass LinkedList from another class for JTable. Automatically update table JTable中。 打印所选行的值,如果所选行为空,则提示错误 - JTable. Printing values of selected row and prompt an error if the selected row is null JTable 排序 - 选择一行 - JTable Sorting - Selecting a row JTable中的分页与行排序 - Pagination in JTable with row sorting 从JTable写入文本文件,从文本文件读取到JTable - Writing text file from JTable, and Reading from text file to JTable 从我的数据库中获取信息并将它们写入 JTable。 错误:java.lang.IndexOutOfBoundsException:索引:2,大小:2 - Getting the information from my database and write them in a JTable. Error: java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 对其进行排序后,无法从JTable获取正确的行(Swing) - Not able to get the correct row from a JTable after Sorting it (Swing) 将AbstractTableModel实现到JTable。 如何添加方法? - Implementing AbstractTableModel to JTable. How to add the methods? 删除我的JTable上的数据。 ArrayIndexOutOfBoundsException:0 - Deleting data on my JTable. ArrayIndexOutOfBoundsException:0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM