所以我使用Netbeans的GUIBuilder创建了一个JTable,它有许多列包含不同类型的数据,并且可以使用比较器对列进行排序。我的问题是,对于双列,当表被排序时,值10.0似乎永远不会进入正确的位置。当按升序排列时,10.0在1.0之后但在2.0之前。除此之外,排序与处理其他双重值有关。
这是我正在运行的。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class OData extends javax.swing.JFrame {
public OData() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
OTable = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("Bundle"); // NOI18N
setTitle(bundle.getString("OsuData.title")); // NOI18N
setName("Form"); // NOI18N
jScrollPane1.setName("jScrollPane1"); // NOI18N
OTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Column1", "Column2", "Column3"
}
));
OTable.setColumnSelectionAllowed(true);
OTable.setName("OTable"); // NOI18N
OTable.getTableHeader().setReorderingAllowed(false);
jScrollPane1.setViewportView(OTable);
OTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
if (OTable.getColumnModel().getColumnCount() > 0) {
OTable.getColumnModel().getColumn(0).setResizable(false);
OTable.getColumnModel().getColumn(0).setPreferredWidth(15);
OTable.getColumnModel().getColumn(0).setHeaderValue(bundle.getString("OsuData.jTable1.columnModel.title5")); // NOI18N
OTable.getColumnModel().getColumn(1).setResizable(false);
OTable.getColumnModel().getColumn(1).setPreferredWidth(15);
OTable.getColumnModel().getColumn(1).setHeaderValue(bundle.getString("OsuData.jTable1.columnModel.title6")); // NOI18N
OTable.getColumnModel().getColumn(2).setResizable(false);
OTable.getColumnModel().getColumn(2).setPreferredWidth(15);
OTable.getColumnModel().getColumn(2).setHeaderValue(bundle.getString("OsuData.jTable1.columnModel.title7")); // NOI18N
}
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1185, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 295, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(36, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private static void LoadTable()
{ // <editor-fold defaultstate="collapsed" desc="Load The Table">
String LoadLine = null;
DefaultTableModel dtm = (DefaultTableModel) OTable.getModel();
try {
BufferedReader br = new BufferedReader(new FileReader("data.txt"));
while ((LoadLine = br.readLine()) != null)
{
Vector data= new Vector();
StringTokenizer st1 = new StringTokenizer(LoadLine,"\t");
while (st1.hasMoreTokens())
{
String nextToken = st1.nextToken();
data.add(nextToken);
}
System.out.println(data);
dtm.addRow(data);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
} // </editor-fold>
public static void SaveTable(JTable table)
{ // <editor-fold defaultstate="collapsed" desc="Save The Table">
try{
TableModel OTM = OTable.getModel();
FileWriter OTW = new FileWriter("data.txt");
for(int i=0; i< OTM.getRowCount(); i++)
{
for(int j=0; j < OTM.getColumnCount(); j++)
{
OTW.write(OTM.getValueAt(i,j).toString()+"\t");
}
OTW.write("\n");
}
OTW.close();
}catch(IOException e){ System.out.println(e); }
} // </editor-fold>
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(OData.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new OData().setVisible(true);
OData.LoadTable();
OTable.setAutoCreateRowSorter(true);
TableRowSorter<DefaultTableModel> rowSorterOD = (TableRowSorter<DefaultTableModel>)OTable.getRowSorter();
rowSorterOD.setComparator(8, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
}
});
}
// Variables declaration - do not modify
private static javax.swing.JTable OTable;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}
data.txt包含:
4.0 2.0 3.0
7.0 6.0 6.0
9.0 7.0 7.0
3.0 2.0 1.0
5.0 3.0 3.0
7.0 6.0 6.0
8.0 8.0 6.0
3.0 1.0 1.0
9.0 10.0 6.0
2.0 1.0 2.0
3.0 2.0 3.0
5.0 4.0 4.0
8.0 6.0 6.0
9.0 7.0 8.0
对不起,如果这有点长