File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Swing Components Declared in an Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Swing Components Declared in an Interface" Watch "Swing Components Declared in an Interface" New topic

Swing Components Declared in an Interface

Cory Hartford
Ranch Hand

Joined: May 16, 2011
Posts: 85

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.

‎"The greatest of all weaknesses is the fear of appearing weak." - JB Bossuet
Darryl Burke

Joined: May 03, 2008
Posts: 5115

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).

luck, db
There are no new questions, but there may be new answers.
Cory Hartford
Ranch Hand

Joined: May 16, 2011
Posts: 85

Will do. Thanks Darryl.
I agree. Here's the link:
subject: Swing Components Declared in an Interface
It's not a secret anymore!