Search code examples
javaswingjtableabstracttablemodel

JTable cant accept the values from String array


Hey guys i am building a java application in which i have a jtable and i would like to have fixed columns from a string array.

My code is:

final String[] names = { "Id", "Description", "Type", "Price per Unit", "Quantity" };
        JTable table = new JTable(new AbstractTableModel() {



             @Override
             public String getColumnName(int c) {
                    return "lol";
             }

             public int getRowCount() {
                 return products.size();
             }

             public int getColumnCount() {
                 return 5;
             }

             public Object getValueAt(int row, int col) {
                 Product p = products.get(row);
                 if(col == 0){
                     return p.getId();
                 }
                 if(col == 1){
                     return p.getDescription();
                 }
                 if(col == 2){
                     if(p.getClass() == Service.class){
                         return "Service";
                     }else{
                         return "Item";
                     }
                 }
                 if(col == 3){
                     if(p.getClass() == Service.class){
                         Service service = (Service)p;
                         return service.getNumberOfHours();
                     }else{
                         Item item = (Item) p;
                         return item.getPricePerItem();
                     }
                 }
                 if(col == 4){
                     if(p.getClass() == Service.class){
                         Service service = (Service)p;
                         return service.getPricePerHour();
                     }else{
                         Item item = (Item) p;
                         return item.getQuantity();
                     }
                 }

                 return null;
             }


         });

the values from string[] do not show but the rest of the data is fine.

Thanks in advance.


Solution

  • Since you have overridden getColumnName() that always returns same column name "lol" as per your code.

    Simply return names[c] instead of "lol";

    @Override
    public String getColumnName(int c) {
        return names[c];
    }
    
    @Override
    public int getColumnCount() {
        return names.length;
    }
    

    Sample Code 1: (using DefaultTableModel)

    final String[] names = { "Type", "Company", "Shares", "Price" };
    final Object[][] products = {
            { "Buy", "IBM", new Integer(1000), new Double(80.50) },
            { "Sell", "MicroSoft", new Integer(2000), new Double(6.25) },
            { "Sell", "Apple", new Integer(3000), new Double(7.35) },
            { "Buy", "Nortel", new Integer(4000), new Double(20.00) } };
    
    JTable table = new JTable(new DefaultTableModel(products, names);
    
    JScrollPane scrollPane = new JScrollPane(table);
    // now add the scroll pane in `JPanel` 
    

    Sample code 2: (using AbstractTableModel)

    final String[] names = { "Type", "Company", "Shares", "Price" };
    final Object[][] products = {
            { "Buy", "IBM", new Integer(1000), new Double(80.50) },
            { "Sell", "MicroSoft", new Integer(2000), new Double(6.25) },
            { "Sell", "Apple", new Integer(3000), new Double(7.35) },
            { "Buy", "Nortel", new Integer(4000), new Double(20.00) } };
    
    JTable table = new JTable(new AbstractTableModel() {
    
        @Override
        public String getColumnName(int col) {
            return names[col];
        }
    
        @Override
        public int getRowCount() {
            return products.length;
        }
    
        @Override
        public int getColumnCount() {
            return names.length;
        }
    
        @Override
        public Object getValueAt(int row, int col) {
            return products[row][col];
        }
    
    });
    
    JScrollPane scrollPane = new JScrollPane(table);
    // now add the scroll pane in `JPanel` 
    

    snapshot:

    enter image description here