This is a MenuBar class which is supposed to assemble a Menu bar.. basically i have some JMenus which contains some JMenu items here... and add all the JMenus to this class which is the bar (sorry if this is confusing)

i am trying to write a public method to remove or better making one of the MenuItem "unclickable" (which built-in function should i use??) ... ok, i was on the JFrame class outside this class trying to access the public methods in this class but seems the public methods are not reachable.

last thing to do to complete my project (hopefully)... i appreciate all the inputs =)

import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;

/*
 * This is purely GUI component 
 * This is a MenuBar class with components added to it
 */
public class MenuBar extends JMenuBar 
{
	private JFrame frame;
	private JMenu menuFile, menuEdit, menuHelp;
	private JMenuItem closeMenuItem, printMenuItem, showTableMItem, addRecordMItem, searchMItem, updateMItem, deleteMItem, insMItem, adMItem;
    
	
	public MenuBar(JFrame theFrame)
	{
		/////////////////////   file //////////////////////////////
		this.frame = theFrame;
		menuFile = new JMenu("File");           // menu file
		menuFile.setForeground(java.awt.Color.black);
		
		showTableMItem = new JMenuItem("Show Table");  // showTable item 
		menuFile.add(showTableMItem);                   // menu edit with showTable item
		showTableMItem.addActionListener(new ShowTable(frame));
		
		
		closeMenuItem = new JMenuItem("Close"); // menu closeItem
		closeMenuItem.setAccelerator(KeyStroke.getKeyStroke('q', java.awt.Event.CTRL_MASK, false));
		closeMenuItem.addActionListener(new closeMenuHandler());
		
	
		//printMenuItem = new JMenuItem("Print");  // menu printItem
		//menuFile.add(printMenuItem);            // menu file with printItem
		//printMenuItem.setAccelerator(KeyStroke.getKeyStroke('p', java.awt.Event.CTRL_MASK, false));
		////////////// ActionListener /////////////
	
		menuFile.add(closeMenuItem);            // menu file with closeItem
		
		
		//////////// Menu Bar Edit items /////////////////
		menuEdit= new JMenu("Edit");          // menu edit
		menuEdit.setForeground(java.awt.Color.black);
		
		
		
		
		addRecordMItem = new JMenuItem("Add a Record");  // addRecord item
		menuEdit.add(addRecordMItem);					// edit menu with addRecord item 
		addRecordMItem.addActionListener(new AddRecord(frame));
		
		
		searchMItem = new JMenuItem("Search Record");
		menuEdit.add(searchMItem);
		searchMItem.addActionListener(new Search(frame));
		
		/*
		updateMItem = new JMenuItem("Update Record");
		menuEdit.add(updateMItem);
		updateMItem.addActionListener(new Update(frame));
		*/
		
		deleteMItem = new JMenuItem("Delete Record");
		menuEdit.add(deleteMItem);
		deleteMItem.addActionListener(new Delete(frame));
		
		adMItem = new JMenuItem("Advanced SQL Access");
		menuEdit.add(adMItem);
		adMItem.addActionListener(new Advance(frame));
		///////////////////////////////////////
		
		menuHelp = new JMenu("Help");
		menuHelp.setForeground(java.awt.Color.black);
		
		insMItem = new JMenuItem("Read me");
		insMItem.addActionListener(new Instruction());
		menuHelp.add(insMItem);
		
		//////// Add all menu things to the menu bar /////////
		//menuBar = new JMenuBar();    // menu bar
		setBackground(java.awt.Color.gray);
		add(menuFile);		// menu bar with menu file
		add(menuEdit);		// menu bar with menu edit
		add(menuHelp);
		frame.add(this);
		
	}// end Constructor
	
    [B] /// NOT reachable [/B]
	public JMenuItem getAdvance()
	{
		return adMItem;
	}
	[B] /// NOT reachable [/B]
	public void removeAdSQL()
	{
		this.remove(adMItem);
	}
	
}


class closeMenuHandler implements ActionListener
{
	public void actionPerformed(ActionEvent ae)
	{
		System.exit(0);
		
	}
}

Recommended Answers

All 4 Replies

You'll be able to access those methods if you have declared the variable to be of your "MenuBar" type. If you're using a JMenuBar reference, those won't be visible.

got it... so in whatever class i need to implement my menuBar ... is there a built-in function i could use to remove one of the MenuItem?

//// the class i need to add the menuBar 
MenuBar mb = new MenuBar()
mb.??????(mb.getAdvance());  //getAdvance() returns me the MenuItem

thanks

Well, that really depends on how you decide to provide that access. Your menu bar can disable items by using their setEnabled() method if that's what you're wanting to do. The only way to actually remove an item is to remove it from its parent menu, but if your classes are removing a lot of menu items you really need to stop and ask yourself why you're trying to share one menu bar and hack it up as needed. Perhaps each screen or state of your application may need its own menu.

Well, that really depends on how you decide to provide that access. Your menu bar can disable items by using their setEnabled() method if that's what you're wanting to do. The only way to actually remove an item is to remove it from its parent menu, but if your classes are removing a lot of menu items you really need to stop and ask yourself why you're trying to share one menu bar and hack it up as needed. Perhaps each screen or state of your application may need its own menu.

setEnable()... that is it!!!! thanks a ton

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.