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:单击左侧的,您可以接受此答案。