• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPanel custom painting in a generated JFrame in Netbeans.

 
Greenhorn
Posts: 13
Netbeans IDE Netscape Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems to be a simple question, but I am just the guy to not know it.
I have a generated JFrame in Netbeans (Right click the package and select "JFrame") and added a JPanel as a variable.
I cannot implement an external JPanel (public class Panel1 extends JPanel) because I haven't found a way to do so.
So, with the variable JPanel added to the frame, how do I override its paintComponent(Graphics g) method?
Placing the overriding method inside the JFrame form doesn't work (I call repaint(); in the post-creation code of the Panel and whenever I click within the frame.)
There is a debugging statement (System.out.println("paintComponents was called!");) and the message never appears.
I know that this is a lot of writing, but being a writer, I just have top provide full coverage.
Thanks for your answers!
 
Bartender
Posts: 5491
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Xander,

you can do it in the following way:

1) in the GUI Builder, select the panel by clicking on it
2) right click, and from the menu select 'Customize Code'
3) in the window that pops up, you see the default initializer
4) in the left column, you see a combobox that has as title 'default code'
5) click on the small arrow, and click 'Custom Creation'
6) now you can edit the code, especially, between the last ')' and the ';' type a '{' and press return
7) now you can type the '@Override' and the custom paintComponent

But if you are a beginner, I would not advise to use a GUI Builder (using NetBeans is oke!).
Try to create a JFrame in code, and add a JPanel with an overriden paintComponent to it.
This way you have a much better view of what's going on, and you can gain experience in using a LayOutManager.

Greetings,
Piet

 
Xander Jacob
Greenhorn
Posts: 13
Netbeans IDE Netscape Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have, in the Custom Editor,

panel1 = new java.awt.Panel() {
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(1, 1, 99, 99);
}
};

but an error occurs. Apparently, my @Override annotation is incorrect, as public void paintComponent(Graphics g) doesn't override what is found in JComponent.
I've made a different overriding method in another file and public void paintComponent(Graphics g) matches the signature of what is found in JComponent.
Is there a different painComponent method that I should be using or did I place it incorrectly (Between the last ")" and the ";" )?
 
Xander Jacob
Greenhorn
Posts: 13
Netbeans IDE Netscape Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*Shyly*
Remember how I mentioned that it was a JPanel? From javax.swing?
Well, my brain saw the chance and took it.
I used a java.awt.Panel instead of the javax.swing.JPanel that we've been talking about.
Stupid brain.
But, without that error, it works perfectly! Thanks!
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...and added a JPanel as a variable...


No, you are using java.awt.Panel, hence you should override the paint() method.

And welcome to the Ranch!

EDIT: Well, I guess you typed it faster.
 
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic