aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JPanel Form Problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPanel Form Problems" Watch "JPanel Form Problems" New topic
Author

JPanel Form Problems

Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi,

I am having two problems with JPanels:

#1: I make a new JPanel Form, and override it's paintComponent() so that that the background always fills with yellow. Then I drag and drop this panel onto a JFrame form.

The problem: When I run the program, the JPanel doesn't become yellow. From the debugger it seems the overriden paintComponent() method is not being called.

#2: I make a new JPanel form, and add a method to it called 'testMethod()'. I drag and drop this panel onto a JFrame form.

The problem: I can't access testMethod() from the panel's reference (which is in the JFrame). This is even when I use "customize code" so that the declaration of the panel uses my new class rather than the superclass JPanel. When I explicitly cast the reference to my class' type, I can see testMethod(), but an exception is generated when run.

Any suggestions how to get round these problems and why they occur?

Thanks
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1134

1. Post your code.
2. What IDE are you using?
3. What is a JPanel Form ?
4. What is a JFrame Form ?
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi,

Main class:


JFrame Form class (Netbeans generated all of this code automatically):


JPanel Form class (Netbeans generated all of this code except for the overriden paintComponent() method):


I am using Netbeans 6.7.1.

The "Form" is the graphical interfaces Netbeans has, which you can drag and drop components onto and code is automatically generated.

In the code above, the paintComponent() method of the JPanel is overidden so it fills yellow. I coped this panel onto the JFrame, so when the program is run, the panel inside the JFrame should be yellow. But it is not changing colour.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
In the code above, the paintComponent() method of the JPanel is overidden so it fills yellow.


Why did you override the paintComponent() method? All you need to do is:



Don't use the IDE to create your form. Learn how to write the code yourself so you understand what is going on.
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi,

Thanks for the tip. The above works if I want to set the background (which is what I asked for). What I actually need for the application is to draw an image on the panel. So I try:



But it's not having any effect. In the actual code I'd use drawImage() of Graphics2D.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Check this out for an example on drawing images on a panel
http://faq.javaranch.com/java/BackgroundImageOnJPanel


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi,

I need to change the image during runtime, so I add a method to the above example that allows one to change the referenced image object. The problem is I cannot access this new method from the JPanels instance in the JFrame.

So for example, if I do this in pseudo-code:


If I drag and drop this JPanel into a JFrame, then I can't access the public method changeImage from that reference in the JFrame.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPanel Form Problems