Add Method to Built In Class
- by Evorlor
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()));
}