I'm tryin' to import some data from a textfile to my JTable.. I wrote some code to do this:
JMenuItem Open = new JMenuItem("Open gegevens");
Open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
readTXT();
}
});
mnNewMenu.add(Open);
public void readTXT(){
try{
String readLine = null;
File file = new File("Resultaten.txt");
FileReader reader = new FileReader(file);
BufferedReader bufReader = new BufferedReader(reader);
while((readLine = bufReader.readLine()) != null) {
String[] splitData = readLine.split("; ");
System.out.println(splitData.length);
String match= splitData[0];
String set= splitData[1];
String spelertje1= splitData[2];
String gamesspeler1= splitData[3];
String streep= splitData[4];
String gamesspeler2= splitData[5];
String spelertje2= splitData[6];
String setwinnaartje= splitData[7];
String matchwinnaartje= splitData[8];
model.addRow(new Object[] { match,set, spelertje1, gamesspeler1,
streep, gamesspeler2, spelertje2, setwinnaartje, matchwinnaartje });
System.out.println("okidokii");
}
reader.close();
}
catch(IOException e){
JOptionPane.showMessageDialog(null, "Error");
e.printStackTrace();
}
}
In my opinion this should work but yet i got this :
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at TennisKlassement.readTXT(TennisKlassement.java:888)
at TennisKlassement$14.actionPerformed(TennisKlassement.java:807)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:389)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:809)
at com.apple.laf.AquaMenuItemUI.doClick(AquaMenuItemUI.java:137)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:850)
at java.awt.Component.processMouseEvent(Component.java:6414)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6179)
at java.awt.Container.processEvent(Container.java:2084)
at java.awt.Component.dispatchEventImpl(Component.java:4776)
at java.awt.Container.dispatchEventImpl(Container.java:2142)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4279)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4209)
at java.awt.Container.dispatchEventImpl(Container.java:2128)
at java.awt.Window.dispatchEventImpl(Window.java:2492)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:676)
at java.awt.EventQueue$2.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:690)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
In fact i do understand the problem.. The String splitdata has only one string to hold. yet i intialised it whit the split method..
my txtfiles looks like this :
This is the code i used to write my jtable to txtfile:
public void writeTXT(){
try {
File file = new File("Resultaten.txt");
PrintWriter os = new PrintWriter(file );
System.out.println(os);
os.println("");
for (int row = 0; row < table.getRowCount(); row++) {
for (int col = 0; col < table.getColumnCount(); col++) {
//os.print(table.getColumnName(col));
os.print(";");
os.print(table.getValueAt(row, col) + "\t")
System.out.println("ok");
}
os.println("");
}
os.close();
System.out.println("Done!");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
For your knowledge, nothing happens with the jtable when using this method....
Thanks for your kind response