• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extend Swing component AND other class

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody

I have to types of custom gui components. One extends JLabel while the other one extends JButton. I'd like these two to have exactly the same methods but Java does not allow multiple extends.

The methods are a kind of getters and setters. I'm aware that I can implement extend the JLabel and then implement an interface but I don't want to implement the same method twice.

The implementation does the trick but why bother to implement when I can copy the exact same methods anyway, right?

My problem is more of a structural one. I want to use a 'clean' solution that does not force me to 'copy' the methods in both classes.


Here is what I'd like:



I've tried some things like ClassWithTheMethods extends <T> where T would be JLabel or JButton but I figured generics only go so far for a (good) reason...

Thank you very much.

Dean
 
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can make ClassWithTheMethods as abstract class and make a concrete class which is extending the ClassWithTheMethods
 
Dean WinchesterD
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi CK, I'm not sure I am following.

How can ClassWithTheMethods extend a class that extends JButton or JLabel? Doesn't this mean that I have to create two ClassWithTheMethods-classes?

I'm aware that it is a rather complicated solution for a simple issue, maybe I'm better off the copy the methods anyway.

Your reply got me thinking though, thanks for that.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

As you know, Java™ only supports single inheritance, not multiple inheritance. In the cases you mention, many of the methods are common to buttons and components. Have a look at the documentation for a button and a label; you will find their inheritance hierarchy at the opt of the page. Look where those inheritance trees diverge, and you can work out which methods they have in common and which are disjoint. Choose whichever has the more disjoint methods (I suspect button) and make you class inherit from that. Then, you can say myNewButton instanceof JButton, but you can't get anything sensible from myNewButton instanceof JLabel.

I would reconsider my design if I found myself facing this sort of problem.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had the same problem, and it didn't take me long to find out that the common superclass of JLabel and JButton is JComponent. Which implies that the setText() method of JLabel and the setText() method of JButton don't have a common ancestor. That's just convergent evolution, if you like.

My solution was the traditional Java solution; if you want two classes to "have the same methods" and to have that mean something, you create an interface with those methods and have each of the two classes implement that interface.

And the other thing that happened, once I decided to use the interface, was that I realized I didn't need to extend JLabel or JButton either. I realized that composition could work for me just as well as inheritance.
 
Destroy anything that stands in your way. Except this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic