JTable无法接受String数组中的值

时间:2014-06-13 22:27:05

标签: java swing jtable abstracttablemodel

嘿伙计们我正在构建一个java应用程序,其中我有一个jtable,我想从字符串数组中获得固定列。

我的代码是:

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;
             }


         });

来自string []的值没有显示,但其余数据都没问题。

提前致谢。

1 个答案:

答案 0 :(得分:2)

由于您已覆盖getColumnName(),因此根据您的代码始终返回相同的列名"lol"

只需返回names[c]而不是"lol";

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

@Override
public int getColumnCount() {
    return names.length;
}

示例代码1:(使用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` 

示例代码2:(使用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` 

快照:

enter image description here