Can you stop a program repainting a panel when the program is minimized? * Used Swing
Asked
Active
Viewed 947 times
0
-
1Using what framework? Swing? AWT? SWT? Other? – Ted Hopp Mar 07 '12 at 17:35
-
1Why does it matter if repainting occurs? This makes me suspect a bigger problem is lurking underneath such as having program logic in your `paint` or `paintComponent` method. – Hovercraft Full Of Eels Mar 07 '12 at 17:43
-
In my program I have a thread that calls repaint every 1 sec which repaints a graph that simulates a bank account balance. if I repaint when I shouldn't this throws off the graph – Chris Mowbray Mar 07 '12 at 17:47
-
http://www.filedropper.com/assigment31 – Chris Mowbray Mar 07 '12 at 17:49
-
@Chris everything depends of Executor or Timer's type – mKorbel Mar 07 '12 at 17:50
-
1Your Jar file has no source code. More importantly, I did some *major* work trying to help you in this thread: [graph-plotting-issue](http://stackoverflow.com/questions/9557633/graph-plotting-issue) including a demonstration program, and you didn't reply. What's with that?? I felt as if I wasted a great deal of time with this. That's not how to motivate folks to help you further you know. – Hovercraft Full Of Eels Mar 07 '12 at 17:57
-
Im real sorry man i thought i replied :/ the help was greatly appreciated :) – Chris Mowbray Mar 07 '12 at 18:06
-
@HovercraftFullOfEels can you not execute the .jar file – Chris Mowbray Mar 07 '12 at 18:09
-
Sure one can execute, but source is everything. Without source, we have no idea why your application is behaving as it's behaving. – Hovercraft Full Of Eels Mar 07 '12 at 18:11
-
I know i was just showing what the problem was, im having trouble understanding what the timer class has to do with the JFrame repainting. – Chris Mowbray Mar 07 '12 at 18:12
-
If you take my sample program from my previous question, make the Timer a final local variable, and add a WindowListener or WindowAdapter to the JFrame you can see a solution. – Hovercraft Full Of Eels Mar 07 '12 at 22:15
2 Answers
2
The real issue, I guess, is suspending animation loops and the like when minimized. If you're using a JFrame
, you can detect window minimization events with:
myFrame.addWindowStateListener(
new WindowStateListener() {
@Override
public void windowStateChanged(WindowEvent evt) {
if (myFrame.getState() == Frame.ICONIFIED) {
// suspend painting/animation loops
} else {
// resume or continue painting/animation loops
}
}
}
);

Ted Hopp
- 232,168
- 48
- 399
- 521
-
@Chris: look at methods available for Swing Timers. You'll figure it out. – Hovercraft Full Of Eels Mar 07 '12 at 18:07
-
@Chris - If your animation is driven by a `javax.swing.Timer`, you can simply call the timer's `stop()` and `start()` methods to suspend/resume the animation. See the article [Threads and Swing](http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html) for some sample code. – Ted Hopp Mar 07 '12 at 18:13
-
There's a `Timer` related example [here](http://stackoverflow.com/a/5529043/230513). – trashgod Mar 07 '12 at 18:32
-
@TedHopp i am familiar with those methods as i have used them in this program on the start and stop buttons on the jar file above – Chris Mowbray Mar 07 '12 at 18:38
-
@Chris - So why can't you just use them to suspend and resume the painting? It's easy to use a `Timer` object to drive the animation, as the link in my previous comment shows. – Ted Hopp Mar 07 '12 at 19:02
-
@TedHopp The frame is always showing for some reason, it never reaches the else when I minimize – Chris Mowbray Mar 07 '12 at 20:25
-
@Chris - I modified the logic for testing the window state. See if using `getState()` provides better behavior. – Ted Hopp Mar 07 '12 at 20:40
-
@TedHopp I have used .getExtendedState() == frame.ICONIFIED i think it works, i have declared my timer in another class, how can i call that timer to stop and start from the JFrame class i am currently in? – Chris Mowbray Mar 07 '12 at 20:46
-
@Chris You'll need to either make the timer visible to the JFrame code or else provide methods in the other class to relay the start/stop requests to the timer. (I'd recommend the latter.) – Ted Hopp Mar 07 '12 at 20:54
-
@TedHopp Thanks for your help ive done that and i dont think it has fixed my problem, Thankyou anyway – Chris Mowbray Mar 07 '12 at 20:55
1
An example of using a WindowListener could be as simple as making a small modification to my previous example which can be found here:
private static void createAndShowGui() {
ShowGraph showGraphPanel = new ShowGraph(MAX_POINTS);
TimerListener timerListener = new TimerListener(MAX_POINTS, showGraphPanel);
JFrame frame = new JFrame("TestShowGraph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(showGraphPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
// Timer now made a final variable so it can be referred to
final Timer timer = new Timer(TIMER_DELAY, timerListener);
timer.start();
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent arg0) {
timer.start();
}
@Override
public void windowIconified(WindowEvent arg0) {
timer.stop();
}
@Override
public void windowDeiconified(WindowEvent arg0) {
timer.start();
}
@Override
public void windowActivated(WindowEvent arg0) {
timer.start();
}
});
}

Community
- 1
- 1

Hovercraft Full Of Eels
- 283,665
- 25
- 256
- 373