1

我想从文件选择器中选择多个文件并将这些值放入JTable. 我像这样尝试过,但相同的值在JTable. 在打印行中,它正确打印值。

JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
int returnVal = fileChooser.showOpenDialog(fileChooser);
if (returnVal==JFileChooser.APPROVE_OPTION) {

   File file[] = fileChooser.getSelectedFiles();
   DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
   Vector v = new Vector();

   for (int i = 0; i < file.length; i++) {
        String name;
        String path;
        long  size;
       name = file[i].getName();
       path = file[i].getPath();

       System.out.println("name = "+name+" path = "+path);

           v.add(name);
           v.add(path);
           dtm.addRow(v);;

          } 

    try {

    } catch (Exception ex) {
   System.out.println("problem accessing file"+file.getAbsolutePath());
    }
} else {
    System.out.println("File access cancelled by user.");
}
4

2 回答 2

2

您希望每次都添加一个新 Vector - 您需要将 Vector 移动到 for 循环内。

   for (int i = 0; i < file.length; i++) {
       Vector v = new Vector();
       String name;
       String path;
       long  size;
       name = file[i].getName();
       path = file[i].getPath();

       System.out.println("name = "+name+" path = "+path);
       v.add(name);
       v.add(path);
       dtm.addRow(v);;
       // rest of your code
于 2013-10-09T17:22:58.930 回答
1

使用 aJButton作为单元格编辑器,如此处所示。让按钮的事件处理程序唤起JFileChooser. 更新您TableModel以反映所选文件。

于 2013-10-09T17:21:59.477 回答