JScrollPane not appearing?

痞子三分冷 提交于 2019-12-24 19:27:54

问题


Why is my JScrollPane not showing up? if I do it with the table (commented part) it works fine, but when I put it into a scrollpane ( I need too, the data base is really long) it never appears, can anyone help me? :o)

Here's the code:

 JButton btnRefresh = new JButton("Refresh");
    btnRefresh.setBounds(130, 35, 80, 30);
    btnRefresh.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try{

                ResultSet r = s.executeQuery("Select * From "+comboBox.getSelectedItem());
                int rows = 0;
                while ( r.next ( ) ) rows++;
                r.first();
                int cols =  r.getMetaData().getColumnCount();


                String columnNames[ ] = new String [ cols ];
                for ( int i = 0; i < columnNames.length; i++ )
                    columnNames [ i ] = r.getMetaData().getColumnName(i + 1);

                for ( int i = 0; i < columnNames.length; i ++ )
                    System.out.print( columnNames[i] + " " );

                r.first();
                int i = 1;
                while(r.next()){
                    for ( int j = 0; j < cols; j++ ){
                        rowData[i][j] = r.getString(j+1);
                    }
                    i++;
                }


                table = new JTable( rowData, columnNames);
                //table.setBounds ( 10, 95, 770, 560 );

                scrollPane = new JScrollPane(table);
                //browserPanel.add( table);
                browserPanel.add(scrollPane);

                //table.repaint();
                scrollPane.repaint();
                //table.setVisible(true);
                scrollPane.setVisible( true );

                browserPanel.revalidate();
                browserPanel.repaint();

            }
            catch(Exception x){
                JOptionPane.showMessageDialog(null, "Not Found");
                x.printStackTrace();
            }
        }
    });
    browserPanel.add(btnRefresh);

回答1:


Here:

browserPanel.add(scrollPane);

If browserPanel has already been displayed then you should call revalidate() method after adding the scroll pane, like this:

browserPanel.add(scrollPane);
browserPanel.revalidate();
browserPanel.repaint();

From Container.add() javadoc:

This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to display the added component.

Some other tips:

  • As @Andrew says It is typically easier to have the table and scroll pane added at start-up. Then when the DB access is complete, set a new model with the ResultSet

  • Swing components must be created and updated in the Event Dispatch Thread.

  • If you are actually doing so then you should avoid make database calls in the EDT. These should be done in a separate thread using, for instance, a SwingWorker which ensures heavy tasks are made in a separate thread and GUI updates are performed in the EDT.



来源:https://stackoverflow.com/questions/20061200/jscrollpane-not-appearing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!