0

So I have an object that holds a nonspecific JPanel: private static JPanel setPanel;

I have lots of JPanel subclasses for different kinds of screens I'm swapping out on my JFrame, which behave differently. Now, each of these subclasses implements a certain method getKeyPress(), and I have made an interface that includes this method, which every one of these JPanel subclasses also implements. My question is whether I can refer to that method through my generic setPanel object, as in: setPanel.getKeyPress() in some convenient manner, or do I have to create a subclass of JPanel with that one method in it and then have each subclass be a subclass of that one?

Matt
  • 14,906
  • 27
  • 99
  • 149
David Lalo
  • 155
  • 7
  • 1
    `private static JPanel setPanel;` Why is it declared `static`? I've never seen a valid use of `static` for a GUI component. – Andrew Thompson Jun 21 '16 at 10:30
  • `setPanel` would also usually be the name of a method... – T.J. Crowder Jun 21 '16 at 10:42
  • @Andrew Thompson- It is being held in the class with my main method (which is the class that initializes and maintains my JFrame), the whole point being that I can call different methods to switch out the panel that the JFrame holds. – David Lalo Jun 22 '16 at 12:26
  • The whole point is that it's not the best solution, and will likely cause problems. – Andrew Thompson Jun 22 '16 at 12:27
  • @T.J.Crowder- Good point. That could be confusing to other people and while it's a personal project, it's always good to practice programming things that other people can understand. I'll change it. – David Lalo Jun 22 '16 at 12:27
  • @Andrew Thompson- I'm not very experienced and just sort of figuring things out. I'm not following any particular methodology, but treating the project as more of an exercise in problem solving. Is there a better way of switching which JPanel a frame holds? – David Lalo Jun 22 '16 at 12:29
  • *"Is there a better way of switching which JPanel a frame holds?"* Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). I don't see how that directly relates to the use of `static` though. – Andrew Thompson Jun 22 '16 at 12:37
  • Thanks for the link. To be clearer, my JFrame is a private object that is created in the class that runs my `main` method. In order to use this object in my main, it must too be static. However, since my JFrame is static, any related methods that operate on it are too. So, for example, I have a `switchPanel(JPanel)` method that changes which panel is currently set as the one on the JFrame. Since it operates on the static JFrame, it and its components are also static. Also, it makes sense to me that a method like `switchPanel` is static, since I use it from different places. – David Lalo Jun 22 '16 at 13:22

1 Answers1

1

...or do I have to create a subclass of JPanel with that one method in it and then have each subclass be a subclass of that one?

That's the way to go, your own MyJPanel (or similar) class (either abstract or not). Then, you use MyJPanel throughout your code. That gives you a compile-time check that you're dealing with one of your subclasses, and gives you a convenient place to add further methods if you need to.

You could do

((TheInterface)setPanel).getKeyPress();

...but it's clumsy, and you lose compile-time code checks (and all the usual arguments against casting apply).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    Another, less elegant approach could be override one JPanel's methods , in all those JPanel subclasses. – c0der Jun 21 '16 at 10:41