我的代碼太大,無法在此處添加。我會爲您提供有關問題的部分。從txt加載JTable數據
frame2.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
File table_f = new File("C:\\Database\\Table\\Table.txt");
String table_f_path = table_f.getPath();
try {
BufferedReader b_reader = new BufferedReader(new FileReader(new File(table_f_path)));
BufferedReader reader = new BufferedReader(new FileReader(table_f_path));
int lines = 0;
while (reader.readLine() != null) lines++;
reader.close();
String table_apo_out;
for (int y = 1;y < lines;y++){
table_apo_out = b_reader.readLine();
String[] table_apo_out_array = table_apo_out.split("~");
for (int z=0;z < 20;z++) {
System.out.println(Arrays.toString(table_apo_out_array));
System.out.println(y);
System.out.println(lines);
model_table.setValueAt(table_apo_out_array[z],y,z);
}
}
} catch (IOException qq) {
qq.printStackTrace();
}
}
});
這部分嘗試從txt文件加載保存的數據。我也會給你保存在txt文件,這些信息的一部分:
300/50~Δευτέρα 12 Μαΐου 2014~gak~gak~gak~gak~gak~gak~gak~gak~gak~gak~gak~Παρασκευή 5 Μαΐου 2017~524.0~2452.0~gak~2452.0~gak~245245
300/50~Δευτέρα 12 Μαΐου 2014~gak~gak~gak~gak~gak~gak~gak~gak~gak~gak~gak~Παρασκευή 5 Μαΐου 2017~524.0~2452.0~gak~2452.0~gak~---
的數據保存:
frame2.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
File table_f = new File("C:\\Database\\Table\\Table.txt");
int num_row = table.getRowCount();
int num_columns = 21;
for (int n = 0;n < num_row; n++){
for(int m = 0;m < num_columns; m++){
FileWriter f_writer = null;
BufferedWriter b_writer = null;
String table_f_path = table_f.getPath();
try {
f_writer = new FileWriter(table_f_path, true);
b_writer = new BufferedWriter(f_writer);
if (m == 20) {
Object com = model_table.getValueAt(n, m);
String com_str = com.toString();
b_writer.write(com_str+"\r\n");
} else if (m == 0){
continue;
} else {
Object data = model_table.getValueAt(n, m);
String data_str = data.toString();
b_writer.write(data_str+"~");
}
} catch (IOException | NullPointerException | ArrayIndexOutOfBoundsException io){
io.printStackTrace();
} finally {
try {
if (b_writer != null)
b_writer.close();
if (f_writer != null)
f_writer.close();
if(n == num_row){
frame2.dispose();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
});
我們從JTable中得到的數據是這樣的。現在我面臨一個問題,試圖加載它們!這裏是個例外,我得到:
Exception in thread "AWT-EventQueue-0"
java.lang.ArrayIndexOutOfBoundsException: 1 >= 0
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:664)
at Test$21.windowOpened(Test.java:2545)
at java.awt.AWTEventMulticaster.windowOpened(AWTEventMulticaster.java:340)
at java.awt.Window.processWindowEvent(Window.java:2051)
at javax.swing.JFrame.processWindowEvent(JFrame.java:305)
at java.awt.Window.processEvent(Window.java:2013)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
代碼的某些部分是在希臘,對不起
'at Test $ 21.windowOpened(Test.java:2545)' - 2545?重構的時間 –
您會在Q&A'a中找到您的問題重複的完整示例;如果這不是重複的,請編輯您的問題以包含顯示您修改方法的[mcve]。 – trashgod
此問題最初是作爲以下副本而封閉的:https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it。雖然該問題試圖解決「一般」ArrayIndexOutOfBoundsException的原因,但基於花費一些時間來真正查看代碼,OP不理解使用DefaultTableModel的基本知識,因此需要更多信息來解決問題。所以我重新提出了這個問題。 – camickr