[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

stop from re painting the Panel.

Posted on 2007-10-11
10
Medium Priority
?
241 Views
Last Modified: 2008-01-09
hi experts,
                I created a java Frame application .Frame has a panel in it and when ever when i minimize the window or focus is lost on the frame it keeps going back to paintComponent(Graphics g) method and repaints the whole panel is there a way to stop this as i dont want it to repaint as it creates something that i dont want to do.

Thanks in advance.
dex11


      
0
Comment
Question by:dex11
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20062386
If you want paintComponent() method to be executed only once you could do this by using one additional boolean variable:

...

private boolean alreadyPainted;

public void paintComponent(Graphics g) {
      if (!alreadyPainted) {
             alreadyPainted = true;
             // code for painting...
      }
}

...
0
 
LVL 92

Expert Comment

by:objects
ID: 20062396
no sorry thats how it works.
And if you don't repaint it then your panel will be blank :)

paintComponent() should paint always paint your component as you want it to appear.
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20062520
Yes, unfortunately, it will be blank. :-)

You can override update method:

public void update(Graphics g) {
      paint(g);
}

However, if you minimize or resize window everything that was painted will be lost, so this isn't what you need, either.

================================

Only solution as objects said is to make sure your paintComponent() method do always the same thing. If all external parameters that paint method uses for drawing stay the same then it will always draw the same thing. If this is not the case, you can buffer somewhere what you've painted first time and then draw it again every time paintComponent() method is called.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 20063485
>>... as it creates something that i dont want to do.

What do you mean by this?

0
 

Author Comment

by:dex11
ID: 20066360
What i have written in paintcomponent() is to shuffle a group of numbers and throw on the panel in a grid fashion.Everytime i minimize it or when focus is lost and  gained it would  reshuffle the numbers again and draw a completely different version than the prior one(panel before minimize) .So that is what i meant when i said "creates something that i dont want".Is there a way to stop this?Also  i am also using repaint method at different points so I dont want to use something like

public void paintComponent(Graphics g) {
      if (!alreadyPainted) {
             alreadyPainted = true;
             // code for painting...
      }
}

that would stop repaint method i.e. reshuffle numbers and create number grid when i want it.The problem here is it does it everytime i minmize or focus lost and gained.

thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20066780
It seems to me that you need to cache the data and simply control when those data change. paintComponent would simply represent the cache, which would change only when you tell it to
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 20069429
> What i have written in paintcomponent() is to shuffle a group of numbers and throw on the panel in a grid fashion

you should instead be doing your shuffling *outside* paintComponent().
You should have no program logic inside paintComponent() as you cannot control  when/how often it gets called.

You need to determine when the numbers needs to be shuffled and implement that in your app instead of doing it in paintComponent()

> Is there a way to stop this?

no and u don';t want to.
If you did stop it your panel would be blank or partially blablank. It gets called because it *needs* to repaint it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20070421
UrosVidojevic, can you tell me please how the accepted answer differs from what i'd already suggested?
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20070643
Hi CEHJ,

I'm not the author of the question, so I didn't accept solution :-).
Maybe you have confused me with author of this question?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20070823
>>I'm not the author of the question, so I didn't accept solution :-).

Sorry UrosVidojevic - i blame the poor layout, where the most important name, i.e. the name of the questioner in the very first posting gets one of the least prominent styles and is badly positioned ;-)

So, dex11, can you tell me please how the accepted answer differs from what i'd already suggested?
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question