aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes repaint happens only on mouseclick Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "repaint happens only on mouseclick" Watch "repaint happens only on mouseclick" New topic
Author

repaint happens only on mouseclick

Viktoria Silver
Greenhorn

Joined: Jul 01, 2010
Posts: 6
I need to draw a graph in my app.
I have created a separate class for it which extends jPanel.

when launching the app I create an instance of this class:

here's the constructor of that class if it matters any:

On a button click I launch the following code:

I can see in the console that every line of that code is passed, number 1 through 3 are printed. But nothing happens on the jPanel7. BUT when after the button press I click with the mouse in the area where the graph should be, it is drawn.

Here's the code of repaint, it works, but, as I said, only on a consequent mouse click.


Thank you for your time, it's appreciated.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
Bits of random code don't show the context of how your code is used.

Post your SSCCE that demonstrates the problem.
Viktoria Silver
Greenhorn

Joined: Jul 01, 2010
Posts: 6
got it solved, thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Would you like to share your solution, so others with the same problem can use it?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Viktoria Silver
Greenhorn

Joined: Jul 01, 2010
Posts: 6
It looks like repaint() works on Graphics class instance only if it has already been painted before. I don't know if there is a way to code an app initiated first time paint, but it is done automatically by system on some occasions.

My app has a jTabbedPane with multiple tab pages. I set it up in a way that the graph is first drawn (using repaint()) right after data is loaded into the app. it happens on the first tab, but the graph is on the third tab. Then when user clicks on the tab with the graph a system initiated first paint happens automatically.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

Viktoria Silver wrote:It looks like repaint() works on Graphics class instance only if it has already been painted before.

That's just wrong.

You were asked for an SSCCE. If you really want to know what's wrong with your code, now would be a good time to post it.


luck, db
There are no new questions, but there may be new answers.
Viktoria Silver
Greenhorn

Joined: Jul 01, 2010
Posts: 6
you're right it is a wrong process that gives a right result. I guess I don't really care about the process, I just need the result.
It took me less time to get to this right result the wrong way than it would take me to create SSCCE, wait if you can hint me to the right process and then implement it.

thanks for your passionate willingness to help.

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Viktoria Silver wrote:you're right it is a wrong process that gives a right result.


But it's not the right result as it will easily break.

I guess I don't really care about the process, I just need the result.


This won't work, not for programming.


It took me less time to get to this right result the wrong way than it would take me to create SSCCE, wait if you can hint me to the right process and then implement it.


We can't really hint if we don't have a decent understanding of what you're trying to do, hence the request for an SSCCE. My advice is still that you create one and post it so that we do have enough information to provide you with decent advice on how to do it right -- but only if you care about learning programming.
Sven Goetgeluck
Greenhorn

Joined: Jul 16, 2008
Posts: 14
hello,

try
SwingUtilities.UpdateComponentTree(optgr );

when you want to repaint

Sven
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

Sven Goetgeluck wrote:hello,

try
SwingUtilities.UpdateComponentTree(optgr );

when you want to repaint

Sven

Sven, please don't post bad advice. Which could only have been worse if you recommended a method with a somewhat similar name that actually exists. This one doesn't.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: repaint happens only on mouseclick