JScrollPane没有出现? [英] JScrollPane not appearing?

查看:135
本文介绍了JScrollPane没有出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的JScrollPane没有出现?如果我使用表(带注释的部分)来完成它,则工作正常,但是当我将其放入滚动窗格(我也需要,数据库确实很长)时,它永远不会出现,有人可以帮助我吗? :o)

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)

代码如下:

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

推荐答案

此处:

browserPanel.add(scrollPane);

如果已经显示了browserPanel,则应调用

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

来自 Container.add() javadoc:

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.

其他一些提示:

  • 正如@Andrew所说,在启动时添加表和滚动窗格通常比较容易.然后,在完成数据库访问后,使用ResultSet

必须在事件分发中创建和更新SWing组件线程.

如果您实际上正在这样做,则应避免在EDT中进行数据库调用.这些操作应该在单独的线程中完成,例如,使用 SwingWorker 确保在单独的线程中完成繁重的任务,并在EDT中执行GUI更新.

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.

这篇关于JScrollPane没有出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆