0

How would I sort a JTable by the column "quantity" in descending order? I am adding the data in the table by scanning text file and adding each thing to an object, etc. if that makes sense.

What is the simplest way of doing this?

tblProduct = new JTable();
scrollPane.setViewportView(tblProduct);

dtmProduct = new DefaultTableModel();
dtmProduct.setColumnIdentifiers(new Object[] {"Barcode", "Device Name", "Device Type", "Brand", "Colour", "Connectivity", "Quantity", "Retail Price", "Add. Info."});
tblProduct.setModel(dtmProduct);

JButton btnViewProducts = new JButton("View all");
btnViewProducts.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        try {
            ArrayList<Product> productList = new ArrayList<Product>();
            File inputFile = new File("Stock.txt");
            Scanner fileScanner = new Scanner(inputFile);
            Product product;            
            
            while (fileScanner.hasNextLine()) {
                String[] details = fileScanner.nextLine().split(",") ;
                product = new Product(Integer.parseInt(details[0].trim()), details[1].trim(), details[2].trim(), details[3].trim(), details[4].trim(), details[5].trim(), Integer.parseInt(details[6].trim()), Float.parseFloat(details[7].trim()), Float.parseFloat(details[8].trim()), details[9].trim());
                productList.add(product); 
            } 
            
            for(Product product1: productList) {
                Object[] rowdata = new Object[] {product1.getbarcode(), product1.getdeviceName(), product1.getdeviceType(), product1.getbrand(), product1.getcolour(), product1.getconnectivity(), product1.getquantity(), product1.getretailPrice(), product1.getadditionalInformation()};
                dtmProduct.addRow(rowdata);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Elee
  • 7
  • 2
  • 2
    this url will help you. https://stackoverflow.com/questions/28823670/how-to-sort-jtable-in-shortest-way – vaibhavsahu Aug 25 '21 at 22:03
  • 3
    Start by looking at [`JTable`, Sorting and Filtering](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting), for [example](https://stackoverflow.com/questions/28823670/how-to-sort-jtable-in-shortest-way/28823706#28823706) – MadProgrammer Aug 25 '21 at 22:10

0 Answers0