2013-04-06 60 views
0

我有以下代码:重写“getValueAt”中AbstractTableModel的绑定映射到JTable中

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类“文件”中的变量加载到JTable中。有人可以告诉我我做错了什么吗?我现在基本上已经停留了3天,真的很感谢一些帮助。

回答

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

有很多错误。

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

然后,您的模型通过返回0实现getColumnCount()getRowCount()。这意味着您的表包含0行和0列。所以如果你不打算有任何价值的话,我真的不明白使用表格的意义。

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

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

+0

感谢您的输入。对Java很新。所以我希望文件中的3个变量(文件,持续时间和状态)在JTable中显示为三列。 – Omid 2013-04-06 12:32:46

+0

和地图有什么关系?你想在表中显示一个文件,因此它有1行3列吗?你是否已经根据我已经给你的指示开始修复你的代码? – 2013-04-06 12:44:02

+0

是,1行3列。但只要在地图中添加值,它们也会被添加到JTable中。是的,我删除了所有静态。 – Omid 2013-04-06 12:47:42

1

我需要一个键/值对。

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

+0

getenv不工作时,密钥的地图是一个类。因此:private final Map list = System.getenv(); – Omid 2013-04-06 17:45:26

+0

@Omid:对,这只是一个相关的例子。 – trashgod 2013-04-06 17:50:03