将文本文件读取到jTable

时间:2014-04-18 19:02:27

标签: java exception jtable bufferedreader awt-eventqueue

我正在尝试将一些数据从文本文件导入到我的JTable中。 我写了一些代码来做到这一点:

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();

            }


    }

在我看来这应该有效,但我得到了这个:

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)

实际上我确实理解了这个问题..字符串splitdata只有一个字符串可以容纳。然而我用分裂方法将其初始化了。

我的txtfiles看起来像这样:

enter image description here

这是我用来将我的jtable写入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();

      }


        }

根据您的了解,使用此方法时jtable没有任何反应....

感谢您的回复

2 个答案:

答案 0 :(得分:0)

根据异常,你的代码似乎是splitData的长度小于9。

编辑:(由OP接受第一部分)

确保您的文件以“;”分隔并具有所需的长度。

答案 1 :(得分:0)

  

String splitdata只有一个字符串可以容纳。然而我用分裂方法将其初始化了。

那么为什么要粘贴与该表相关的所有代码?您的问题在于将文本拆分为令牌。

根据您的代码,您似乎正在写出semi-colon and a tab character,但看起来拆分使用的是semi-colon and a space character,因此您无法获得预期的令牌。

您可以编写一个简单的SSCCE来测试它:

public class Main

{
    public static void main(String[] args)
    {
        String text = ";\tone;\ttwo;\tthree;\tfour";

        for (String word: text.split("; "))
//      for (String word: text.split(";\t"))
            System.out.println(word);
    }
}

当您发布问题时,仅发布相关信息以了解问题。