• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JPanel Form Problems

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
rubbery bacon. crispy tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic