[英]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.