Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing Components Declared in an Interface

 
Cory Hartford
Ranch Hand
Posts: 85
Eclipse IDE Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys; I have trolled this forum a bunch. Love it. First time posting though.
I have an interface that I am using for some Frames that I am using. I declare the components (Jbuttons, Jlabels etc) in the interface. Works great for the first Class that implements the interface; however the 2nd class that implements the interface retains the values of the first class assigned to the components. (And vice versa depending on which class is called first). I am coming to understand that variables and components declared in an interface are really one entity and not a fresh copy each time you implement the interface.
Would it be better then to just have a container class that stores the components and instantiate that for the components? And then keep the interface for my abstract methods.

Just want to make sure I am thinking about this right and implementing the correct solution.

Thanks again.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Java Language Specification
Every field declaration in the body of an interface is implicitly public, static, and final.


If you have a lot of GUI classes that share some commonality, you could design that common portion as a separate class and incorporate an instance of that class in each separate GUI (favor composition over inheritance).
 
Cory Hartford
Ranch Hand
Posts: 85
Eclipse IDE Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will do. Thanks Darryl.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic