覆盖" getValueAt"在AbstractTableModel中将Map绑定到JTable

时间:2013-04-06 11:40:23

标签: java swing jtable hashmap abstracttablemodel

我有以下代码:

    class Files {       
    private static String files;
    private static String duration;
    private static String status;

    public Files(String files, String duration, String status) {
        this.files = files;
        this.duration = duration;
        this.status = status;
    }

    public static String getfiles() {
        return files;
    }

    public static String getduration() {
        return duration;
    }

    public static String getstatus() {
        return status;
    }
}

    Map<Files, String> hmap = new HashMap<Files,String>();

    private void AddFiles(String addfile,String addurr,String addstatus, String addpath){       
    Files f = new Files(addfile, addurr, addstatus);                
    hmap.put(f, addpath);       
}
    final JTable table = new JTable();
    table.setBounds(26, 27, 664, 274);
    table.setModel(new MyTableModel());

所以我正在创建一个新表并覆盖“getValueAt”。

        class MyTableModel extends AbstractTableModel {    
        @Override
        public int getColumnCount() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public int getRowCount() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
             switch (columnIndex) {
             case 0:
                    return Files.getfiles(); 
             case 1:
                    return Files.getduration();
             case 2:
                    return Files.getstatus();
             default:
                    throw new IndexOutOfBoundsException();
             }
        }
    }

然而,我无法将HashMap的“Files”类中的变量加载到JTable中。谁能告诉我我做错了什么?我基本上已经被困了3天了,真的很感激一些帮助。

3 个答案:

答案 0 :(得分:1)

有很多错误。

首先,为什么所有字段和方法都在Files中是静态的。这意味着Files实例实际上根本没有任何状态,并且所有Files实例共享相同的文件,持续时间和状态。 Thos字段和方法肯定应该是实例字段和方法(即您必须删除static修饰符)。

然后,您的模型通过返回0来实现getColumnCount()getRowCount()。这意味着您的表包含0行和0列。如果你不打算在其中有任何价值,我真的没有看到使用表的重点。

另一方面,getValueAt()方法意味着所有行都包含相同的值,因为无论rowIndex参数包含什么,都返回相同的值。

最后,你说你有一个Map<Files, String>,但你没有说这个地图和桌子之间的关系应该是什么。你不要在你的模型中的任何地方使用这个地图,并且由于代码本身没有意义,因此很难猜出地图实际包含的内容以及表格应该实际显示的内容。

答案 1 :(得分:1)

  

我需要一个键/值对。

供参考,EnvTableTest从现有AbstractTableModel构建Map。它使用地图的keySet()作为第0列,它使用每个键来获取该行的值。

答案 2 :(得分:0)

好的,刚刚找到解决方案:

private final Map<Files, String> list = new LinkedHashMap<Files,String>();

class MyTableModel extends AbstractTableModel {

private String[] columnNames = {"File","Duration","Status"};

public void addElement(String addfile,String addurr,String addstatus, String addpath) {             
Files f = new Files(addfile, addurr, addstatus);                
list.put(f, addpath); // edit
fireTableRowsInserted(list.size()-1, list.size()-1);   
}

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

@Override
public int getRowCount() {
return list.size();
}

@Override
public String getColumnName(int col) {
return columnNames[col];
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {

List<Entry<Files,String>> randAccess = new ArrayList<Entry<Files,String>>(list.entrySet());

switch (columnIndex) {
case 0:
return randAccess.get(rowIndex).getKey().getfiles();
case 1:
return randAccess.get(rowIndex).getKey().getduration();
case 2:
return randAccess.get(rowIndex).getKey().getstatus();
default:
throw new IndexOutOfBoundsException();
        }
    }
}