Kieran
Kieran

Reputation: 13

Including a JPanel in multiple JFrames

I have had a reasonable amount of experience in Java SWING development and trying to simplify updates to the program. I have a basic menu (group of JButtons) which I would like at the top of every window (JFrame) displayed, instead of trying to maintain these buttons on each individual Frame I was wondering if it was possible to create one JPanel and "import" it into multiple JFrames, similar to how you can "include" in php (Bad example but best I can think of)

Added after suggestion from first response

I currently have a JPanel on my frame titled topPanel and for the initiation of this I have: topMenuPane = new menuPanel(); where menuPanel is a class extending JPanel however the buttons and values in the menuPanel class are not displaying in the JFrame.

Upvotes: 1

Views: 169

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347204

You could...

Extend JFrame and create a base frame all you applications must use.

For - Easy to maintain; centralized implementation; other developers don't need to remember to do the "setup"

Against - Tightly coupled to an implementation, may not be easy to extend for other developers (that might need to use the API).


Use a "WindowFactory" that generated the JFrame that all you applications use to generate there base frames.

For - Easy to maintain; centralized implementation; other developers don't need to remember to do the "setup"

Against - There is still the possibility that other developers may "choose" to not use it

Upvotes: 2

Code-Apprentice
Code-Apprentice

Reputation: 83527

I would create a subclass of JPanel which creates the JButtons. You can then create instances of this subclass to add to any JFrame (or any other container) you wish.

Upvotes: 0

Related Questions