File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPanel Form Problems

 
Mikpo Siru
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1141
1
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2475
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10975
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this out for an example on drawing images on a panel
http://faq.javaranch.com/java/BackgroundImageOnJPanel
 
Mikpo Siru
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic