#Java adding JPanel does not work correctly

20 messages · Page 1 of 1 (latest)

tender kernel
#

https://pastebin.com/sfbuvx6m full code
Sometimes it works (Input between 1-17)
What di i mean with sometimes?
It is random if it will show the output or not. (same input)

public static void main(String[] args) {
        JFrame frame = new JFrame("Widerstand");
        frame.setSize(1000, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel currentPanel = new JPanel();
        frame.add(currentPanel);
        while(true)
        {
            frame.remove(currentPanel);
            int select = Integer.parseInt(JOptionPane.showInputDialog("Which Version?"));
            Anordnung anordnung = Anordnung.values()[select];
            currentPanel = new Darsteller(anordnung, "100", "200", "300", "400");
            frame.add(currentPanel);
            frame.revalidate();
            frame.repaint();
            frame.setVisible(true);
        }
    }
raven cypressBOT
#

This post has been reserved for your question.

Hey @tender kernel! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

snow kestrel
#

I mean, for starters GUIs just don't have to do anything predictible when you don't program them like they tell you to do.
Did any official documentation tell you you could program Swing that way? If not then you don't have grounds to expect for anything.
I understand the adventurer and trying to hack things deviating from the normal rules attitude that come from programming, and it's normal, but that doesn't work with GUIs, end of story. You follow the rules or any expectation is stupid.

Secondly the only time your program starts waiting and letting the windows draw themselves, is when you ask the user to select something, which is right after you remove the panel. What you should intuitively expect is that nothing is ever displayed.
In practice though, what will happen will be what will happen, GUIs must be programmed correctly.

tender kernel
#

You saw that i am adding it right after it again?

snow kestrel
#

.... So what? What matters should intuitively be that you remove it before

tender kernel
#

if i am not removing/adding it it displays nothing

snow kestrel
#

I mean you pretty much have to add it otherwise yeah, curiously nothing is drawn (though I would have rather put one component that draws itself in different ways instead of constantly adding/removing components.)
However it would have sounded like it makes more sense to remove the component right before adding another one, not with user interaction in-between

#

Of course, that's still not programming correctly and there is still no guarantee of anything, so I suppose maybe, it convinced you in some ways that you got "better" results writing like that. But at the end of the day results need to be good, not better.

tender kernel
#

So you also dont know why it performs like that

snow kestrel
#

Of course not, why would I want to know what GUI libraries do when you call them randomly?

tender kernel
#

randomly?
its a class which extends a JPanel in there is a paintComponent method which based on input drawn rectangels onto the JPanel

snow kestrel
#

Yeah, and you call GUI-building code in an infinite loop with a blocker pop-up to let the loop rest. And those components you define have no size. Randomly, yes. Did anything anywhere tell you you can do these sort of things?

tender kernel
#

Got it working with a keylistener i think the programm had not enough time to draw the panel before the JOptionPane was called again

#

but again only with first removing and then adding it back

#

without that it is not working

atomic rune
# tender kernel but again only with first removing and then adding it back

sure because that is how its supposed to behave. Once you add the component to the frame you can only change its internal state (through a reference to said component). Changing the object entirely in your separate array won't have any effect on the object currently being displayed. If you don't want to create a new object each time you change the version work with getters and setters.

#

btw its Widerstände not Wiederstände

tender kernel
#

Ja guut