I currently have a rather large class that extends JPanel. The class is about 2000 lines of code although it is broken up into many mostly small functions. I want to add a second mode which will render something totally different in the panel. For organization, I am thinking of simply passing the panel to another class and have the other class render the panel. Or I could just add onto the first class. What would be the best approach? The only ugliness with the second approach is that the main class would have to check what mode it is in and if it is in the second mode, it would have to pass mouse and key movements to the secondary class.
Asked
Active
Viewed 30 times
0
-
3Consider using the [mvc pattern](https://stackoverflow.com/questions/8693815/java-learning-mvc). Have a `Model` class that holds all the information that the view (gui) needs. Have a `View` class that uses the model to displays gui. Have a `Controller` class that controls the model and view. There are many [examples](https://stackoverflow.com/a/49945155/3992939) around. – c0der Aug 19 '20 at 04:19
-
Thanks although the model is totally different for each case and the view is also totally different. So I would basically need 2 models, 2 views and 1 controller. Imagine in one mode, I want to display some instrumentation / gauges and in the other mode, I want to display a game of pong -- so totally different applications, but I want them to display in the same panel at different times. I thought about just using 2 JPanels and only have 1 added to the JFrame at a time. When I want to change modes, unregistered one panel, register the other. I'll give it some more thought. – PentiumPro200 Aug 19 '20 at 17:19
-
1Consider adding both JPanels to the JFrame using [CardLayout](https://stackoverflow.com/a/46013230/3992939) and have the controller switch them. – c0der Aug 20 '20 at 03:45
-
Thanks @c0der, I'll look into that tomorrow. I have not heard of CardLayout. – PentiumPro200 Aug 22 '20 at 07:38
1 Answers
0
If it's something completely different, keep them in two different classes so if anyone else were to look at your code, they'll understand better of what's going on.

charri
- 984
- 7
- 9