JTable摆动异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JTable摆动异常相关的知识,希望对你有一定的参考价值。

这是我前一天问过的一个问题的后续问题,我一直试图调试代码没有成功,但基本上应用程序应该允许用户在选择后选择要导入或查看的CSV该文件只能在JTable中查看Csv的内容。目前我可以查看csv中的数据,但它没有像JIB那样在Jtable中显示。这是下面程序的代码,除了netbeans抛出我。

public class Asist_payment_import_v1 extends JPanel implements ActionListener{
private final JTable Table;

public Asist_payment_import_v1(){
    super(new BorderLayout(3,3));


    this.Table = new JTable(new MiModel());
    this.Table.setPreferredScrollableViewportSize(new Dimension(700,70));
    this.Table.setFillsViewportHeight(true);

    JPanel ButtonOpen = new JPanel(new FlowLayout(FlowLayout.CENTER));
    //the below  utton will be used to upload csv data to Database, the action listener should connect to the db, and pass data into the Db.
    //JPanel ButtonUpload = new JPanel(new FlowLayout(FlowLayout.LEFT));

    JButton open_file = new JButton("Open");
    JButton save_file = new JButton("Save");

    ButtonOpen.add(open_file);
    add(ButtonOpen, BorderLayout.SOUTH);

    open_file.addActionListener(this);
    save_file.addActionListener(this);

    //create scrollPane to this Panel
    JScrollPane scrollpane = new JScrollPane(Table);

    //add the scroll pane to this pane
    add(scrollpane,BorderLayout.CENTER);

    //add Border
    setBorder(new EmptyBorder(5,5,5,5));

}

public void actionPerformed(ActionEvent Event)
{

    JFileChooser file_chooser = new JFileChooser();
    boolean pressed = true;

    if(pressed)
    {
        int ReturnVal = file_chooser.showOpenDialog(null);
        CSVFile Rd = new CSVFile();

        MiModel NewModel = new MiModel();
        this.Table.setModel(NewModel);// to set the Table model

        File DataFile = file_chooser.getSelectedFile();
        ArrayList<String[]> rs2 = Rd.ReadCSVFile(DataFile);

        NewModel.AddCSVData(rs2);
        System.out.println("Rows: " +NewModel.getRowCount());
        System.out.println("Cols: " +NewModel.getColumnCount());

    }
}


public class CSVFile
{
    private ArrayList<String[]> Rs = new ArrayList<>();
    private String[] OneRow;

    public ArrayList<String[]> ReadCSVFile(File DataFile){
        try{
            BufferedReader brd = new BufferedReader (new FileReader(DataFile));

            while(brd.readLine()!= null){
                String st = brd.readLine();
                OneRow = st.split(",");
                Rs.add(OneRow);
                System.out.println(Arrays.toString(OneRow));
                }
    }//end try 
        catch(Exception ex){
        String errmsg = ex.getMessage();
        System.out.println("File not Found: "+errmsg);
    }//end exception handling
    return Rs;
  }//End of ArrayList_readCSVFile class
}//End of CSVFile class

private static void createAndShowGui()
{
    //setup Window
    JFrame frame = new JFrame("PaymentImport");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //setup the conentPane
    Asist_payment_import_v1 newContentPane = new Asist_payment_import_v1();
    frame.setContentPane(newContentPane);

    //display the window
    frame.pack();
    frame.setVisible(true);

}


class MiModel extends AbstractTableModel
{
    private String [] columnNames = {"Nr","LinkId","Ammount","PayDate","Month","Year","Printed","User","RefNo","ShortSwi",
                                    "BranchNo","SyncHQ","SyncBranch","SysDateStamp","RDReason","UnderPrice","PaydMark","RecieptPrinted"};

    private ArrayList<String[]>Data = new ArrayList<>();

    public void AddCSVData(ArrayList<String[]> DataIn){
        this.Data = DataIn;
        this.fireTableDataChanged();
    }

    @Override
    public int getColumnCount(){
        return columnNames.length;
    }

    @Override
    public int getRowCount(){
        return Data.size();
    }

    @Override
    public String getColumnName(int col){
        return columnNames[col];
    }

    @Override
    public Object getValueAt(int row, int col){
        return Data.get(row)[col];
    }
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // schedule a job for the event-despatching thread:
    //creating and showing this application's GUI
    javax.swing.SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            createAndShowGui();
        }
    }); } }

抛出的异常:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 16
at asist_payment_import_v1.Asist_payment_import_v1$MiModel.getValueAt(Asist_payment_import_v1.java:150)
at javax.swing.JTable.getValueAt(JTable.java:2717)
at javax.swing.JTable.prepareRenderer(JTable.java:5706)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:780)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
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.awt.EventQueue.dispatchEvent(EventQueue.java:726)
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)

这是netbeans输出面板上显示的结果的一部分,而不是JTable:

[2019017, 288633, 69.00, 20171127, 11, 2017, "False", "Annamarie", "Group 
Payment (19061)", "False", "All", 0, , , "2017-11-27 09:43:13", "", 54.00, 
20171127]
[2019019, 288638, 42.00, 20171127, 11, 2017, "False", "Annamarie", "Group 
Payment (19061)", "False", "All", 0, , , "2017-11-27 09:43:13", "", 27.00, 
20171127]
[2019021, 315068, 42.00, 20171127, 11, 2017, "False", "Annamarie", "Group 
Payment (19061)", "False", "All", 0, , , "2017-11-27 09:43:14", "", 27.00, 
20171127]
[2019023, 294133, 69.00, 20171127, 11, 2017, "False", "Annamarie", "Group 
Payment (19061)", "False", "All", 0, , , "2017-11-27 09:43:14", "", 54.00, 
20171127]
[2019025, 288623, 130.00, 20171127, 11, 2017, "False", "Annamarie", "Group 
Payment (19063)", "False", "All", 0, , , "2017-11-27 09:43:30", "", 1068.80, 
20171127]
[2019027, 288625, 105.00, 20171127, 11, 2017, "False", "Annamarie", "Group 
Payment (19063)", "False", "All", 0, , , "2017-11-27 09:43:31", "", 855.00, 
20171127]
Rows: 690551
Cols: 18
答案

我遇到了这个问题的解决方案,我对我的CSV方法进行了修改,读取了csv数据..这里是所做的更改

public class CSVFile
{
    private ArrayList<String[]> arrLs = new ArrayList<>();
    private Object OneRow;

    public ArrayList<String[]> ReadCSVFile(File DataFile)
    {
        try
        {    
            //BufferedReader to Read through CSV Contents
            BufferedReader reader = new BufferedReader (new FileReader(DataFile));
            String line;

            // while loop to read through the data, while bufferedreader is not null-do .... 
            while(reader.readLine()!= null)
            {
                try
                {
                    line = reader.readLine();

                    if(line != null)
                    {
                        String[] array = line.split(",");

                        for(String result:array)
                        {
                            //System.out.println(OneRow[2]+");
                            System.out.println(result);
                        }
                    }                      
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
                finally
                {
                    if(reader == null)
                    {
                        reader.close();
                    }
                }

                //if statement needed in the case the 
                //Selected CSV has more than the required number of ColumnHeaders
                //and the BufferedReader needs to skip the first Row, as this is the 
                //columnHeaders and they cannot be included again
                String read = reader.readLine();//bufferedreader string variable
                OneRow = read.split(",");
                arrLs.add((String[]) OneRow);
                // System.out.println(Arrays.toString(OneRow));
            }//end try 
            catch(Exception ex)
            {
                 String errmsg = ex.getMessage();
                 //System.out.println("File not Found: "+errmsg);
            }    // end exception handling

            return (ArrayList<String[]>) arrLs;
        }   //End of ArrayList_readCSVFile class
    } //End of CSVFile class
}

这使我能够查看JTable中的csv数据,这是这里的主要问题

以上是关于JTable摆动异常的主要内容,如果未能解决你的问题,请参考以下文章

比较两个或多个 JTable 和“突出显示”差异

执行 jtable 显示异常

从 RestulSet 表编辑 JTable

JTable运行的时候抛出NullPointerException的问题

使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段

异常和TCP通讯