Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Netbeans override paint() method in a JPanel

Hi I'm a newbie programmer
I want to develop a Java program's GUI using Netbeans IDE

Using Netbeans GUI Builder,
First, I create a new JFrame Form
Then, I add a JPanel from the toolbar/palette

Question is,
How can I override the paint() function of the newly created JPanel ?

I want to draw a background and some spheres inside the JPanel,
I tried using getGraphics() function to paint and draw, it does the job, but it won't draw anymore when I call repaint()

Should I create a new class implementing JPanel or JComponent, with my custom paint() function, instead ?
(If it so, how can I do it with Netbeans GUI Builder?)

Similar Question :

how to use jpanel with paint (or repaint)

(but it doesn't use Netbeans GUI Builder)

like image 323
topher Avatar asked Aug 24 '13 07:08

topher


2 Answers

The usual way to do this is to create your own JPanel subclass (e.g. MyJPanel) and implement the paint() method there.

After you implemented that class, switch to your form, select the panel and then use "Custom creation code" in the the "Code" tab of the panel's properties to create an instance of MyJPanel instead of JPanel

Custom creation code

This has the slight disadvantage that you will need to cast the instance variable to MyJPanel each time you want to access methods that are defined in MyJPanel but not in JPanel. If you never need to do that, this is the quickest method.

If you want to access additional methods in your panel class (without casting the instance variable each time), it's easier to remove the existing JPanel and add a "Bean" using the new class.

This is done by clicking on the "Choose Bean" button in the palette:

Choose bean

Once you click OK you can place the panel on your form and NetBeans will create an instance variable of the type MyJPanel (instead of JPane) and you can access all methods defined in that class. Note that the class must be compiled before you can add it that way!

like image 181
a_horse_with_no_name Avatar answered Nov 16 '22 02:11

a_horse_with_no_name


Hi I'm a newbie programmer I want to develop a Java program's GUI using Netbeans IDE

Hi! Since you are a newbie programmer, use Netbeans just as a regular text editor. In order to do it, create plain Java classes instead of Forms and do the coding yourself. The reason is that you have to understand Swing first, before having to deal with Netbeans approach.

Should I create a new class implementing JPanel or JComponent, with my custom paint() function, instead ?

You could certainly only extend JPanel or JComponent, since they are not Interfaces.

If you insist on using Netbeans Mattise GUI editor, then create a new JPanel Form and override it's paintComponent method.

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    // do your painting here...
    g.setColor(Color.RED);
    g.fillRect(6, 20, 12, 120);
    g.setColor(Color.WHITE);
    g.drawString("test", 50, 25);
}

Finally just add the component to your JFrame Form's constructor:

    add(new MyNewJPanel());
    pack();

Make sure that your JFrame's ContentPane has an appropriate layout.

like image 42
Costis Aivalis Avatar answered Nov 16 '22 00:11

Costis Aivalis