Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Viewing menu items in menubars

Posted on 2003-11-26
8
286 Views
Last Modified: 2010-03-31
I have created a methods to create a menubar.  When I put it on the frame I can see the menu titiles, but when I click on the menus it does not drop down to display the menu items for the menus.  I sort of followed a tutorial that I saw on the sun website but it has not helped me.  Any help would be nice. Also I am trying to upload xml files in my jframe so if anyone can give me some directions on what would be the best way to go about it that would help as well.  Thanks in advance.  Here is my code:

public JMenuBar xmlMenu() {
            
            JMenuBar menuBar;
            JMenu file, operation;
            JMenuItem open, save, exit, query;
            
            menuBar = new JMenuBar();
            
            file = new JMenu("File");
            file.setMnemonic(KeyEvent.VK_A);
            file.getAccessibleContext().setAccessibleDescription(
                "The only menu in this program that has menu items");
            menuBar.add(file);
            
            open = new JMenuItem("open");
            open.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_1, ActionEvent.ALT_MASK));
        open.getAccessibleContext().setAccessibleDescription(
                "This doesn't really do anything");
            open.addActionListener(this);
            file.add(open);
            
            save = new JMenuItem("save");
            save.addActionListener(this);
            file.add(save);
            exit = new JMenuItem("exit");
            exit.addActionListener(this);
            file.add(exit);
            
            operation = new JMenu("Operations");
            menuBar.add(operation);
            
            query = new JMenuItem("Query");
            operation.add(query);
            
            return menuBar;
            
      }//end JMenuBar
0
Comment
Question by:janiab
  • 3
  • 2
  • 2
8 Comments
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9829018
try this and see if there any different? ;)

public JMenuBar xmlMenu() {
         
          JMenuBar menuBar;
          JMenu file, operation;
          JMenuItem open, save, exit, query;
         
          menuBar = new JMenuBar();
         
          file = new JMenu("File");
          file.setMnemonic(KeyEvent.VK_A);
          file.getAccessibleContext().setAccessibleDescription(
                "The only menu in this program that has menu items");
         
         
          open = new JMenuItem("open");
          open.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_1, ActionEvent.ALT_MASK));
          open.getAccessibleContext().setAccessibleDescription(
                "This doesn't really do anything");
          open.addActionListener(this);
          file.add(open);
         
          save = new JMenuItem("save");
          save.addActionListener(this);
          file.add(save);

          exit = new JMenuItem("exit");
          exit.addActionListener(this);
          file.add(exit);
         
          operation = new JMenu("Operations");
         
         
          query = new JMenuItem("Query");
          operation.add(query);

         menuBar.add(file);
          menuBar.add(operation);
         
          return menuBar;
         
     }//end JMenuBar
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9829039
How do you add the menu bar?

frame.setJMenuBar(xmlMenu());

?
0
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9830248
>>How do you add the menu bar?

janiab, post your code here.

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:janiab
ID: 9839547
I added the menu bar with this line of code
setJMenuBar(xmlMenu());

this is my entire code that I used.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.table.*;
import java.util.*;
import java.text.*;

/**
 * CS4440:  
 *
 * <PRE>
 * Enter a brief description here
 *
 * Revisions:  1.0  Feb 21, 2003
 *                  Created class XMLClient
 * </PRE>
 *
 * @author <A HREF="mailto:gte284k@prism.gatech.edu">Janet Nicole Abdul-Karim</A>
 * @author <A HREF="mailto:gte">Zaheer Hooda</A>
 * @author <A HREF="mailto:gte">James Voerg</A>
 * @author <A HREF="mailto:gte"></A>
 * @version Version 1.0, November, 2003
 */


class XMLClient extends JFrame implements MouseListener, ActionListener {
      
      
      Panel mainPanel;
      Container frameContainer;
      JFileChooser xmlFile;
      JTextArea output;
      JScrollPane outputPane;
      
      /**
       * Constructor
       */
      public XMLClient() {
            
            super("XMLClient");
            
            /**
             * Text Area
             */
            output = new JTextArea(5, 30);
        output.setEditable(false);
        outputPane = new JScrollPane(output);
      
            /**
             * Panel setup
             */                  
            mainPanel = new Panel(new BorderLayout());
            //mainPanel.setOpaque(true);
            mainPanel.add(outputPane, BorderLayout.CENTER);
            /**
             * Container setup
             */
            frameContainer = getContentPane();
            frameContainer.add(mainPanel, BorderLayout.CENTER);            
            this.setContentPane(frameContainer);
            
            xmlFile = new JFileChooser();
            /**
             * closes the frame
             */
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            /**
             *add JMenuBar
             */
             setJMenuBar(xmlMenu());
             
            
      }// end constructor
      
      public JMenuBar xmlMenu() {
            
            JMenuBar menuBar;
            JMenu file, operation;
            JMenuItem open, save, exit, query;
            
            menuBar = new JMenuBar();
            
            file = new JMenu("File");
            file.setMnemonic(KeyEvent.VK_A);
            file.getAccessibleContext().setAccessibleDescription(
                "The only menu in this program that has menu items");
            menuBar.add(file);
            
            open = new JMenuItem("open");
            open.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_1, ActionEvent.ALT_MASK));
        open.getAccessibleContext().setAccessibleDescription(
                "This doesn't really do anything");
            open.addActionListener(this);
            file.add(open);
            
            save = new JMenuItem("save");
            save.addActionListener(this);
            file.add(save);
            exit = new JMenuItem("exit");
            exit.addActionListener(this);
            file.add(exit);
            
            operation = new JMenu("Operations");
            menuBar.add(operation);
            
            query = new JMenuItem("Query");
            operation.add(query);
            
            return menuBar;
            
      }//end JMenuBar
      
      /**
       * frame display
       */
      
       public void createAndShowGUI() {
             
             setDefaultLookAndFeelDecorated(true);
             setSize(450, 260);
        setVisible(true);
             
       }//end createAndShowGUI
      
      
      public void actionPerformed(ActionEvent e) {
            
      }//end actionPerformed
      
      /**
       *MouseListener Interface
       */
      
    public void mouseClicked(MouseEvent e){

    }
   
    public void mousePressed(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
   
    public static void main (String argv[]) {
          
          XMLClient test = new XMLClient();
          test.createAndShowGUI();
          
          }//end main
      
 
}//end class
 
0
 

Accepted Solution

by:
janiab earned 0 total points
ID: 9844050
I seem to have found out my problem.  In my code I was mixing awt components with swing components.  I was using Panel in stead of JPanel.  Once I changes Panel to JPanel that resolved my problem.  Thanks for your help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9845544
No problem - should have spotted that myself!
0
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9847949
; )
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 43
null output 3 35
jsp insert to database example 2 46
Fast way to search item into Java Array (Rhino compatible) 2 31
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question