CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2011
    Posts
    15

    Talking Inserting pre programmed UI elements

    Hi everyone, its my first time in this forum!
    i am kind of new to java programming and iam about to start a project, a java web start application
    for now, i need to know how can I develop a bar menu, a status bar, or another set of elements that are going to be common for many jframes
    the thing is that I need to programm them one time, and insert in all the frames that are required, dont know if I explain myself

    excuse my mistakes, i am from venezuela and my english is not that good

    Thanks in advance and I will be here everyday from now!

    Something missing: I am working with netbeans ide 7.0.1

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Inserting pre programmed UI elements

    You can't share the same instance of GUI components with different panels/frames so you have create a new instance for each panel/frame you want to add them to. To do this you can have a factory to create the components you want multiple instances of. You then call the factory's methods each time you want a new component with your predetermined values (or optionally pass in values to configure the object). For example in it's simplest form a method might be something like:

    Code:
    public static JMenuBar createMyMenuBar()
    	{
    	JMenuBar myMenuBar = new JMenuBar();
    	
    	JMenu myMenu1 = new JMenu("My Menu 1");
    	myMenuBar.add(myMenu1);
    
                  ...
    	
    	return myMenuBar;
    	}
    And you would use it like this:

    Code:
    JFrame myFrame = new JFrame();
    mtFrame.setJMenuBar(MyGuiFactoryClass.createMyMenuBar());
    For help on creating/using menus read the Java tutorial
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Inserting pre programmed UI elements

    The other questions:
    how to put names on the menu items
    how do you add listeners to respond to the menu events?
    Perhaps the create method should take an array of names for the items and a listener reference.
    Norm

  4. #4
    Join Date
    Sep 2011
    Posts
    15

    Re: Inserting pre programmed UI elements

    wow thats beyond my narrow knowledge, i will try something and come back to the forum! thanks for helping

  5. #5
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Inserting pre programmed UI elements

    In pseudo code:

    Code:
    public static JMenuBar createMyMenuBar(String[] names, Listener lstnr)
    	{
    	JMenuBar myMenuBar = new JMenuBar();
    	for(String aName : names) {
    	  JMenu myMenu1 = new JMenu(aName);
              myMenu1.addListener(lstnr);
    	  myMenuBar.add(myMenu1);
            }  
                  ...
    	
    	return myMenuBar;
    }
    Norm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured