tags:

views:

37

answers:

1

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. "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...

A: 

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

eugener
Any reason for not sub-classing Swing components?I had to set text/size/add action listener for each JButton, which just meant I had duplicated code everywhere, hence why I've made a simple Button class that extends JButton, now I just create a new Button("Title"); and it'll have the default size (most buttons will use this size) with it's title and an added action listener.Is this how I should be doing it? or could I improve it?
Dan2k3k4
What you described can be easily done with the Factory class. A good example inSwing will be BorderFactory
eugener