aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Why Java forces me to initialize my JPanel? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Why Java forces me to initialize my JPanel? " Watch "Why Java forces me to initialize my JPanel? " New topic
Author

Why Java forces me to initialize my JPanel?

Morgan Morten
Greenhorn

Joined: Feb 19, 2005
Posts: 9
Hello All,
Here is a question I have being trying to answer for the longest (forgive me if this is a basic question, but I taught myself Java and I could never answer this question myself):

I have a Frame and a Panel. My Panel shows a button from my Frame. My Frame shows my Panel.

I am able to pass my Frame's button to my Panel by the following statement:


But I can't pass my Panel to my Frame the same way:


I can only pass it if I initialize it in my Frame first:


So, my question is this: Why I can pass my button to my Panel, but I can't pass my Panel to my Frame (the same way) without making an instance of my Panel first in my Frame? (even when I used the key word Static)

Here are the 2 files:

----- MyFrame.java ---



----- MyPanel.java-----



Is there another way of passing this Panel to my Frame (without initializing it)?
I know I am asking a dumb question, but I least I will take this issue to rest.
~Thank 'yall ! Morgan






Morten
Claude Moore
Ranch Hand

Joined: Jun 24, 2005
Posts: 151
Hi Morgan,

from your code it's clear that while Frame_Button is static within MyFrame class, while Panel isn't static in MyPanel; so if you want to reference Panel of MyPanel in MyFrame, you need to instantiate MyPanel before.

Morgan Morten
Greenhorn

Joined: Feb 19, 2005
Posts: 9
Claude,
I added the word "static" to my Panel (in MyPanel.java) and STILL the Panel will not show in the Frame ..


static JPanel Panel = new JPanel(); // --- Added the word 'Static' in MyPanel.java ---



That's my point, I can't show Panel without making an instance of it in the Frame
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

Morgan Morten wrote:That's my point, I can't show Panel without making an instance of it in the Frame


Well, of course you can't. The instance is what you would be displaying. Variables only exist so you can keep references to the instances which are what Java actually uses.

And when you do create a JPanel instance, there's nothing in it so you don't see anything when you display it. Can I suggest you start at the beginning of the Swing tutorial? And try to make your code look like the code you get from that tutorial -- what you've written so far is almost unreadable because of its failure to follow the Java coding standards.
Claude Moore
Ranch Hand

Joined: Jun 24, 2005
Posts: 151
Morgan Morten wrote:
I added the word "static" to my Panel (in MyPanel.java) and STILL the Panel will not show in the Frame ..


Oh well, that's another problem... I did not understood what did you mean....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32713
    
    4
You should never use static in the hope it will work/compile, etc. That is a mistake in 99.9% of instances. Things static belong to the class, not to the object, so that is a completely different sort of design.
Morgan Morten
Greenhorn

Joined: Feb 19, 2005
Posts: 9
I got it. I will used instanciated classes to refer to the class objects. THANK YOU for all your help!
~Morgqn
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32713
    
    4
You’re welcome
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Why Java forces me to initialize my JPanel?
 
Similar Threads
Swing - Calling repaint in actionPerformed method
GUI Code Placement
Doubt in GUI
Adding JPanel to JFrame
Problem with Adding JScrollPane to JPanel!