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

code_flow picture code_flow · Nov 29, 2013 · Viewed 8.4k times · Source

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.

Answer

camickr picture camickr · Nov 29, 2013

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.