Add Method to Built In Class
        Posted  
        
            by 
                Evorlor
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Evorlor
        
        
        
        Published on 2014-06-10T03:14:07Z
        Indexed on 
            2014/06/10
            3:25 UTC
        
        
        Read the original article
        Hit count: 223
        
I am pretty sure this is not doable, but I will go ahead and cross my fingers and ask.
I am trying to add a method to a built in class. I want this method to be callable by all of the built in class's subclasses. Specifically:
I have a JButton, a JTextPane, and other JComponents.  I want to be able to pass in a JDom Element instead of a Rectangle to setBounds().  My current solution is to extend each JComponent subclass with the desired methods, but that is a LOT of duplicate code.  Is there a way I can write the following method just one time, and have it callable on all JComponent objects?  Or is it required that I extend each subclass individually, and copy and paste the method below?
public void setBounds(Element element) {
    this.setBounds(Integer.parseInt(element.getAttribute(
            "x").toString()), Integer.parseInt(element
            .getAttribute("y").toString()), Integer
            .parseInt(element.getAttribute("width").toString()),
            Integer.parseInt(element.getAttribute("height")
                    .toString()));
}
        © Stack Overflow or respective owner