Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将多个filechooser的值添加到jtable_Java_Swing_Jtable_Jfilechooser - Fatal编程技术网

Java 如何将多个filechooser的值添加到jtable

Java 如何将多个filechooser的值添加到jtable,java,swing,jtable,jfilechooser,Java,Swing,Jtable,Jfilechooser,我想从文件选择器中选择多个文件,并将这些值放入JTable。我这样尝试过,但相同的值在JTable上重复。在打印行中,它可以正确打印值 JFileChooser fileChooser = new JFileChooser(); fileChooser.setMultiSelectionEnabled(true); int returnVal = fileChooser.showOpenDialog(fileChooser); if (returnVal==JFileChooser.APPROV

我想从文件选择器中选择多个文件,并将这些值放入
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.");
}
JFileChooser fileChooser=newjfilechooser();
fileChooser.setMultiSelectionEnabled(true);
int returnVal=fileChooser.showOpenDialog(fileChooser);
if(returnVal==JFileChooser.APPROVE_选项){
File File[]=fileChooser.getSelectedFiles();
DefaultTableModel dtm=(DefaultTableModel)jTable1.getModel();
向量v=新向量();
for(int i=0;i
使用
JButton
作为单元格编辑器,如图所示。让按钮的事件处理程序调用
JFileChooser
。更新您的
表格模型
以反映所选文件。

您希望每次都添加一个新向量-您需要将向量移动到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
for(int i=0;i
@user2136160:单击左侧的,您可以接受此答案。