Java 压倒一切;getValueAt";在AbstractTableModel中,将映射绑定到JTable

Java 压倒一切;getValueAt";在AbstractTableModel中,将映射绑定到JTable,java,swing,jtable,hashmap,abstracttablemodel,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.duratio

我有以下代码:

    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());

但是,我无法将HashMap类“Files”中的变量加载到JTable中。谁能告诉我我做错了什么?我基本上已经被困了3天了,非常感谢你的帮助

很多事情都不对劲

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

然后,模型通过返回0来实现
getColumnCount()
getRowCount()
。这意味着您的表包含0行和0列。因此,如果你不想在表格中有任何价值,我真的不认为使用表格有什么意义

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

最后,您说您有一个
映射
,但您没有说这个映射和表之间的关系应该是什么。您在模型中的任何地方都不使用此映射,而且由于代码本身没有意义,因此很难猜测映射实际包含的内容以及表实际应显示的内容

我需要一对密钥/值


作为参考,从现有的
映射构建
AbstractTableModel
。它使用映射的
keySet()
作为列零,并使用每个键获取该行的值。

确定刚刚找到了解决方案:

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();
        }
    }
}
private final Map list=new LinkedHashMap();
类MyTableModel扩展了AbstractTableModel{
私有字符串[]columnNames={“文件”、“持续时间”、“状态”};
public void addElement(String addfile、String addurr、String addstatus、String addpath){
文件f=新文件(addfile、addurr、addstatus);
list.put(f,addpath);//编辑
fireTableRowsInserted(list.size()-1,list.size()-1);
}
@凌驾
public int getColumnCount(){
返回columnNames.length;
}
@凌驾
public int getRowCount(){
返回list.size();
}
@凌驾
公共字符串getColumnName(int-col){
返回列名[col];
}
@凌驾
公共对象getValueAt(int行索引、int列索引){
List randAccess=newarraylist(List.entrySet());
开关(列索引){
案例0:
返回randAccess.get(rowIndex.getKey().getfiles();
案例1:
返回randAccess.get(rowIndex.getKey().getduration();
案例2:
返回randAccess.get(rowIndex.getKey().getstatus();
违约:
抛出新的IndexOutOfBoundsException();
}
}
}

感谢您的输入。对Java来说是相当陌生的。因此,我希望文件中的3个变量(文件、持续时间和状态)在JTable中显示为三列。与映射的关系是什么?是否要在表中显示一个文件,该文件将有1行3列?您是否已经开始根据我已经给您的指示修复代码?是的,1行3列。但一旦在映射中添加了值,它们也将添加到JTable中。是的,我删除了所有静态数据。@mKorbel:我同意
DefaultTableModel
的便利性,但是
AbstractTableModel
可能允许更少的数据复制。@trashgod这个抽象不容易理解,您和JB的知识和经验到目前为止……,:-)当映射的键是类时,getenv不起作用。这样:private final Map list=System.getenv()@奥米德:对,这只是一个相关的比较例子。
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();
        }
    }
}