开发者

"MVC" and controlling a jMenubar depending on selected jTabbedpane from a "Control" class

开发者 https://www.devze.com 2023-01-07 04:07 出处:网络
Right so I know my code/structure is pretty messy, I\'ve not done MVC before and I\'m pretty sure I\'ve done it wrong anyway. I want to be able to \"control\" the jMenubar depending on which tab is se

Right so I know my code/structure is pretty messy, I've not done MVC before and I'm pretty sure I've done it wrong anyway. I want to be able to "control" the jMenubar depending on which tab is selected.

I have a main GUI class which simply creates a new "MenuBar" and each new "PanelXXXX", where "PanelXXXX" could be "PanelDesign", "PanelSource" etc. These Panels are added as new tabs into my jTabbedpane. "PanelXXXX" all extend "Panel" which contains some extra/default values.开发者_如何学C "Panel" extends "jPanel" and implements "ActionListener". "MenuBar" extends "jMenubar". Inside "MenuBar" - I setup the different "JMenuItem"s.

This all works fine. However in my "Container" class (which implements "ActionListener"), I check the current selected tab and decide whether my forward/back buttons wrap around or not etc. - works fine too.

Now I'm stuck wondering how I will control the MenuBar without creating a new object, I could re-code the MenuBar to imeplement a Singleton class, that might work?

Hmm, I think I need "MenuBar" to implement "ActionListener"? Wait, no that wouldn't be correct...


First of all do not subclass Swing components unless you need new component with extended functionality.

Now, the way to deal with the menu:

  1. Create empty JMenuBar.

  2. Write a method to clear menu bar and fill it with menus/actions based on selected tab

  3. Add change listener to your tabbed pane such that it calls previously described method

0

精彩评论

暂无评论...
验证码 换一张
取 消