JavaSwing将多个JTable添加到JFrame

问题描述 投票:0回答:2

我正在使用Java swing应用程序。我必须浏览多个CSV数据文件,并使用JTable将它们显示到JFrame中。我正在尝试将多个(非静态)JTable添加到JFrame中。我想要包含在其中的所有(N)JTable一个JSrollPane。目前,我只能向JScrollPane显示1个表。

提前感谢!

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


                }
java swing jtable jpanel jscrollpane
2个回答
1
投票
JScrollPane sp = new JScrollPane(j); 
sp.setBounds(10, 200, 780, 440);
add(sp);

0
投票

为了将每个JTable显示到JFrame中,我将所有JTable添加到一个JPanel中,然后将此JPanel添加到JScrollPane中。显示每个JTable之后,问题是我们无法显示每个JTable标头。我将每个JTable标头添加到另一个JPanel中,上]。

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



            }
© www.soinside.com 2019 - 2024. All rights reserved.