0

So I have a JTable which is filled with data from a text file, I need to sort the table based on the 6th column "Stock"

File inputFile = new File("Stock.txt");
BufferedReader br = new BufferedReader(new FileReader(inputFile));


static Object[] colContent = { "Barcode", "Device Name", "Device Type", "Brand", "Colour", "Connectivity", "Stock", "ogCost", "Price", "Additional" };          
static Object[][] data={ {null,null,null,null,null,null,null,null,null,null }};
JTable table = new JTable((Object[][]) data, colContent);

I have tried everything including overriding the table model but nothing seems to work!

        DefaultTableModel model = new DefaultTableModel() {

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

        };

        table.setModel(model);
        model.setColumnIdentifiers(colContent);


        String firstLine = br.readLine().trim();
        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);
        }

This is the sorter I have used, it works but is displaying values like 1,10,15,3,5,8

         TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
         ArrayList<SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
         sortKeys.add(new RowSorter.SortKey(6, SortOrder.ASCENDING));
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ahsirk83
  • 23
  • 4

0 Answers0