Displaying a loading JFrame while a loop is running in main thread

[亡魂溺海] 提交于 2020-01-04 09:13:08

问题


this is my first post so please inform me if I'm doing anything wrong. I've been working on a mini-applet to get the status of several nodes. This works by having a thread of each node which checks its uptime in a network class. Once it gets the status of the node it'll put it into a hashmap. I then check the status of that hashmap and wait till its content amount is equal to the total amount of nodes that we're checking. Once done it proceeds to display them on a screen. The problem is despite making this a heavily threaded application fetching the status for all nodes still takes a few seconds. During this time I want to display a separate JFrame with a loading bar (or even use the same JFrame if it would work.

I'm not going to lie I'm a Java newbie and the code is horrible, I'm wondering if you guys could give me advice as to what I should do. Here is the code.

--main node status window--

public NodeStatusWindow() {
    super(Managment.getTitle() + " - Nodes");
    setLayout(new FlowLayout());

    Thread threada = new Thread(new ShowLoading());
    threada.start();

    System.out.println("Checking node status...");

    Nodes.nodeOnlineCurrent.clear();

    for (int i = 0; i < Nodes.getNodes().size(); i++) {
        Object currentNode = Nodes.getNodes().keySet().toArray()[i];//

        Thread thread = new Thread(new CheckUptime((int) currentNode));
        thread.start();
    }

    while (Nodes.nodeOnlineCurrent.size() < Nodes.getNodes().size()) {
        System.out.println(Nodes.nodeOnlineCurrent.size() + " < " + Nodes.getNodes().size());
    }


    for (int i = 0; i < Nodes.nodeOnlineCurrent.size(); i++) {
        JLabel[] nodeList = new JLabel[Nodes.getNodes().size()];
        Object currentNode = Nodes.getNodes().keySet().toArray()[i];
        nodeList[i] = new JLabel("Node: " + currentNode.toString());

        nodeList[i].setOpaque(true);

        if (Nodes.nodeOnlineCurrent.get(currentNode)[0] == false) {
            nodeList[i].setForeground(new Color(255, 0, 0)); //Red
        } else if (Nodes.nodeOnlineCurrent.get(currentNode)[1] == false) {
            nodeList[i].setForeground(new Color(230, 222, 0)); //Yellow
        } else {
            nodeList[i].setForeground(new Color(0, 230, 0)); //Green
        }

        add(nodeList[i]);
    }

--Loading window. This class is created in the threada object in the class above--

public class NodeStatusLoadingWindow extends JFrame {
    private static final long serialVersionUID = 1369511477485416862L;

    private JLabel status;

    public NodeStatusLoadingWindow() {
        super(Managment.getTitle() + " - Nodes");
        setLayout(new FlowLayout());

        status = new JLabel("Loading...");
        add(status);
    }

    public void closeWindow() {
        this.setVisible(false);
        this.dispose();
    }
}

Here is the showLoading thread

public class ShowLoading implements Runnable {
    public void run() {
        try {
            GUIHandler.drawNodeStatusLoadingWindow();
        } catch (Exception e) {
            System.out.println("Exception! " + e.toString());
        }
    }
}

来源:https://stackoverflow.com/questions/12937759/displaying-a-loading-jframe-while-a-loop-is-running-in-main-thread

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