This is what the code looks like:
class Main extends JFrame {
public MyPanel panel;
public Main() {
//all the frame init stuff
panel = new MyPanel(this);
Panel badPanel = new Panel();//this makes the remove method go veryy slow
//add(badPanel, BorderLayout.SOUTH);//
JPanel goodPanel = new JPanel();
add(goodPanel, BorderLayout.SOUTH); // this fixes the slowness of the remove method in calculate()
add(panel, BorderLayout.CENTER);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Main main = new Main();
}
});
}
}
class MyPanel extends JPanel {
Main main;
public MyPanel(Main main) {
this.main = main;
//init everything
}
public void calculate() {
MyPanel newPanel = new MyPanel(main);
//do some computation
main.remove(main.panel);
main.add(newPanel, BorderLayout.CENTER);
main.panel = newPanel;
main.revalidate();
}
}
So everything works fine, it's just for some reason when it gets to the remove()
method, the execution time is so slow, it pauses for at least 5 seconds and then finishes the rest of the lines. I tried commenting it out so I know that's the line causing the problem.
Anyone know what is going on?
edit: so this is basically whats going on.. i honestly dont know what else i need to show you, nothing else in the code has anything to do with the problem im experiencing. if i comment out the remove method, everything works quickly, but when its there it goes very slowly.