开发者

Why does java swing's setVisible take so long?

I am writing a JPanel that is loading labels in a flowlayout whose information is taken from a database table's column. I don't believe this is relevant though, because loading the data does not take long, only "setVisible" is taking a while to process.

The unfortunate effect I see is that the labels being loaded (let's say 100 labels) look like they're being placed one at a time. While it does happen within a second, I would much prefer it to ALL LOAD first, then simply be put on at once.

I always thought that when you add the component everything is already done, and setVisible(true/false) just flicks the visibility on and off. But due to this issue I see that perhaps that's not exactly how it works.

Here's my main method (I can provide more of my code if necessary):

    public static void mai开发者_C百科n(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                JFrame window = new JFrame();
                window.setBounds(100, 100, 450, 300);
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                window.getContentPane().setLayout(new BorderLayout());
                JobInfoPanel jobInfoPanel = new JobInfoPanel();
                window.getContentPane().add(jobInfoPanel,
                        BorderLayout.CENTER);
                window.pack();
                window.setVisible(true);        
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

The thing that confirms to me that the problem lies in setVisible is if I add the following code after window.setVisible(true); the same problem arises:

jobInfoPanel.setPanelListVisible(false);
jobInfoPanel.setPanelListVisible(true);

I've tried a lot of things and nothing seems to work, including placing "jobInfoPanel.setPanelListVisible(false);" before window.setVisible(true). The same thing occurs.

The only solution I think I am left with is somehow painting this panel off-screen and then moving it quickly to the correct location... if that makes any sense (I've read this as a suggestion on some pages, but this particular issue hasn't been brought up often).

Is there an easier way to do what I need?

Any and all help is much appreciated, thank you.

-Asaf


Panel that is loading labels in a flowlayout whose information is taken from a database table's column

That's job for JTable. You can find example here, This way you can reduce time needed for painting tons of JLabels.


I appreciate everyone's response, but was able to fix the issue by making a separate method which initialized this panel and calling it AFTER pack() in the main method, like this:

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                JFrame window = new JFrame();
                window.setBounds(100, 100, 450, 300);
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                window.getContentPane().setLayout(new BorderLayout());
                JobInfoPanel jobInfoPanel = new JobInfoPanel();
                window.getContentPane().add(jobInfoPanel,
                        BorderLayout.CENTER);
                window.pack();
                jobInfoPanel.createDriverListPanel();                   
                window.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

I can't entirely explain why that works, but it did exactly what I wanted it to do. Instead of seeing the labels appear one at a time, they all just pop at once.

Thank you again, -Asaf

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜