这是更新问题:Read a file in a jtable。你知道,我有一个jTable
,其中包含两个文件名,当它连续点击时会被读入Jtextarea
。
这是代码:
if (evt.getClickCount() == 1) {
int row = jTable4.getSelectedRow();
if (row != -1) {
String firstColumnValue = jTable4.getModel().getValueAt(row, 0).toString();
String secondColumnValue = jTable4.getModel().getValueAt(row, 1).toString();
System.out.println(firstColumnValue + " = " + secondColumnValue);
BufferedReader brComparedFile = null;
try {
String strMainFile;
String strComparedFile;
BufferedReader brMainFile = new BufferedReader(new FileReader(firstColumnValue));
brComparedFile = new BufferedReader(new FileReader(secondColumnValue));
while ((strMainFile = brMainFile.readLine()) != null) {
System.out.println("File Acuan");
jTextAreaMainFileHighlight.read(brMainFile, null);
}
while ((strComparedFile = brComparedFile.readLine()) != null) {
System.out.println("File Pembanding");
jTextAreaFileComparedFileHighlighter.read(brComparedFile, null);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (brComparedFile != null) {
brComparedFile.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Assumpted,我的java项目位于名为final_exam的文件夹中。当JTable
中的文件位于final_exam文件夹中时,它适用于将文件读入JtextArea
。但是当不在final_exam文件夹中时(例如文件名是a.java),例外是:java.io.FileNotFoundException: a.java (The system cannot find the file specified)
。
我的目标是:我的应用程序始终可以读取该文件所在的文件...
任何人都可以提供帮助吗?
EDIT
这是我创建表格的代码:
StringBuilder namaFileUtama = new StringBuilder(); // name of main file
for (final JCCDFile file : files) {
JCCDFile temp;
temp = files[0];
String name = temp.getName();
String enemy = namaFileUtama.toString();
DefaultTableModel models = (DefaultTableModel) Main_Menu.jTable4.getModel();
List<ReportMomentOfTruth> theListRMOT = new ArrayList<ReportMomentOfTruth>();
ReportMomentOfTruth rmot = new ReportMomentOfTruth();
rmot.setNameOfMainFile(name);
rmot.setNameOfComparingFile(enemy);
theListRMOT.add(rmot);
for (ReportMomentOfTruth reportMomentOfTruth : theListRMOT) {
models.addRow(new Object[]{
reportMomentOfTruth.getNameOfMainFile(),
reportMomentOfTruth.getNameOfComparingFile(),});
}
答案 0 :(得分:3)
“我的目标是:我的应用始终可以在该文件所在的位置读取文件”
首先,它只是保存表中的文件名(对于这个用例)是无用的
您可以将文件作为File
对象保存到表中,这样,无论文件位于何处,您始终都有文件对象来获取路径。您可以创建自定义单元格渲染器以仅渲染文件名。
否则我不知道如何从“任何地方”读取文件,正如你所说,只是从文件名中读取。
如果没有看到您如何将文件名实际加载到表中,我可能会建议使用JFileChooser
。
以下是将文件作为File对象保存到表中的示例。您需要为此示例填写几个文件名
import java.awt.Component;
import java.io.File;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class FileRendererDemo {
private static String filePath1 = " [ Enter a valid file path here ]";
private static String filePath2 = " [ Enter a valid file path here ]";
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
File file1 = new File(filePath1);
File file2 = new File(filePath2);
String[] cols = {"file 1", "file 2"};
DefaultTableModel model = new DefaultTableModel(cols, 0);
model.addRow(new File[] {file1, file2});
model.addRow(new File[] {null, null});
model.addRow(new File[] {null, null});
JTable table = getTable(model);
JOptionPane.showMessageDialog(null, table);
}
});
}
private static JTable getTable(TableModel model) {
final JTable table = new JTable(model) {
@Override
public boolean isCellEditable(int row, int col) {
return false;
}
};
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
int row = table.getSelectedRow();
if (row != -1) {
System.out.println("file 1 - " + table.getValueAt(row, 0));
System.out.println("file 2 - " + table.getValueAt(row, 1));
}
}
}
});
table.setDefaultRenderer(Object.class, new FileCellRenderer());
return table;
}
private static class FileCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
JLabel comp = (JLabel) super.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, column);
File file = (File)value;
if (file != null) {
String fileName = (String)((File)value).getName();
comp.setText(fileName);
}
return comp;
}
}
}