Creating a menu using packages

I am creating a GUI package and just made a CreateFrame & CreateMenuBar class.

The CreateFrame class sets up a 800x600 Frame.

In that frame I want to create a menu with the class CreateMenuBar.

How can I display the menu in that frame, in other words how do I point to the frame I created with CreateFrame ?

[351 byte] By [appo_neo] at [2007-9-26 1:47:20]
# 1
I assume that you have extended JFrame for your custom frame and also extended JMenubar for your menubar. In that case you can add your custom menubar to your custom frame just as you add JMenubar to a JFrame using the same methods unless you have overridden them.
malaydas at 2007-6-29 2:46:16 > top of Java-index,Archived Forums,Swing...
# 2

public static void main(String[] args) {

/**

* Setup the Graphical User Interface (GUI)

*

* @param appoTitle: title of the frame

* @param appoWidth: width of the frame

* @param appoHeight: height of the frame

* @param appoLaF: look and feel of the frame

*/

CreateFrame frame = new CreateFrame(appoTitle, appoWidth, appoHeight, appoLaF);

CreateMenuBar menu = new CreateMenuBar(appoWidth, 30);

frame.setJMenuBar(menu);

CreateFrame creates a frame -> this works ok !

CreateMenuBar should create the menubar, compiled ok !

I gey an error at the frame.setJMenuBar(menu); part.

What could I be doing wrong ?

appo_neo at 2007-6-29 2:46:16 > top of Java-index,Archived Forums,Swing...
# 3
are u sure you have extended JMenubar for your createMenubar class? please check if you have extended Menubar instead of JMenubar in which case it will not compile.
malaydas at 2007-6-29 2:46:16 > top of Java-index,Archived Forums,Swing...
# 4

I would like to use just these following lines in my main class to create the frame with the menu :

CreateFrame frame = new CreateFrame(appoTitle, appoWidth, appoHeight, appoLaF);

CreateMenuBar menu = new CreateMenuBar(appoWidth, 30);

here is the code for the CreateMenuBar :

package appo.gui;

/** 2001 3me me ia

*

* @author A. Postma aka aPPo neo

* -

* CreateMenuBar

* -

* This class will create & show a default new frame

*

* version 0.01 24.07.2001 - Create a basic menu bar

*/

// import Swing packages

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.JMenu;

import javax.swing.JMenuItem;

import javax.swing.JCheckBoxMenuItem;

import javax.swing.JRadioButtonMenuItem;

import javax.swing.ButtonGroup;

import javax.swing.JMenuBar;

import javax.swing.KeyStroke;

import javax.swing.ImageIcon;

import javax.swing.border.Border;

import javax.swing.JTextArea;

import javax.swing.JScrollPane;

import javax.swing.JFrame;

// import Abstract Window Toolkit package

// Create and show the GUI

public class CreateMenuBar extends JMenuBar {

// Border references(top, left, bottom, right)

static Border BorderUp= BorderFactory.createEmptyBorder(4,5,2,5);

static Border BorderDown= BorderFactory.createEmptyBorder(2,5,4,5);

static Border blackLine= BorderFactory.createLineBorder(Color.black);

static Border etchedLine= BorderFactory.createEtchedBorder();

static Border raisedBevel = BorderFactory.createRaisedBevelBorder();

static Border loweredBevel = BorderFactory.createLoweredBevelBorder();

static Border menuBorder= BorderFactory.createCompoundBorder

(BorderUp, etchedLine);

static Border panelBorder = BorderFactory.createCompoundBorder

(BorderDown, loweredBevel);

static JMenuBar menuBar = new JMenuBar();

/**

* Create a menu bar

*

* @param menuWidth: the width of the menu

* @param menuHeight: the height of the menu

*/

public CreateMenuBar(int menuWidth, int menuHeight) {

menuBar.setOpaque(true);

menuBar.setBorder(menuBorder);

menuBar.setPreferredSize(new Dimension(menuWidth, 30));

setJMenuBar(menuBar);

}

}

How do I have to change this or add code to it to get it working ? The problem is the setJMenuBar. I guess I have to refer to the existing frame, but how do I do that ? If I understand how this works I can rapidly start coding the rest of the package ... I just have to understand the basics of referring to the frame created with CreateFrame.

thanks again (in advance)

appo_neo at 2007-6-29 2:46:16 > top of Java-index,Archived Forums,Swing...
# 5

it is strange that you are calling setJMenubar method in menubar class itself(createMenubar in your case). it should be called in your frame class that is createFrame. it is the frame which is setting the menubar .the menubar is not setting itself on itself. so wherever u are creating the createFrame instance, call setJMenubar method on that instance. it should work after that.

malaydas at 2007-6-29 2:46:16 > top of Java-index,Archived Forums,Swing...
# 6
Thanks ... that did the job ...
appo_neo at 2007-6-29 2:46:16 > top of Java-index,Archived Forums,Swing...