繁体   English   中英

读取文件并将数据从文件发送到JTable

[英]reading file and sending data from the file to a JTable

public void showTotalSummary()
        {
            JFrame totalSummary = new JFrame("Total Leave Credit Summary");
            totalSummary.setSize(970, 523);
            totalSummary.setResizable(false);
            totalSummary.setLocationRelativeTo(null);
            totalSummary.setVisible(true);              

            panelTotalSummary = new JPanel();               
            panelTotalSummary.setBorder ( (Border) new TitledBorder ( new EtchedBorder (), "Display Area" ) );
            totalSummary.add(panelTotalSummary);

            String[] headings = {"Employee No.", "Employee Names", "Vacation Leave", "Tardiness", "Sick Leave", "Nonattendances", "Total Leave Earned "};                   

            String [][] data = {{"01", "Adlawan","10.50","2.50","20", "4", "30.50"},
                                {"02","Angeles","20.10","5.90","25","6","45.10"},
                                {"03","Benenoso","30.70","7.60","34","8","64.70"},
                                {"04","Bermas","20","4.10","25","3","45"}};

            JTable totalSummaryTable = new JTable(data, headings);              
            totalSummaryTable.getTableHeader().setFont( new Font( "Tahoma" , Font.BOLD, 12 ));
            Font f = new Font("Arial", Font.ITALIC, 13);                    
            totalSummaryTable.setFont(f);           
            totalSummaryTable.setGridColor(Color.BLUE);
            totalSummaryTable.setPreferredScrollableViewportSize(new Dimension(900, 400));
            totalSummaryTable.setFillsViewportHeight(true);

            JScrollPane jcp = new JScrollPane(totalSummaryTable);               
            jcp.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );   
            panelTotalSummary.add(jcp); 

        }

使用这些代码,我能够将数据发布到我的JTable中。 我的问题是我如何能够从文件中读取该文件中的数据并将该数据从JTable发送到一行? 我知道如何从文件读取并将那些数据发送到JTextField。 我将如何执行相同的过程,而不是将数据发送到JTextField,而是将文件中的数据从JTabel发送到行。 任何人持有的任何东西都将不胜感激。

您需要执行的是实现自己的TableModel 本教程“如何使用表”将有助于理解表在Swing中的工作方式。

基本上,您需要做的是创建一个类,该类将扩展AbstractTableModel并至少提供以下三种方法的实现:

public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);

您可以使用模型,使其可以将其数据保存在数组,向量或哈希图中,或者可以从外部源获取数据,例如读取文件并从中获取数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM