aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How do I repaint only portions of the JPanel and/or Frame? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do I repaint only portions of the JPanel and/or Frame?" Watch "How do I repaint only portions of the JPanel and/or Frame?" New topic
Author

How do I repaint only portions of the JPanel and/or Frame?

Nan Mroz
Greenhorn

Joined: Jul 06, 2011
Posts: 6
I am having a small issue with the MiniMusicService in Chapter 18 of the Head First Java book covering
Java 5.0. This is the chapter on remote deployment with RMI (pages 645 & 646).

When I select MiniMusic on the client side, it works fine to draw the colored rectangles with the music.
However, it also repaints the "Play it" button in an odd location and also repaints what appears to be
a label from the JComboBox in the left upper quadrant (not at 0,0).

Now the "Play it" button is not located in the paint method on the Server side. It was added to the main panel. The myPanel=new MyDrawPanel(), is also added to the main panel and it is this "JPanel" that should be repainted.

Is this an issue with the repaint? Do I need to add code somewhere to specifically say not to repaint something? Could it be a problem due to the client/server with RMI (all on my one machine)?

Thanks for your answers.

Nancy Mroz


Daniel Marti
Ranch Hand

Joined: Jun 08, 2011
Posts: 37
I am not in a place where i can test this for you, but try the following:
In the container that has your myPanel override

Inside it make a check:
if you need to repaint everything call super.paintComponent(g).
If you need to only repaint your myPanel call myPanel.repaint().

I am sure i have a piece of code for the same objective that you have somewhere in my old school projects, if by the time i get home you don't have a satisfactory answer, i'll try to dig it up.
Nan Mroz
Greenhorn

Joined: Jul 06, 2011
Posts: 6
Thanks Daniel,

I tried both, however, I get runtime errors when I try to make the paint component private. Something about not being able to override paint and give it weaker security. The message indicated it was protected (paint is in an inner class), however, I don't know why as usually it is set to "public".

Using the myPanel.repaint() didn't solve the issue either. I've attached a jpg of the output. Here is what should be happening.

server starts
client starts
client gets list of services
server sends list of services
client displays list in a JComboBox

User selects service(Visual Music Service)
client sends selection
server sends stub
client receives stub, calls service
server sends service (method getGuiPanel)
At this point, the "main panel" is drawn in the "browser" which includes a blank "myPanel" and a "Play it" button.

User hits the play it button. This activates the listener which sets up the midi sequence and plays it.
The midi sequence has an "event" which triggers the drawing of a random color rectangle (paintComponent).

Now the extraneous label and button are nowhere in the code to be repainted or in the event listener. The extraneous label was originally in the service list (JComboBox) and the button in the getGuiPanel method (they are all in the same file though).

Perhaps there is a cache somewhere that is not being cleared??





[Thumbnail for MiniMusicError.jpg]

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Have you gone through this tutorial?

And you may also need to go through this one.


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Daniel Marti wrote:In the container that has your myPanel override

Inside it make a check:
if you need to repaint everything call super.paintComponent(g).
If you need to only repaint your myPanel call myPanel.repaint().


No.
Nan Mroz
Greenhorn

Joined: Jul 06, 2011
Posts: 6
I've gone through the tutorials and it was a good review of what I learned in the Head First book. However, it did not help me with the issue. I do want to repaint the entire "myPanel" inside the client's GUI. I don't want to set the background or call super.paint() because then it will clear each previous rectangle.

What I am looking for is how to stop the repaint of the label and JComboBox line when the first repaint
happens. Actually, I would like to know how it got there in the first place. The flow happens this way:

The original JPanel (returned from the Server) is displayed containing myPanel and the button.
User hits play
On the first repaint only - the label and text string from the JComboBox shows up again and in the wrong place and a rectangle is painted.
Subsequent repaints only paint the rectangle which is correct.

Since the painting is triggered by the midi music events - I don't have a way to paint the background on
the first "repaint" unless I do it for all repaints.

Thanks for trying!


Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2213
    
    7
(they are all in the same file though).


Not sure what that means.

Components are added to a GUI and Swing will determine when the component needs to be repainted.

If you are dynamically adding or removing components from a panel then you need to do:



Then the layout manager will be invoked and the components will be layed out and painted properly.

If you are doing custom painting of some kind then I would guess you are not repainting the entire background before you paint the components so you have some garbage left over.
Nan Mroz
Greenhorn

Joined: Jul 06, 2011
Posts: 6
I played with changing background colors of the panels to try to figure out what was going on.
I think I understand what is going on but not how to fix it. I believe the Jpanel is being repainted when I click the button BEFORE the actual service starts painting with the repaint.
In the other services only a label is changed and the panel is repainted due to the change by the client-side code (where the GUI is actually being displayed).

In the MiniMusic Service, I believe the client side GUI (SerivceBrowser) is doing a repaint of something now containing garbarge before the service (running in the client at this point) starts its repaint. If it is repainting the original panel that it was sent, I don't know why that panel would now contain garbage.
I cannot figure out why it repainted since no labels or panels or sizes have changed until the service starts to repaint.

The only thing I know for sure is that it is the "myPanel" that is getting repainted.

I've played with the setOpaque(true) as well with no success.


Nan Mroz
Greenhorn

Joined: Jul 06, 2011
Posts: 6
I fixed the issue by setting a flag outside of the paint component so that the first time repaint
is called, the background panel is set to white and then I reset the flag inside the paint component. Then after that, the rectangles get painted on top of one another properly.

I still don't know why the other buttons and visual line shows through as those items were not
in the panel location in the first place.

Thanks to all for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I repaint only portions of the JPanel and/or Frame?