Fation Hadri
Fation Hadri

Reputation: 150

JavaSwing add multiple JTable to JFrame

I am working on java swing application.I have to browse multiple CSV data files and display them into JFrame using JTable.I am trying to add multiple(non static) JTable into JFrame.All (N) JTable i want to include inside a JSrollPane.At this moment i can display only 1 table to JScrollPane.

Thanks in advance!

if (e.getSource() == jbtreadbrowsefile) {

            try {

                for(int k=0;k<file_locations.length;k++) {

                    String currentLocation=file_locations[k];
                    inputList = new ArrayList();
                    File inputF = new File(currentLocation);
                    InputStream inputFS = new FileInputStream(inputF);
                    BufferedReader br = new BufferedReader(new InputStreamReader(inputFS));
                    inputList = br.lines().collect(Collectors.toList());
                    br.close();

                       int count = inputList.size();

                       if(count>0)
                       {
                           data = new String[count - 1][8];
                           for (int i = 0; i < count - 1; i++) {

                           String[] arrOfStr = inputList.get(i + 1).toString().split(",");
                           String test1= arrOfStr[0];
                           String test2= arrOfStr[1];
                           String test3= arrOfStr[2];
                           String test4 = arrOfStr[3];
                           String test5= arrOfStr[4];
                           String test6= arrOfStr[5];
                           String test7= arrOfStr[6];

                           data[i][0] = "" + (i + 1);
                           data[i][1] = test1;
                           data[i][2] = test2;
                           data[i][3] = test3;
                           data[i][4] = test4;
                           data[i][5] = test5;
                           data[i][6] = test6;
                           data[i][7] = test7;

                       }

                       j = new JTable(data, columnNames);
                       TableColumnModel tcm = j.getColumnModel();
                       tcm.getColumn(0).setPreferredWidth(40);
                       tcm.getColumn(1).setPreferredWidth(220);
                       tcm.getColumn(2).setPreferredWidth(120);
                       tcm.getColumn(3).setPreferredWidth(80);
                       tcm.getColumn(4).setPreferredWidth(80);
                       tcm.getColumn(5).setPreferredWidth(80);
                       tcm.getColumn(6).setPreferredWidth(80);
                       tcm.getColumn(7).setPreferredWidth(80);

                       DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
                       centerRenderer.setHorizontalAlignment(JLabel.CENTER);
                       j.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);

                       for (int i = 0; i < j.getRowCount(); i++) {
                           j.setRowHeight(i, 20);
                       }

                       j.getTableHeader().setFont(new Font("SansSerif", Font.BOLD, 15));
                       j.getTableHeader().setReorderingAllowed(false);
                       j.repaint();



                       JScrollPane sp = new JScrollPane(j); 
                       sp.setBounds(10, 200, 780, 440);
                       add(sp);



                       }
                   else
                       {
                           JOptionPane.showMessageDialog(this,"No Data Found in the File");
                       }


                }

Upvotes: 0

Views: 615

Answers (2)

Fation Hadri
Fation Hadri

Reputation: 150

For displaying each JTable into JFrame,i added all JTable into a JPanel and added this JPanel into JScrollPane.After displaying each JTable the problem was that we can not display each JTable header.I added to another JPanel each JTable header and it's shows up.

if (e.getSource() == jbtreadbrowsefile) {

            GridLayout grid = new GridLayout(0, 1, 30, 20);
            jpaneltable=new JPanel(grid);
            try {

                for(int k=0;k<file_locations.length;k++) {

                    String currentLocation=file_locations[k];
                    inputList = new ArrayList();
                    File inputF = new File(currentLocation);
                    InputStream inputFS = new FileInputStream(inputF);
                    BufferedReader br = new BufferedReader(new InputStreamReader(inputFS));
                    inputList = br.lines().collect(Collectors.toList());
                    br.close();

                       int count = inputList.size();

                       if(count>0)
                       {
                           data = new String[count - 1][8];
                           for (int i = 0; i < count - 1; i++) {

                           String[] arrOfStr = inputList.get(i + 1).toString().split(",");
                           String test1= arrOfStr[0];
                           String test2= arrOfStr[1];
                           String test3= arrOfStr[2];
                           String test4= arrOfStr[3];
                           String test5= arrOfStr[4];
                           String test6= arrOfStr[5];
                           String test7= arrOfStr[6];

                           data[i][0] = "" + (i + 1);
                           data[i][1] = test1;
                           data[i][2] = test2;
                           data[i][3] = test3;
                           data[i][4] = test4;
                           data[i][5] = test5;
                           data[i][6] = test6;
                           data[i][7] = test7;

                       }

                       j = new JTable(data, columnNames);
                       TableColumnModel tcm = j.getColumnModel();
                       tcm.getColumn(0).setPreferredWidth(40);
                       tcm.getColumn(1).setPreferredWidth(220);
                       tcm.getColumn(2).setPreferredWidth(120);
                       tcm.getColumn(3).setPreferredWidth(80);
                       tcm.getColumn(4).setPreferredWidth(80);
                       tcm.getColumn(5).setPreferredWidth(80);
                       tcm.getColumn(6).setPreferredWidth(80);
                       tcm.getColumn(7).setPreferredWidth(80);

                       DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
                       centerRenderer.setHorizontalAlignment(JLabel.CENTER);
                       j.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);

                       for (int i = 0; i < j.getRowCount(); i++) {
                           j.setRowHeight(i, 20);
                       }

                       j.getTableHeader().setFont(new Font("SansSerif", Font.BOLD, 15));
                       j.getTableHeader().setReorderingAllowed(false);

                       j.getTableHeader().setPreferredSize(new Dimension(0,HEADER_HEIGHT));
                       j.repaint();


                       jpaneltable.add(j.getTableHeader(),BorderLayout.NORTH);
                       jpaneltable.add(j, BorderLayout.CENTER);


                       }
                   else
                       {
                           JOptionPane.showMessageDialog(this,"No Data Found in the File");
                       }


                }

                JScrollPane sp = new JScrollPane(jpaneltable,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
                sp.setBounds(10, 200, 780, 440);
                add(sp);



            }

Upvotes: 0

camickr
camickr

Reputation: 324108

JScrollPane sp = new JScrollPane(j); 
sp.setBounds(10, 200, 780, 440);
add(sp);

Don't use setBounds(). It is the job of the layout manager to determine the size/location of a component.

At this moment i can display only 1 table to JScrollPane.

Correct. A JScrollPane is designed to display a single JTable. The header of the JTable will be displayed at the header view of the scroll pane and the table will be displayed in the center of the scroll pane. You may then also see scrollbars in the scroll pane depending on the data in the table.

If you want to "merge" data from multiple files into the same table, then you need to update the TableModel with the data from each file instead of creating a new table each time.

You can dynamically add data to the model by using the addRow(…) method of the DefaultTableModel.

Upvotes: 1

Related Questions