Search code examples
javaclassjythonextend

How to extend a java class in Jython?


I am trying to extend Plot.java in a Jython class and use "setSize() and setButtons()" methods in Plot.java in Histogram.py subclass. However, I cannot do so I get the error that global variable setSize() is not defined. Can someone tell me what the problem is?

class Histogram(Plot):
    dataset = 0;
    def __init__(self):
        theJFrame = JFrame();
        theJFrame.setSize(400, 350);            #outer box
        setSize(self,350, 300);                 #graph window
        setButtons(self,true);                  #buttons to print, edit, etc.
        setMarksStyle(self,"none");             #do not show marks at points

Solution

  • Dave Newton is right, but also note that Jython exposes Java setters and getters as properties, so self.size = (350, 300) works as well (and is a little prettier to the eyes of a Python developer). To save a little typing, you can even call the setters from the JFrame constructor itself like so:

    theJFrame = JFrame(
        size = (400, 350)            #outer box
    )
    

    See: http://www.jython.org/jythonbook/en/1.0/GUIApplications.html for more detail.