janiab
asked on
Viewing menu items in menubars
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( ).setAcces sibleDescr iption(
"The only menu in this program that has menu items");
menuBar.add(file);
open = new JMenuItem("open");
open.setAccelerator(KeyStr oke.getKey Stroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
open.getAccessibleContext( ).setAcces sibleDescr iption(
"This doesn't really do anything");
open.addActionListener(thi s);
file.add(open);
save = new JMenuItem("save");
save.addActionListener(thi s);
file.add(save);
exit = new JMenuItem("exit");
exit.addActionListener(thi s);
file.add(exit);
operation = new JMenu("Operations");
menuBar.add(operation);
query = new JMenuItem("Query");
operation.add(query);
return menuBar;
}//end JMenuBar
public JMenuBar xmlMenu() {
JMenuBar menuBar;
JMenu file, operation;
JMenuItem open, save, exit, query;
menuBar = new JMenuBar();
file = new JMenu("File");
file.setMnemonic(KeyEvent.
file.getAccessibleContext(
"The only menu in this program that has menu items");
menuBar.add(file);
open = new JMenuItem("open");
open.setAccelerator(KeyStr
KeyEvent.VK_1, ActionEvent.ALT_MASK));
open.getAccessibleContext(
"This doesn't really do anything");
open.addActionListener(thi
file.add(open);
save = new JMenuItem("save");
save.addActionListener(thi
file.add(save);
exit = new JMenuItem("exit");
exit.addActionListener(thi
file.add(exit);
operation = new JMenu("Operations");
menuBar.add(operation);
query = new JMenuItem("Query");
operation.add(query);
return menuBar;
}//end JMenuBar
How do you add the menu bar?
frame.setJMenuBar(xmlMenu( ));
?
frame.setJMenuBar(xmlMenu(
?
>>How do you add the menu bar?
janiab, post your code here.
janiab, post your code here.
ASKER
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.ed u">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(mainPan el, BorderLayout.CENTER);
this.setContentPane(frameC ontainer);
xmlFile = new JFileChooser();
/**
* closes the frame
*/
this.setDefaultCloseOperat ion(JFrame .EXIT_ON_C LOSE);
/**
*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( ).setAcces sibleDescr iption(
"The only menu in this program that has menu items");
menuBar.add(file);
open = new JMenuItem("open");
open.setAccelerator(KeyStr oke.getKey Stroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
open.getAccessibleContext( ).setAcces sibleDescr iption(
"This doesn't really do anything");
open.addActionListener(thi s);
file.add(open);
save = new JMenuItem("save");
save.addActionListener(thi s);
file.add(save);
exit = new JMenuItem("exit");
exit.addActionListener(thi s);
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() {
setDefaultLookAndFeelDecor ated(true) ;
setSize(450, 260);
setVisible(true);
}//end createAndShowGUI
public void actionPerformed(ActionEven t 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
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
* @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(mainPan
this.setContentPane(frameC
xmlFile = new JFileChooser();
/**
* closes the frame
*/
this.setDefaultCloseOperat
/**
*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.
file.getAccessibleContext(
"The only menu in this program that has menu items");
menuBar.add(file);
open = new JMenuItem("open");
open.setAccelerator(KeyStr
KeyEvent.VK_1, ActionEvent.ALT_MASK));
open.getAccessibleContext(
"This doesn't really do anything");
open.addActionListener(thi
file.add(open);
save = new JMenuItem("save");
save.addActionListener(thi
file.add(save);
exit = new JMenuItem("exit");
exit.addActionListener(thi
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() {
setDefaultLookAndFeelDecor
setSize(450, 260);
setVisible(true);
}//end createAndShowGUI
public void actionPerformed(ActionEven
}//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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No problem - should have spotted that myself!
; )
public JMenuBar xmlMenu() {
JMenuBar menuBar;
JMenu file, operation;
JMenuItem open, save, exit, query;
menuBar = new JMenuBar();
file = new JMenu("File");
file.setMnemonic(KeyEvent.
file.getAccessibleContext(
"The only menu in this program that has menu items");
open = new JMenuItem("open");
open.setAccelerator(KeyStr
KeyEvent.VK_1, ActionEvent.ALT_MASK));
open.getAccessibleContext(
"This doesn't really do anything");
open.addActionListener(thi
file.add(open);
save = new JMenuItem("save");
save.addActionListener(thi
file.add(save);
exit = new JMenuItem("exit");
exit.addActionListener(thi
file.add(exit);
operation = new JMenu("Operations");
query = new JMenuItem("Query");
operation.add(query);
menuBar.add(file);
menuBar.add(operation);
return menuBar;
}//end JMenuBar