直到数据集映射结束,我才能看到 JFrame 组件.为什么? [英] I can't see the JFrame components until the end of dataset mapping. Why?

查看:35
本文介绍了直到数据集映射结束,我才能看到 JFrame 组件.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一款使用 neo4j 创建本体的软件.本体构建完成后,我开始将 200 万行的数据集映射到它,这大约需要 20 分钟才能完成.因此,我希望添加一个显示流程执行的 JFrame.下面的代码在开始时创建 JFrame,然后开始映射数据集.但是,我在执行过程中可以看到 JFrame,但是在映射完成后,它的组件出现在 JFrame 内部.我已经读到问题可能是由于缺少围绕代码的线程.谁能帮我解决这个问题??

I have developed a piece of software that creates an Ontology using neo4j. Once the ontology is built, I start mapping the dataset of 2 million rows to it, which takes more or less 20 minutes to be completed. As a result, I wished to add a JFrame that shows the process execution. The code below creates at the beginning the JFrame and then it starts mapping the dataset. However, I can see during the execution the JFrame, but its components appears inside the JFrame after the mapping finishes. I have read that the problem might be due to the lack of a thread that surrounds the code. Can anyone help me in solving this matter??

void createGraphDataset(String [][] choices , final ArrayList<String[]> DatabaseFile, GraphDatabaseService BORO_DB){

        JFrame converterFrame = new JFrame();
        converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        converterFrame.setBounds(100, 100, 650, 288);

                    JPanel contentPane = new JPanel();
        contentPane.setLayout(null);
        contentPane.setVisible(true);
        converterFrame.getContentPane().add(contentPane);

        JPanel panelNeo1 = new JPanel();
        panelNeo1.setBounds(6, 6, 638, 254);
        panelNeo1.setVisible(true);
        contentPane.add(panelNeo1);
        panelNeo1.setLayout(null);

        JLabel labelNeo1 = new JLabel("CSV BORO Converter");
        labelNeo1.setBounds(16, 19, 260, 37);
        panelNeo1.add(labelNeo1);
        labelNeo1.setVisible(true);

        JPanel panelNeo2 = new JPanel();
        panelNeo2.setBounds(16, 60, 605, 167);
        panelNeo1.add(panelNeo2);
        panelNeo2.setLayout(null);
        panelNeo2.setVisible(true);

        /*
        JProgressBar progressBar = new JProgressBar();
        progressBar.setBounds(27, 89, 547, 20);
        panelNeo2.add(progressBar);
        panelNeo2.setVisible(true);
        */

        JLabel labelNeo2 = new JLabel(" Processing: Number of row");
        labelNeo2.setOpaque(true);
        labelNeo2.setBounds(28, 36, 184, 20);
        panelNeo2.add(labelNeo2);
        labelNeo2.setVisible(true);

        JLabel labelNeo3 = new JLabel("");
        labelNeo3.setBounds(212, 36, 76, 20);
        panelNeo2.add(labelNeo3);
        labelNeo3.setVisible(true);

        JLabel labelNeo4 = new JLabel();
        labelNeo4.setText(String.valueOf(DatabaseFile.size()));
        labelNeo4.setBounds(311, 36, 70, 20);
        panelNeo2.add(labelNeo4);
        labelNeo4.setVisible(true);

        JLabel labelNeo6 = new JLabel("of");
        labelNeo6.setBounds(288, 36, 23, 20);
        panelNeo2.add(labelNeo6);
        labelNeo6.setVisible(true);

        converterFrame.setVisible(true);



        TopNode= new Node [DatabaseFile.get(0).length];


        //Create TopNodes
        Transaction tx0 = BORO_DB.beginTx();
            try{

                for(int u =0; u<DatabaseFile.get(0).length;u++){

                        TopNode[u]=BORO_DB.createNode();
                        TopNode[u].setProperty("name", choices[u][0]);

                }

                tx0.success();

            }
            finally{

                tx0.finish();

            }

        //Create the database 
        for(int i =0; i<DatabaseFile.size();i++){

        Transaction tx2 = BORO_DB.beginTx();

            try
            {

                // Nodes for each row
                Node []graphNode= new Node [DatabaseFile.get(i).length];

                // Relationships for each row ingoing
                Relationship [] graphRelOn = new Relationship [DatabaseFile.get(i).length-1];

                // Relationships for each row outgoing
                Relationship [] graphRelOut = new Relationship [DatabaseFile.get(i).length-1];

                //Relationship to TopNode ingoing
                Relationship TopNodeRelIn[]=new Relationship [DatabaseFile.get(i).length];


                            //Creates Nodes for row and relationship to TopNode

                            for(int j=0; j<DatabaseFile.get(i).length;j++){

                                    //Stores Database values
                                    String []ValuesRow =DatabaseFile.get(i);

                                    //Creates nodes for 1 row
                                    graphNode[j] = BORO_DB.createNode();
                                    graphNode[j].setProperty("name", ValuesRow[j]);

                                    //From row to TopNode Relationship (enter)
                                    TopNodeRelIn[j]=graphNode[j].createRelationshipTo(TopNode[j], RelTypes.typeInstances);
                                    TopNodeRelIn[j].setProperty("relationship-type", "typeInstances");  
                            }

                            //Creates Relationships

                            for(int k=0; k<(DatabaseFile.get(i).length)-1;k++){


                                    //Between same elements of the same row (left to right)
                                    graphRelOn[k]=graphNode[k].createRelationshipTo(graphNode[k+1], RelTypes.relatesTo);
                                    graphRelOn[k].setProperty("relationship-type", "relatesTo");

                                    //Between same elements of the same row (right to left)
                                    graphRelOut[k]=graphNode[(DatabaseFile.get(i).length)-1].createRelationshipTo(graphNode[(DatabaseFile.get(i).length)-(2+k)], RelTypes.relatesTo);
                                    graphRelOut[k].setProperty("relationship-type", "relatesTo");


                            }

                    tx2.success();
                }
        finally
        {
        tx2.finish();
        }        
        }   
    }

推荐答案

你说得对,你的问题是负责绘制组件的线程被你的操作阻塞了.然而,解释多线程编程的所有方面超出了单一答案的范围.

Your are right, your problem has to do with the problem that the Thread responsible for painting the components is blocked by your operation. However, explaining all aspects of multi-threaded programming is beyond the scope of a single answer.

作为一种解决方法,您可以延迟计算.

As a workaround you can just delay the computation.

// setup the GUI
frame.setVisible(true);
EventQueue.invokeLater(new Runnable() {
    public void run()
    {
      doYourHavyComputation();
    }
  });

这样您的计算仍然在同一个线程中运行并阻塞 UI,但会在稍后绘制初始帧内容时进行.

This way your computation still runs within the same thread and blocks the UI but at a later time when the initial frame contents has been painted.

如果你想进行真正的背景计算,你必须学习一个或多个关于多线程的教程以了解所有的复杂性,然后尝试它,如果你有更具体的问题,请回来.

If you wanna do real background computation you have to study one or more tutorials about multi-threading to understand all the complications then try it and come back if you have more specific questions.

这篇关于直到数据集映射结束,我才能看到 JFrame 组件.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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