So I tried to look at the Java docs for help for this but got confused pretty quickly. I am trying to add a submenu to the menu item 'Edit' which will have submenus Copy and Paste, and I'm note sure how to do it. I have a submenu variable created, do I use that? Please help, thank you. Code is below.
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
public class MyJFrame extends JFrame
{
JMenuBar menubar;
JMenu menu, submenu;
JMenuItem mi;
public MyJFrame(String title)
{
super(title);
menubar = new JMenuBar();
setJMenuBar(menubar);
buildMenu();
}
void buildMenu()
{
menu = new JMenu("File");
mi = new JMenuItem("New");
menu.add(mi);
menubar.add(menu);
mi = new JMenuItem("List Files");
menu.add(mi);
menubar.add(menu);
menu.addSeparator();
mi = new JMenuItem("Save As");
menu.add(mi);
menubar.add(menu);
menu.addSeparator();
mi = new JMenuItem("Close");
menu.add(mi);
menubar.add(menu);
menu = new JMenu("Tools");
mi = new JMenuItem("Sort");
menu.add(mi);
menubar.add(menu);
mi = new JMenuItem("Search");
menu.add(mi);
menubar.add(menu);
mi = new JMenuItem("Edit");
menu.add(mi);
menubar.add(menu);
mi = new JMenuItem("Copy");
menu.add(mi);
menubar.add(menu);
mi = new JMenuItem("Paste");
menu.add(mi);
menubar.add(menu);
}
}
create a Menu instead of a MenuItem for Edit, and add MenuItems Copy and Paste. Then Add edit menu to Tools Menu .
menu = new JMenu("Tools");
mi = new JMenuItem("Sort");
menu.add(mi);
mi = new JMenuItem("Search");
menu.add(mi);
JMenu med = new JMenu("Edit");
mi = new JMenuItem("Copy");
med.add(mi);
mi = new JMenuItem("Paste");
med.add(mi);
menu.add(med);
menubar.add(menu);
Note: I have edited the last part of your code. Just replace the code from tools menu to this and try this. If it gets error, tell me.
Here is a simple example of how you can do it:
JMenuBar menuBar = new JMenuBar();
// Edit Menu
JMenu editMenu = new JMenu("Edit");
menuBar.add(editMenu);
// Edit -> Copy
JMenuItem copyMenuItem = new JMenuItem("Copy");
editMenu.add(copyMenuItem);
// Edit -> Paste
JMenuItem pasteMenuItem = new JMenuItem("Paste");
editMenu.add(pasteMenuItem);
Simplified excerpt from https://docs.oracle.com/javase/tutorial/uiswing/components/menu.html
Result image
menu.addSeparator();
submenu = new JMenu("A submenu");
menuItem = new JMenuItem("An item in the submenu");
submenu.add(menuItem);
menuItem = new JMenuItem("Another item");
submenu.add(menuItem);
menu.add(submenu); // you add the submenu to your existing menu
Related
Just a simple GUI
Shows a JMenu >> JMenuItem
One of those menu items shows a JPopupMenu
My issue is that the JMenuItems' are setVisible(false) on press, by default
I've tried it with MouseAdapter too.
public class SubMenu extends JFrame
{
JMenuBar menubar = new JMenuBar();
JMenu File = new JMenu("File");
JMenuItem New = new JMenuItem("New");
JMenuItem Open = new JMenuItem("Open");
JMenuItem Save = new JMenuItem("Save");
JMenuItem Import = new JMenuItem("Import");
JPopupMenu Import_Popup = new JPopupMenu();
JMenuItem Import_Pop_1 = new JMenuItem("Import newsfeed list...");
JMenuItem Import_Pop_2 = new JMenuItem("Import bookmarks...");
JMenuItem Import_Pop_3 = new JMenuItem("Import mail...");
JMenuItem Exit = new JMenuItem("Exit");
SubMenu()
{
setTitle("SubMenu");
setSize(500, 500);
setLocation(200, 200);
setVisible(true);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
menubar.setBounds(0, 0, 2000, 30);
add(menubar);
menubar.add(File);
File.add(New);
File.add(Open);
File.add(Save);
File.add(new JSeparator());
File.add(Import);
File.add(new JSeparator());
File.add(Exit);
add(Import_Popup);
Import_Popup.add(Import_Pop_1);
Import_Popup.add(Import_Pop_2);
Import_Popup.add(Import_Pop_3);
// The issue is Right here.
Import.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
Import_Popup.show(Import, Import.getWidth(), 0);
}
});
}// Constructor
public static void main(String[] args)
{
new SubMenu();
}
}
Raises this exception on Press :
component must be showing on the screen to determine its location
Is there a way to keep JMenuItems visible after press?
I have a simple prgram where i want to create a menu bar, menu and menu items. but two problems occur in my program. 1 my frame does not display on center of screen while i have added this code:
mainframe.setLocationRelativeTo(null);
Second problem is that no menu is displaying in frame, however i have added 3-4 menus and menu items inside them
this is my application code:
package javaProject;
import javax.swing.*;
public class Converter {
public static void main(String[] args)
{
JFrame mainframe=new JFrame("Converter");
mainframe.setResizable(true);
mainframe.setSize(500, 400);
mainframe.setLocationRelativeTo(null);
mainframe.setVisible(true);
JMenuBar menu=new JMenuBar();
mainframe.setJMenuBar(menu);
// file menu starts
JMenu file=new JMenu("File");
menu.add(file);
JMenuItem open= new JMenuItem("Open");
file.add(open);
JMenuItem save=new JMenuItem("Save");
file.add(save);
JMenuItem play=new JMenuItem("Play");
file.add(play);
JMenuItem pause=new JMenuItem("Pause");
file.add(pause);
JMenuItem exit= new JMenuItem("Exit");
file.add(exit);
// edit menu
JMenu edit= new JMenu("Edit");
menu.add(edit);
JMenuItem paste=new JMenuItem("Paste");
edit.add(paste);
JMenuItem remove=new JMenuItem("Remove");
edit.add(remove);
JMenuItem removeall=new JMenuItem("Remove All");
edit.add(removeall);
// convert menu
JMenu convert=new JMenu ("Convert");
menu.add(convert);
// help menu
JMenu help=new JMenu ("Help");
menu.add(help);
JMenuItem supportedformats=new JMenuItem("Supported Formats");
help.add(supportedformats);
JMenuItem version=new JMenuItem("Version");
help.add(version);
JMenuItem aboutus=new JMenuItem("About Us");
help.add(aboutus);
JMenuItem updates=new JMenuItem("Check For Updates");
help.add(updates);
}
}
Move the code to be after menu initialization
mainframe.setSize(500, 400);
mainframe.setLocationRelativeTo(null);
mainframe.setVisible(true);
I have the following SwingMenu class.
package base;
import javax.swing.*;
public class SwingMenu {
public static void main(String[] args) {
SwingMenu s = new SwingMenu();
}
public SwingMenu() {
JFrame frame = new JFrame(
"Creating a JMenuBar, JMenu, JMenuItem and seprator Component");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
JMenu filemenu = new JMenu("File");
filemenu.add(new JSeparator());
JMenu editmenu = new JMenu("Edit");
editmenu.add(new JSeparator());
JMenuItem fileItem1 = new JMenuItem("New");
JMenuItem fileItem2 = new JMenuItem("Open");
JMenuItem fileItem3 = new JMenuItem("Close");
fileItem3.add(new JSeparator());
JMenuItem fileItem4 = new JMenuItem("Save");
JMenuItem editItem1 = new JMenuItem("Cut");
JMenuItem editItem2 = new JMenuItem("Copy");
editItem2.add(new JSeparator());
JMenuItem editItem3 = new JMenuItem("Paste");
JMenuItem editItem4 = new JMenuItem("Insert");
filemenu.add(fileItem1);
filemenu.add(fileItem2);
filemenu.add(fileItem3);
filemenu.add(fileItem4);
editmenu.add(editItem1);
editmenu.add(editItem2);
editmenu.add(editItem3);
editmenu.add(editItem4);
menubar.add(filemenu);
menubar.add(editmenu);
frame.setJMenuBar(menubar);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
And I want to display the Menu by calling it from the this main class.
package base;
import javax.swing.*;
import java.awt.*;
import base.SwingMenu;
public class StickyNotes {
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Java Sticky Notes");
frame.setPreferredSize(new Dimension(5000, 5000));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
// Add Label
JLabel label = new JLabel("Type Below");
frame.getContentPane().add(label);
// Add Main Menu
SwingMenu mainBar = new SwingMenu();
//frame.setJMenuBar(mainBar);
//frame.getContentPane().add(mainBar);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public Container createContentPane() {
// Create the content-pane-to-be.
JPanel jplContentPane = new JPanel(new BorderLayout());
jplContentPane.setLayout(new BorderLayout());
jplContentPane.setOpaque(true);
return jplContentPane;
}
public static void main(String[] args) {
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
I just cannot figure it out all morning :) How do I get the Menu Bar to show up in Java Swing?
// Add Main Menu
SwingMenu mainBar = new SwingMenu();
Check this modified code example :
import javax.swing.*;
import java.awt.*;
public class StickyNotes {
private JMenuBar getMenuBar()
{
JMenuBar menubar = new JMenuBar();
JMenu filemenu = new JMenu("File");
filemenu.add(new JSeparator());
JMenu editmenu = new JMenu("Edit");
editmenu.add(new JSeparator());
JMenuItem fileItem1 = new JMenuItem("New");
JMenuItem fileItem2 = new JMenuItem("Open");
JMenuItem fileItem3 = new JMenuItem("Close");
fileItem3.add(new JSeparator());
JMenuItem fileItem4 = new JMenuItem("Save");
JMenuItem editItem1 = new JMenuItem("Cut");
JMenuItem editItem2 = new JMenuItem("Copy");
editItem2.add(new JSeparator());
JMenuItem editItem3 = new JMenuItem("Paste");
JMenuItem editItem4 = new JMenuItem("Insert");
filemenu.add(fileItem1);
filemenu.add(fileItem2);
filemenu.add(fileItem3);
filemenu.add(fileItem4);
editmenu.add(editItem1);
editmenu.add(editItem2);
editmenu.add(editItem3);
editmenu.add(editItem4);
menubar.add(filemenu);
menubar.add(editmenu);
return menubar;
}
private void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Java Sticky Notes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
// Add Label
JLabel label = new JLabel("Type Below");
frame.getContentPane().add(label);
// Add Main Menu
frame.setJMenuBar(getMenuBar());
// Display the window.
frame.pack();
frame.setVisible(true);
}
public Container createContentPane() {
// Create the content-pane-to-be.
JPanel jplContentPane = new JPanel(new BorderLayout());
jplContentPane.setLayout(new BorderLayout());
jplContentPane.setOpaque(true);
return jplContentPane;
}
public static void main(String[] args) {
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new StickyNotes().createAndShowGUI();
}
});
}
}
Or you can modify your code a bit like this, if you really wanted to keep the JMenuBar set up in a different Class, where you can simply make an Object of the SwingMenu Class and call the method getMenuBar() by making an Object of this Class :
import javax.swing.*;
import java.awt.*;
public class StickyNotes {
private void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Java Sticky Notes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
// Add Label
JLabel label = new JLabel("Type Below");
frame.getContentPane().add(label);
// Add Main Menu
SwingMenu swingMenu = new SwingMenu();
frame.setJMenuBar(swingMenu.getMenuBar());
// Display the window.
frame.pack();
frame.setVisible(true);
}
public Container createContentPane() {
// Create the content-pane-to-be.
JPanel jplContentPane = new JPanel(new BorderLayout());
jplContentPane.setLayout(new BorderLayout());
jplContentPane.setOpaque(true);
return jplContentPane;
}
public static void main(String[] args) {
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new StickyNotes().createAndShowGUI();
}
});
}
}
class SwingMenu {
public JMenuBar getMenuBar()
{
JMenuBar menubar = new JMenuBar();
JMenu filemenu = new JMenu("File");
filemenu.add(new JSeparator());
JMenu editmenu = new JMenu("Edit");
editmenu.add(new JSeparator());
JMenuItem fileItem1 = new JMenuItem("New");
JMenuItem fileItem2 = new JMenuItem("Open");
JMenuItem fileItem3 = new JMenuItem("Close");
fileItem3.add(new JSeparator());
JMenuItem fileItem4 = new JMenuItem("Save");
JMenuItem editItem1 = new JMenuItem("Cut");
JMenuItem editItem2 = new JMenuItem("Copy");
editItem2.add(new JSeparator());
JMenuItem editItem3 = new JMenuItem("Paste");
JMenuItem editItem4 = new JMenuItem("Insert");
filemenu.add(fileItem1);
filemenu.add(fileItem2);
filemenu.add(fileItem3);
filemenu.add(fileItem4);
editmenu.add(editItem1);
editmenu.add(editItem2);
editmenu.add(editItem3);
editmenu.add(editItem4);
menubar.add(filemenu);
menubar.add(editmenu);
return menubar;
}
}
You are creating 2 different JFrames. After creating the JFrame:
JFrame frame = new JFrame("Java Sticky Notes");
Create the menu bar and assign it to the JFrame:
JMenuBar menubar = new JMenuBar();
// ...
frame.setJMenuBar(menubar);
No need for:
SwingMenu mainBar = new SwingMenu();
Try this out:
This basically makes SwingMenu a manu bar which will help encapsulating the build of the menu bar.
public class SwingMenu extends JMenuBar{
public SwingMenu() {
JMenu filemenu = new JMenu("File");
JMenu editmenu = new JMenu("Edit");
// Build your file menu and edit menu here...
add(filemenu);
add(editmenu);
}
}
Now, in your createAndShowGUI() just create a form and add the newly created menu bar to it.
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Java Sticky Notes");
// other stuff...
// Add Main Menu
SwingMenu mainBar = new SwingMenu();
frame.setJMenuBar(mainBar);
// Display the window.
frame.pack();
frame.setVisible(true);
}
I am making a simple game where numbers fall from top to bottom
and I have to type the number. (number is erased as I type the number)
This is coordinated with a Zen.java which is a JApplet file.
I am trying to make a menu for this game with a typical menu bar.
So far, I've tried this..
public class MenuApplet extends JApplet{
public void init(){
JMenuBar menubar = new JMenuBar();
JMenu menuOption = new JMenu("Option");
JMenuItem NewGame = new JMenuItem("New Game");
menuOption.add(NewGame);
JMenuItem exitGame = new JMenuItem("Exit Game");
menuOption.add(exitGame);
JMenu menuLevel = new JMenu("Level");
JMenuItem levelOne = new JMenuItem("Level One");
JMenuItem levelTwo = new JMenuItem("Level Two");
JMenuItem levelThree = new JMenuItem("Level Three");
}
}
Right before my main method. However, the menu bar doesn't even show up.
I would appreciate a couple of advices.
/* <applet code='MenuApplet' width=200 height=100></applet> */
import javax.swing.*;
public class MenuApplet extends JApplet{
public void init(){
JMenuBar menubar = new JMenuBar();
JMenu menuOption = new JMenu("Option");
JMenuItem NewGame = new JMenuItem("New Game");
menuOption.add(NewGame);
JMenuItem exitGame = new JMenuItem("Exit Game");
menuOption.add(exitGame);
JMenu menuLevel = new JMenu("Level");
JMenuItem levelOne = new JMenuItem("Level One");
JMenuItem levelTwo = new JMenuItem("Level Two");
JMenuItem levelThree = new JMenuItem("Level Three");
// the menu items, menus and menu bar all need
// to be ADDED to something!
menubar.add(menuOption);
menuOption.add(NewGame);
menuOption.add(exitGame);
menubar.add(menuLevel);
menuLevel.add(levelOne);
menuLevel.add(levelTwo);
menuLevel.add(levelThree);
setJMenuBar(menubar);
}
}
You have to add JMenu into JMenuBar and finally use setJMenuBar to set menubar object.
menubar.add(menuOption);
menubar.add(exitGame);
setJMenuBar(menubar);
I'm having problems with my code. The sub-menu for the (Music) menu should be a radio button type.
Here's my first code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AMBAT_FLAB1 extends JApplet implements ActionListener{
JMenuBar mainBar = new JMenuBar();
JMenu menu1 = new JMenu("File");
JMenu menu2 = new JMenu("Format");
JMenu menu3 = new JMenu("Background");
//for file
JMenuItem open = new JMenuItem("Open");
JMenuItem save = new JMenuItem("Save");
JMenuItem reset = new JMenuItem("Reset");
//for format
JMenuItem setFont = new JMenuItem("Set Font");
JMenuItem setColor = new JMenuItem("Set Color");
//for background
JMenuItem image = new JMenuItem("Images");
JMenuItem music = new JMenuItem("Music");
//submenu of music
JRadioButtonMenuItem play = new JRadioButtonMenuItem("Play");
JRadioButtonMenuItem loop = new JRadioButtonMenuItem("Loop");
JRadioButtonMenuItem stop = new JRadioButtonMenuItem("Stop");
ButtonGroup group = new ButtonGroup();
//file chooser
//JFileChooser fileChooser = new JFileChooser();
//fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
//text area
JTextArea myArea = new JTextArea(50, 50);
JScrollPane scrollingArea = new JScrollPane(myArea);
Container con = getContentPane();
public void init(){
setJMenuBar(mainBar);
mainBar.add(menu1);
mainBar.add(menu2);
mainBar.add(menu3);
menu1.add(open);
menu1.add(save);
menu1.add(reset);
menu2.add(setFont);
menu2.add(setColor);
menu3.add(image);
menu3.add(music);
music.group.add(play);
//group.add(loop);
//music.add(stop);
open.addActionListener(this);
save.addActionListener(this);
reset.addActionListener(this);
setFont.addActionListener(this);
setColor.addActionListener(this);
image.addActionListener(this);
music.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
}
}
When I try to run it, the Music menu doesn't appear. It changes to the Play (radio button). Does the button group help? When i tried to use the button group nothing happens.
Like this?
/* <applet code='AMBAT_FLAB1' width=220 height=100></applet> */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AMBAT_FLAB1 extends JApplet implements ActionListener{
JMenuBar mainBar = new JMenuBar();
JMenu menu1 = new JMenu("File");
JMenu menu2 = new JMenu("Format");
JMenu menu3 = new JMenu("Background");
//for file
JMenuItem open = new JMenuItem("Open");
JMenuItem save = new JMenuItem("Save");
JMenuItem reset = new JMenuItem("Reset");
//for format
JMenuItem setFont = new JMenuItem("Set Font");
JMenuItem setColor = new JMenuItem("Set Color");
//for background
JMenuItem image = new JMenuItem("Images");
JMenu music = new JMenu("Music");
//submenu of music
JRadioButtonMenuItem play = new JRadioButtonMenuItem("Play");
JRadioButtonMenuItem loop = new JRadioButtonMenuItem("Loop");
JRadioButtonMenuItem stop = new JRadioButtonMenuItem("Stop");
ButtonGroup group = new ButtonGroup();
//file chooser
//JFileChooser fileChooser = new JFileChooser();
//fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
//text area
JTextArea myArea = new JTextArea(50, 50);
JScrollPane scrollingArea = new JScrollPane(myArea);
Container con = getContentPane();
public void init(){
setJMenuBar(mainBar);
mainBar.add(menu1);
mainBar.add(menu2);
mainBar.add(menu3);
menu1.add(open);
menu1.add(save);
menu1.add(reset);
menu2.add(setFont);
menu2.add(setColor);
menu3.add(image);
menu3.add(music);
group.add(play);
group.add(loop);
group.add(stop);
music.add(play);
music.add(loop);
music.add(stop);
//music.add(stop);
open.addActionListener(this);
save.addActionListener(this);
reset.addActionListener(this);
setFont.addActionListener(this);
setColor.addActionListener(this);
image.addActionListener(this);
music.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
}
}
The mistakes in the code were basically:
If Music had children, it had to be a JMenu, not a JMenuItem
A ButtonGroup is a logical group (e.g. to make radio buttons out of a group of buttons), it is not a container. So besides adding the buttons to the group, it is necessary to add them to the Music JMenu.
You have a syntax error in your source code. Try commenting out the line that is failing and recompiling. That should give you a bit more information in your interface (GUI).