Alex Mikki
Alex Mikki

Reputation: 9

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

        }

Using those codes i was able to post data to my JTable. My question is how will i be able to read from a file get the data from that file and send the data to a row from the JTable? I know how to read from a file and send those data to a JTextField. How will i do the same process but instead of sending the data to a JTextField, i want the data from a file to be send to a row from a JTabel. Any held from anybody would be much appreciated.

Upvotes: 0

Views: 233

Answers (1)

Sujay
Sujay

Reputation: 6783

What you need to do is implement your own TableModel. This tutorial on "How to Use Tables" would be helpful in understanding how tables work in Swing.

Basically, what you need to do is create a class that would extend AbstractTableModel and provide the implementation at least for the following three methods:

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

You can use your model so that it can hold its data in an array, vector, or hash map, or it might get the data from an outside source such as read a file and get data from it.

Upvotes: 3

Related Questions