I'm currently creating software which demonstrates graph theory algorithms. It is possible to draw your own graph as input and there's a 'clear' button, when I click clear, it *sort* of clears it... meaning, if you drag the window to the side, it will clear. But as soon as I draw another vertex, the old graph comes back, but again if I drag the window out of sight (out of my monitors view) the old graph disappears again, leaving only the new graph. I vaguely remember similar graphical problems back in summer 2013 when I was designing some dissertation software. I thought it might be netbeans at first but I get the same problem with the jar file I've just generated. :/ .. Has anyone else experienced this with java Swing? Is it a common thing, or is it my computer/my code?
Well, my rule of thumb is always "It must be my fault". Blaming somebody else's software isn't useful behaviour because it doesn't lead you to a solution, but looking for the problem in your own code usually does. And when I say "usually", for me that meant "in the vast majority of cases". I almost never find a problem is caused by the standard API or the operating system, or even Netbeans or Eclipse.
So, what you're seeing when you hide and unhide your window is that the Swing code calls your window's paintComponent() method. It might call that method more often than you expect, too. So the chances are that inconsistent behaviour is caused by that method not behaving right. You should expect that method to be called at any time and you should code appropriately, i.e. the code should draw everything which should be displayed. Make sure it does that statelessly, i.e. it shouldn't try to remember what it drew the last time or anything like that.
(Also, there are -- or used to be -- strange display things happening if you mixed Swing components with old AWT components, but I can't tell from what I see in your post whether you did that.)
posted 4 years ago
Just to update: I forgot to include super.paintComponent. :/
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
how do I do my own kindle-like thing - without amazon