Dave Elwood wrote:It seems like an interesting approach Martin, except I have no idea what you are talking about.
If this is case, I'd say you should get more familiar with Swing framework.
This tutorial is perhaps somewhat large, but it served me extremely well. You seem to be designing a component of your own and taking some inspiration of how components are designed in Swing itself would be very useful (or so I believe), but you need some prior knowledge before diving into Swing's sources in the JDK.
However I cannot rip the paintComponent() out of my JPanel and stuff it into another class. That would compromise functionality
Yes, you can
, but more on this later.
Separation of concern is a good design practice. Your
paintComponent() method surely needs some data to know what has to be painted, and managing these data inside your child of the
JPanel class is not a good idea. Model is the class that would manage the data for you, and it would provide methods which would allow your component to obtain the data it manages and use them for painting. A
TableModel, which is actually an interface, is a good example of this.
There are some design
patterns describing similar concepts. A look at the Model-view-controller design pattern might give you another useful ideas.
As a side note, the
paintComponent method can delegate the painting onto another class and most Swing components do exactly this; this allows the components to be painted according to the look and feel of the system or application. I don't know whether this is necessary for you; I just wanted to demonstrate that delegating the painting out of the component itself, when done right, does not compromise functionality at all.
And are you truly speaking of a "Modal" or just misspelling "Model"?
I used the words "modal form" to refer to the "smaller frame" you've described. You've mentioned it behaves like a modal form. You want this form to change the way the master form paints itself, and I suggest to do this by manipulating the data in the model.
I don't think I could concoct an addListener() method out of thin air.
More or less, this is what I hinted you to do. You'd implement your own
addListener() methods in your model, when you created one. Maybe we could return to this subject later, when you get more familiar with Swing.
Edit: I've "refactored" this post somehow to better untangle the "modal"/"model" confusion.