File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JPanel painComponent not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPanel painComponent not working" Watch "JPanel painComponent not working" New topic

JPanel painComponent not working

Sergey Schek

Joined: Jul 26, 2009
Posts: 5
I dont know how to best put it, but that seems to be whats happening here. I started a project recently to use up some of the free time I have and decided that Id attack the GUI first, to a degree. Well, ran into my first major problem already, less than 100 lines of code into the project. What Im trying to do is to have a main JFrame that will contain all of the program's graphical elements, and its meant to be without a title bar or any other decoration ( exit using esc key for now). To that I am trying to add a JPanel that will function as a menu ( I want to make it pretty in a sort of way that would be hard using buttons and dialogs.) Now to test out that the panel is working fine, I tried to make it draw a rect....which it didnt. I made sure to set the color, check if the panel is visible, and do all those other things you should do. Im certain I missed something though as Im not having much luck, and I am also certain that its something pretty small and stupid on my part. Anyways, without further ado, here's the code:

Thats the main class that will eventually run everything. So far it creates the main frame, the panel and adds one to the other, in the process printing out if the panel is visible ( which when I run the code returns as a yes )

Thats the code for the MainFrame. Ignore the methods for key and mouse listeners, I know they work fine.

And finally, the code for the menu Panel.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I'm sorry, but what a dog's breakfast.

class MainScreen extends JFrame, but then you create a JFrame in the constructor???
class MainMenu extends JPanel, but then you create a JPanel in the constructor???
many other 'strange' lines of code.

here's a working version of your code, stripped to the problem (no listeners),
in particular look at the commented out lines and the added one

Sergey Schek

Joined: Jul 26, 2009
Posts: 5
D'oh, I knew coding so late at night wasnt a good idea.

Thanks for the prompt reply.
I agree. Here's the link:
subject: JPanel painComponent not working
It's not a secret anymore!