See my answer to this question . This is an example of the correct way to create a JPanel.
As other commentators / responders note, paintComponent belongs to the JPanel class. For you, this means that you need to create a class (let it be called MyPanel ) that extends JPanel. (Note: you can create a new .java file for this class, if you are in eclipse or make it an inner class, it should work anyway).
Once you have done this, simply cut out the paintCOponent method from the setup class and paste it into the new MyPanel class.
And finally, in your installation class, instead of creating a JPanel object, create MyPanel .
Basically, this MyPanel is your own JPanel that does whatever you want! It's almost like magic!
On the side of the note (this will hopefully help you format your code better in the future), and hopefully more experience Java programmers will agree with me on this, I also recommend that you create your own custom JFrame object like Well. Only for this you will not override any methods except constructor . Instead, in the constructor for this custom JFrame you will create all the specifications for the window (for example, calls to setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE and setSize(300,300) ). This constructor also allows you to instantiate the MyPanel object (as well as any other component objects in your window) and maybe give it some ActionListener s.
Then in another class (for example, your setup class) use the main method, which has 1 line: one that creates the JFrame object. This will automatically create a window.
Oh and one more vital thing: you have to call setVisible(true) on the JFrame if you want it to display. I am not sure why it is configured this way.
source share