I am struggling with a bit of inherited code that needs sliders added. I have not gotten that to work. I thought the best thing to do was make an example that had the basic layout of the problem application (with sliders) and then try to plug all the inherited bits into it. Here is what I wrote. It works. In the 'real' code I have to use an 'ApplicationFrame' pulled from a jar, not a JFrame, and set a value in an object created from another non-standard class from the same jar. But before I worry about those details, are my inner classes in the right place? I wasn't sure if they should be inside the main ScrollTestTrouble class or in the MainPanel class. Same issue regarding xLabel and yLabel. Should they be defined in the MainPanel class or are they fine where they are? Either way works, I assume one is better. I want to get this bit right first before I worry about things like resetting the position of the thumbs when the image in the ApplicationFrame is redrawn (you can select a piece of the image the scrollbars are operating on in the actual application).
Always keep the classes and methods separate. Normally, you would put the inner classes first. You keep the fields within the class where they belong. Try to avoid having too many inner classes. Also try to avoid classes that are basically copies. E.g. you could rewrite the X/YChangeListener to ChangeListener and a constructor with a label argument. That class could be adorned with the "static" keyword to show that it does not have any other side effects. As the use is exclusive to the MainPanel class, the class should probably be defined in there.
As for the fields, keep the fields as close to the class that uses them as possible. If you've already got a JPanel class, and the instance of this class uses the xLabel field exclusively, then this class should have the field, not the surrounding class (as the same way the X/YChangeListener class definitions should be local to the MainPanel class). Try and rewrite it in such a way that the MainPanel class is not defined within the ScrollTestTrouble class, but is a top-level class in its own file. In that case the class should have package visibility, which is accomplished by removing the private keyword.
Joined: Oct 10, 2011
Thanks, I will move the variables and Listeners into MainPanel. My plan is to have a class that handles drawing a graph and have that in one file. Then another class that places the graph in a frame of a larger GUI. I only have the 'ScrollTestTrouble' class because I was following an example where the chartPanel was put in an applicationFrame (something defined by the chart class) and I'm not sure if I have to have that around the chart. I just want normal JFrames and JPanels in the routine that uses what this class constructs. So I am creating the applicationFrame here.
subject: Have I got my inner classes and variable declarations in the right places?