-2

I've been having a problem with my program that has been driving me crazy and I cannot understand why this is happening. I have a GUI that when the "Add" button is pressed, a new system listing appears (Which is a class called SystemPanel that extends JPanel and cotains system details, which is created and then put into the Frame's main panel.)

To put it shortly, when I try to add a new SystemPanel, it does not appear for whatever reason. I have code using JSch that connects to the system and verifies whether its processes are online or not, but the line of code that does this is after the creation of the SystemPanel. It is only after the code for testing the processes of the system are executed that the SystemPanel becomes visible, and I can't understand why this is the case. Here is the code for adding a new SystemPanel:

public void actionPerformed(ActionEvent e) {
    //If the "Add" button is pressed
    if (e.getActionCommand() == "Add") {

        PopupWindow popup = new PopupWindow(this);
        popup.setVisible(true);
        String[] results = popup.getResults();
        if (results[0] != null && results[1] != null && results[2] != null && results[3] != null && results[4] != null) {

            SystemPanel newSystem = new SystemPanel(this, results[0], results[1], results[2], results[3], results[4]);

            systemsPanel.add(newSystem);
            revalidate();
            systemsList.add(newSystem);
            System.out.println("Did the stuff");

            boolean[] status = SystemChecker.checkOnline(results[0], results[1], results[2], results[3]);
        }   
    }
}

The PopupWindow is a custom JDialog that allows the user to enter the required information which is returned in a String array and is used to create a new SystemPanel. The checkOnline function grabs the user's inputs and uses them to connect to the system and determine whether the processes are working or not, and returns the results into a boolean array, true for working, false for not.

What's even weirder is that I have another part of my program that reads from an .ini file to obtain existing systems and then creates SystemPanels based on the data that it reads. Through this method, the SystemPanels are added the way I want and work perfectly for some reason, even though the code for adding the panels is hardly any different. Code:

    for (int i = 0; i < systems.size(); i++) {
        SystemPanel newSystem = new SystemPanel(this, systems.get(i)[0], systems.get(i)[1], systems.get(i)[2], systems.get(i)[3], systems.get(i)[4]);
        systemsPanel.add(newSystem);
        revalidate();
        systemsList.add(newSystem);
    }

    for (int i = 0; i < lineNum; i++) {
        boolean[] status = SystemChecker.checkOnline(systems.get(i)[0], systems.get(i)[1], systems.get(i)[2], systems.get(i)[3]);
        systemsList.get(i).updateIcons(status);
    }

This code grabs the details from the file and then makes the SystemPanels based on those details. Here, all of the SystemPanels are added and show up before the connection is tested, which is what I want to happen when I add one normally.

Why is it that the SystemPanel doesn't appear until the connection is tested, even though the code for displaying the SystemPanel is executed before the connection test? Any help would be greatly appreciated, thanks.

0Calories
  • 25
  • 8
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 3) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). However if it is the same component being added each time (like a `SystemPanel`) then it would make more sense to refresh the detail in the existing panel, than to create a new one. – Andrew Thompson Jul 05 '16 at 00:12
  • 4) Is your question *"How to X?"*? If so, add it as an [edit to the question](http://stackoverflow.com/posts/38190025/edit). If not, think of a specific question and do the same (edit). – Andrew Thompson Jul 05 '16 at 00:13
  • Hi, I've made the question shorter and easier to read through. A CardLayout isn't the solution to my problem, as it's a problem with a new SystemPanel becoming visible rather than a layout problem. It's also not the same component being added each time, it has different values and data depending on what the user enters into the JDialog, which is why I'm creating a new one each time. – 0Calories Jul 05 '16 at 12:58
  • *"I've made the question shorter and easier to read through"* I did not advise you to do that. Read my comment again. Follow both links in the first suggestion & read them both, carefully. BTW - there is still no bloody '?' in that entire mess of words and symbols above. If there is a question hidden in there, mark it at the end with a question mark! – Andrew Thompson Jul 05 '16 at 13:02
  • Well I read both articles and I can't really apply them to this problem, since there would be nothing minimal about the code to reproduce this problem... this issue isn't something that can be reproduced without a lot of code. I wanted to highlight the code where the problem actually occurs and demonstrate it with screenshots, rather than have an even bigger mess of code on this post than I already have so that it wouldn't inconvenience anyone. Also, this was more of a problem with my code rather than a "How to X?" thing, which is why I didn't have a question mark. – 0Calories Jul 05 '16 at 14:14

1 Answers1

0

Try it of the current event queue handling, on which actionPerformed is done.

public void actionPerformed(ActionEvent e) {
    EventQueue.invokeLater(() -> { ... your code here ... });
}

Also you cannot add the same component to two parents, every component object has a single parent (container).

(Java 8 notation)

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Thank you very much Joop, this fixed my problem! Also I'm not sure what you mean by adding the same component to two parents, but I'm only adding to a single parent. I may have been a bit unlear with that, sorry. – 0Calories Jul 05 '16 at 14:11
  • newSystem is added to systemsPanel and systemsList. I guess systemsList is not a component but a List or such. – Joop Eggen Jul 05 '16 at 14:29
  • Yes, sorry, it's a LinkedList and systemsPanel is the JPanel containing the SystemPanels. – 0Calories Jul 05 '16 at 14:31