Why I cant see all the elements for my menubar? - java

Sometimes 2 elements are on my menu, sometimes all but if the elements are not visible I need to resize my window. If I resize my window the elements appear. Why?
This is my code:
//Class TextEditor start
public class TextEditor extends JFrame{
private JMenuBar menuBar;
private JMenu file,edit,format,view,help;
private JMenuItem newFile;
private JMenuItem exit;
//Main method start
public static void main(String[] args){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch (Exception e){
}
new TextEditor();
}
//Main class end
//Class constructor start
private TextEditor(){
super("Untitled");
sendUI(this);
sendMenuBar();
}
//Class constructor end
//Menu bar start//
public void sendMenuBar(){
menuBar = new JMenuBar();
setJMenuBar(menuBar);
//File menu and Items
file = new JMenu(" File ");
newFile = new JMenuItem("New");
exit = new JMenuItem("Exit");
menuBar.add(file);
file.add(newFile);
file.add(exit);
//Edit menu and items
edit = new JMenu(" Edit ");
menuBar.add(edit);
//Format menu and items
format = new JMenu(" Format ");
menuBar.add(format);
//View menu and items
view = new JMenu(" View ");
menuBar.add(view);
//Help menu and items
help = new JMenu(" Help ");
menuBar.add(help);
}
private void sendUI(TextEditor texteditor) {
texteditor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
texteditor.setSize(700,400);
texteditor.setLocationRelativeTo(null);
texteditor.setVisible(true);
}
}
//Class TextEditor end
When the error appears:
After I resized the window:

You need to call senUI after sendMenuBar.

Related

Why aren't my menu drop downs names not showing Java

I'm doing a program where I'm working with getting the menu's to appear, but for some reason I can't seem to get the names for my menu drop downs to appear.I would greartly appreciante some help with this matter.
public class Application extends JPanel implements ActionListener{
JPanel p;
JFrame f;
JMenu m;
JMenu m2;
JMenuBar menu;
JMenuItem item;
JMenuItem item2;
Application()
{
super();
//creating panel
f=new JFrame("CMPSC 221 Exam 2 ");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200,200);
f.setContentPane(new JDesktopPane());
m2 = new JMenu();
menu = new JMenuBar();
m = new JMenu();
//adding the the drop down menu
m.setName("File");
m.add("New");
m.addSeparator();
m.add("Open");
menu.add(m);
m2.setName("Edit");
m2.add("Copy");
m2.addSeparator();
m2.add("Paste");
menu.add(m2);
menu.setVisible(true);
f.setJMenuBar(menu);
f.setVisible(true);
}
Don't use m.setName - pass the name into JMenu() constructor

JPopupMenu and JMenuItem general usage

I have a simple question; I am trying to add a menu to my program. This is what I have thus far:
public static void main(String args[]){
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {}
JFrame cipherGUIFrame = new CipherGUIFrame();
cipherGUIFrame.setVisible(true);
JMenuBar bar = new JMenuBar();;
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenuItem open = new JMenuItem("Open");
JMenuItem save = new JMenuItem("Save");
JMenuItem cut = new JMenuItem("Cut");
JMenuItem copy = new JMenuItem("Copy");
JMenuItem paste = new JMenuItem("Paste");
JSeparator sep = new JSeparator();
JMenuItem find = new JMenuItem("Find");
JPopupMenu options = new JPopupMenu("Options");
options.setVisible(true);
file.add(open);
file.add(save);
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(sep);
edit.add(find);
edit.add(options);
bar.add(file);
bar.add(edit);
cipherGUIFrame.setJMenuBar(bar);
}
I am trying to achieve an effect similar to this diagram: http://i.imgur.com/GYi0S9R.jpg .
Is "Options" not the JPopupMenu? It doesn't seem to show up! Or is it simply a JMenuItem and JPopupMenu is the new box that comes up when you hover over it?
A sub menu is just that, a menu which is contained within another menu
Try using something like...
JMenu options = new JMenu("Options");
options.add(new JRadioButtonMenuItem("Forward"));
options.add(new JRadioButtonMenuItem("Backward"));
options.addSeparator();
options.add(new JCheckBoxMenuItem("Case Sensetive"));
Take a closer look at How to Use Menus for more details

Where do I put this statement 'setJMenuBar(menuBar);'?

Main Class:
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Main extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("Checking Account Actions");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
CheckingAccountActions panel = new CheckingAccountActions();
MyWindowAdapter winAdapter = new MyWindowAdapter(panel);
frame.addWindowListener(winAdapter);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
} // Main()
class MyWindowAdapter extends WindowAdapter {
private CheckingAccountActions saved;
public MyWindowAdapter(CheckingAccountActions saved) {
this.saved = saved;
}
// in your window closing method
// check the state of checkActions first before doing anything
public void windowClosing(WindowEvent e) {
// note -- don't check for saved in a static way
// use a method on the instance.
if(!saved.saved) {
String message = "The data in the application is not saved.\n"
+ "Would you like to save it before exiting the
+ application?";
int confirm = JOptionPane.showConfirmDialog (null, message);
if (confirm == JOptionPane.YES_OPTION)
CheckingAccountActions.chooseFile(2);
}
JFrame frame = (JFrame)e.getSource();
frame.setVisible(false);
// Main.frame.setVisible(false);
System.exit(0);
}
} // MyWindowAdapter()
This class, as you see, extends JPanel and this is where my Menu items are initialized, but do I use Main class for this statement 'setJMenuBar(menuBar);', since it gives an error in CheckingAccountActions because JFrame is in MAIN.
CheckingAccountActions class:
public class CheckingAccountActions extends JPanel {
// Panel
private JLabel message;
// Menu
private JMenuBar menuBar;
private JMenu File;
private JMenuItem Read, Write;
private JMenu Account;
private JMenuItem NewAccount, ListAccounts, ListChecks, ListDeposits, FindAccount;
private JMenu Transactions;
private JMenuItem AddTransactions;
// code...
//
//
// code...
public CheckingAccountActions() {
//************************** PANEL *****************************************
// JLabel
message = new JLabel("Please choose one of the items: ");
message.setFont(new Font("Helvetica", Font.BOLD, 15));
CheckingAccountActionsListener listener = new CheckingAccountActionsListener();
//************************** MENU ******************************************
// Menu
File = new JMenu("File");
// MenuItem
Read = new JMenuItem("Read from file");
Write = new JMenuItem("Write to file");
// ActionListener
Read.addActionListener(listener);
Write.addActionListener(listener);
// Add Buttons to Menu
File.add(Read);
File.add(Write);
// Menu
Account = new JMenu("Account");
// MenuItem
NewAccount = new JMenuItem("Add new account");
ListAccounts = new JMenuItem("List accounts transaction");
ListChecks = new JMenuItem("List all checks");
ListDeposits = new JMenuItem("List all deposits");
FindAccount = new JMenuItem("Find an account");
// ActionListener
NewAccount.addActionListener(listener);
ListAccounts.addActionListener(listener);
ListChecks.addActionListener(listener);
ListDeposits.addActionListener(listener);
FindAccount.addActionListener(listener);
// Add Buttons to Menu
Account.add(NewAccount);
Account.add(ListAccounts);
Account.add(ListChecks);
Account.add(ListDeposits);
Account.add(FindAccount);
// Menu
Transactions = new JMenu("Transactions");
// MenuItem
AddTransactions = new JMenuItem("Add Transactions");
// ActionListener
AddTransactions.addActionListener(listener);
// Add Buttons to Menu
Transactions.add(AddTransactions);
// MenuBar
menuBar = new JMenuBar();
menuBar.add(File);
menuBar.add(Account);
menuBar.add(Transactions);
setBackground(Color.white);
setPreferredSize(new Dimension(240, 250));
setJMenuBar(menuBar);
}
private class CheckingAccountActionsListener implements ActionListener {
// code...
}
Edit: what I am confused about is how to add my MenuBar to the Frame while the Frame is in another class?
FINAL EDIT: I got it working. I just moved all my JFrame components to CheckingAccountActions class.
Take a look at the section from the Swing tutorial on How to Use Menus. The MenuDemo example will show you one way to structure your program.
It also shows you the proper way to create your GUI on the Event Dispatch Thread.
setJMenuBar(menubar) is not what you actually looking for. If you want to set the menubar to your frame, just add the menu bar to the frame with the method add(menuBar).

JMenuItem updating without restarting the client

This code adds the user's favorited songs to a JmenuItem from an ArrayMap
public void actionPerformed(ActionEvent evt) {
String cmd = evt.getActionCommand();
if (cmd != null) {
if (cmd.equalsIgnoreCase("Favorite song")) {
Music.f.add(Music.s);
System.out.println(Music.s + " added to favorites");
System.out.println(Music.f + " current list");
}
}
}
public void initUI() {
try {
//...
JMenu fileMenu = new JMenu("Music And Sound Options");
JMenu favorites = new JMenu("Favorite songs");
for (String name : Music.f) {
JMenuItem menuItem = new JMenuItem(name);
menuItem.addActionListener(this);
favorites.add(menuItem);
}
JMenuBar menuBar = new JMenuBar();
JMenuBar jmenubar = new JMenuBar();
frame.add(jmenubar);
menuBar.add(favorites);
frame.getContentPane().add(menuBar, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true); // can see the client
init();
//...
} catch (Exception e) { e.printStackTrace(); }
}
I want the list of songs to update after a song is added, instead of having to restart the client to see more songs
//JMenu favorites = new JMenu("Favorite songs");
favorites = new JMenu("Favorite songs");
The Favorites menu needs to be defined as a class variable. Then when you do this your ActionListener can now reference the menu and add a new menu item to the menu.
if (cmd.equalsIgnoreCase("Favorite song")) {
Music.f.add(Music.s);
System.out.println(Music.s + " added to favorites");
System.out.println(Music.f + " current list");
JMenuItem item = new JMenItem(...);
favorites.add( item );
If you store a reference to your JMenuBar as a class field, you can call menuBar.removeAll() and repopulate it with new menu items whenever you want (though make sure you do it on the Swing thread when you do, using SwingUtilities.invokeLater() or your Swing method of choice).

a GUI java program needing an action event button

I am making a simple java program that represents a Microsoft Word menu bar, and in the file menu I add an exit button... it does nothing.
I need your help to tell me what to do to make the exit button actually exit and close the program.
Here is my code:
public class MicrosoftWordMenu {
public static void main(String [] args)
{
JPanel panel = new JPanel();
JFrame frame = new JFrame("Microsoft Word");
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenu menu1 = new JMenu("Edit");
JMenu menu2 = new JMenu("View");
JMenu menu3 = new JMenu("Insert");
JMenu menu4 = new JMenu("Format");
JMenu menu5 = new JMenu("Tools");
JMenu menu6 = new JMenu("Table");
JMenu menu7 = new JMenu("Window");
JMenu menu8 = new JMenu("Help");
frame.add(bar, BorderLayout.NORTH);
frame.add(panel);
bar.add(menu);
bar.add(menu1);
bar.add(menu2);
bar.add(menu3);
bar.add(menu4);
bar.add(menu5);
bar.add(menu6);
bar.add(menu7);
bar.add(menu8);
JMenuItem menuitem = new JMenuItem("New...");
JMenuItem menuitem1 = new JMenuItem("Open...");
JMenuItem menuitem2 = new JMenuItem("Close");
JMenuItem menuitem3 = new JMenuItem("Save");
JMenuItem menuitem4 = new JMenuItem("Save as...");
JMenuItem menuitem5 = new JMenuItem("Save as web page...");
JMenuItem menuitem6 = new JMenuItem("Web page preview ");
JMenuItem menuitem7 = new JMenuItem("Print ");
JMenuItem menuitem8 = new JMenuItem("Exit");
menu.add(menuitem);
menu.add(menuitem1);
menu.add(menuitem2);
menu.add(menuitem3);
menu.add(menuitem4);
menu.add(menuitem5);
menu.add(menuitem6);
menu.add(menuitem7);
menu.add(menuitem8);
frame.setSize(600,100);
frame.setVisible(true);
}
}
Also consider using Action to let components share functionality, as shown here.
Calling System.exit(0) on menu selection would do just fine.
At the moment you're just creating the GUI element, but they're basically empty shells. In particular, you've created items that appear on the menu, but you haven't added any behaviour to those items. (Since you haven't told your program anywhere what to do when an item is clicked, what did you think would happen)?
In order to provide this behaviour, you'll need to register an ActionListener, which will be called when something happens on an element, and will let you take an appropriate action at that point (such as calling System.exit(0). See the tutorial on Writing [Swing] Event listeners for details.
I always extend the AbstractAction class (which implements the ActionListener interface) which allows you to reuse your actions. By extending the AbstractAction, the actionPerformed(ActionEvent e) method will be invoked very time your button is clicked.
public class CloseAction extends AbstractAction {
public CloseAction() {
super("Close");
}
public actionPerformed(ActionEvent e) {
System.exit(1);
}
}
Now, to apply the above action to one of your buttons, you simply need to follow the below piece of code.
CloseButton.setAction(new CloseAction());
The close button will now shut down your application each time it gets pressed.
Use the ExitAction defined in Closing an Application. Then clicking on the Exit menu item will be just like clicking on the Close icon at the top right of the window. This allows for consistency when closing an application in case you ever need to do close processing.
Edit:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
...
// JMenuItem menuitem8 = new JMenuItem("Exit");
JMenuItem menuitem8 = new JMenuItem( new ExitAction() );

Categories