Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Java forces me to initialize my JPanel?  RSS feed

 
Morgan Morten
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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





 
Claude Moore
Ranch Hand
Posts: 862
8
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 862
8
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53773
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. I will used instanciated classes to refer to the class objects. THANK YOU for all your help!
~Morgqn
 
Campbell Ritchie
Sheriff
Posts: 53773
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!