Sorting double values ​​in JTable

I found many questions related to this, but I did not find a simple solution to my problem.

I cannot find a way to correctly execute double JTable values.

I extended AbstractTableModel to get an array of the class and return the corresponding types for the column:

class TableModelMod extends AbstractTableModel{ private ArrayList data; private String [] headers; private Class [] types; TableModelMod(String [] heads, ArrayList datas, Class [] classes){ headers = heads; data = datas; types = classes; } ... @Override public Class getColumnClass(int c){ if (c > types.length - 1) return null; else return types[c]; } ... 

And then in my custom JTable constructor:

 TableRowSorter<TableModelMod> sorter = new TableRowSorter<TableModelMod>((TableModelMod)getModel()); 

But then I get this error when adding lines:

 java.lang.IllegalArgumentException: Cannot format given Object as a Number 

Cannot execute the DecimalFormat.format(Object number, StringBuffer toAppendTo, FieldPosition pos) , which accepts most numeric types, but Double.

If I use another class for double columns, I do not get an error, but sorting does not work properly. I tried with different number classes, but no one seems to sort the binary letters correctly:

 @Override public Class getColumnClass(int c){ if (c > types.length - 1) return null; else if (types[c] == Double.class) return Number.class; else return types[c]; } 

I'm not sure if I need to implement a custom RowSorter, a custom CellRenderer, or both.

Can someone guide me on how to fix this in a simpler way?

Many thanks and best regards.

CONNECTED: SOLVED

It’s hard to say where the problem was.

An ArrayList containing the Object [] strings was populated from the ResultSet database using getString (int) instead of getObject (int) or getDouble (int), so the value could not be used as a Double handler. It is strange that he made no exceptions using Integer or Number as the column class, but in any case it was sorted as String. I searched for the problem in the wrong classes, as I was convinced that my ArrayList contains only objects.

Thank you very much for your examples, looking at them, I noticed that my doubles were actually strings, and then I could find where the transformation happened.

+6
source share
2 answers

Check out this code. It sorts double values.

enter image description here

enter image description here

 import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.RowSorter; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import javax.swing.table.TableRowSorter; public class RowSorterDemo { public static void main(String args[]) { JFrame frame = new JFrame("Sort Table Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Object rows[][] = { { "J", 23.1 }, { "R", 21.1, }, { "E", 21.2, }, { "B", 27.1, }, { "A", 25.2, }, { "S", 22.9, }, }; String columns[] = { "Name", "Age" }; TableModel model = new DefaultTableModel(rows, columns) { public Class getColumnClass(int column) { Class returnValue; if ((column >= 0) && (column < getColumnCount())) { returnValue = getValueAt(0, column).getClass(); } else { returnValue = Object.class; } return returnValue; } }; JTable table = new JTable(model); RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model); table.setRowSorter(sorter); JScrollPane pane = new JScrollPane(table); frame.add(pane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); } } 

I slightly modified the source specified in this link so that it takes double values.

+6
source

this code may be your SSCCE , you can show that you are issuing this code, none of the code has been edited, that you sent

 import java.util.*; import javax.swing.*; import javax.swing.table.*; public class TableTest2 { public void initGUI() { String[] columnNames = {"numbers","double", "text"}; Object[][] data = {{1, 0.81, "A"}, {10, 5.268752005, "B"}, {7, 100.0,"C"},{6, 52.879999, "A"}, {4, 62.50, "B"}, {2, 854.9999, "C"},{11, 19.01, "A"}, {100, 0.0009, "B"}, {20, 100.09, "C"}}; JTable table = new JTable(new DefaultTableModel(data, columnNames) { private static final long serialVersionUID = 1L; @Override public java.lang.Class<?> getColumnClass(int c) { return getValueAt(0, c).getClass(); } }); table.setAutoCreateRowSorter(true); table.getRowSorter().setSortKeys(Arrays.asList(new RowSorter.SortKey(0, SortOrder.ASCENDING))); JFrame frame = new JFrame("LFIXimate"); frame.setResizable(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JScrollPane(table)); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new TableTest2().initGUI(); } }); } } 
+6
source

Source: https://habr.com/ru/post/896585/


All Articles