0

I am learning Java in school, and we are learning to make GUI's.

Toady we are doing Menubars and such, which I think I understand, and adding menuitems, which I understand. But when I want them to do something when a user clicks on them. I am lost.

Here is my code of the UI:

package UserInterface;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author Jesse
 */
public class AirlineReservation {

    private JFrame frame;
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenuItem addAirport;
    private JMenuItem addAirline;
    private JMenuItem addFlight;
    private JMenuItem exit;
    private JMenu aboutMenu;
    private JMenuItem bookFlight;
    private JMenuItem aboutInfo;
    private JMenu bookMenu;

    public AirlineReservation() {
        initComponents();
    }

    private void initComponents() {
        frame = new JFrame("Airline Reservation ");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setVisible(true);

        // Initializing the JMenuBar
        menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        // Initializing menu items
        addAirport = new JMenuItem("Add Airport");
        addAirline = new JMenuItem("Add Airline");
        addFlight = new JMenuItem("Add Flight");
        exit = new JMenuItem("Exit");
        // Initializing the JMenu and adding JMenuItems
        fileMenu = new JMenu("File");
        fileMenu.setMnemonic('F');
        fileMenu.add(addAirport);
        fileMenu.add(addAirline);
        fileMenu.add(addFlight);
        fileMenu.add(exit);
        menuBar.add(fileMenu);
        fileMenu.addActionListener(new FileMenuAction());

        //initializing the Jmenu and adding JMenuItems (bookFlight)
        bookFlight = new JMenuItem("Flight Reservation");
        bookMenu = new JMenu("Book");
        bookMenu.setMnemonic('B');
        bookMenu.add(bookFlight);
        bookMenu.addActionListener(new BookMenuAction());
        menuBar.add(bookMenu);

        aboutInfo = new JMenuItem("dhdhd");
        aboutMenu = new JMenu("About");
        aboutMenu.add(aboutInfo);
        aboutMenu.addActionListener(new AboutMenuAction());
        menuBar.add(aboutInfo);
    }

    private class FileMenuAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand() == "Add Airport") {
                System.out.println("Add Airport Clicked");
            } else if (e.getActionCommand() == "Add Airline") {
                System.out.println("Add Airline Clicked");

            } else if (e.getActionCommand() == "Add Flight") {
                System.out.println("Add Flight Clicked");
            }
        }
    }

    private class BookMenuAction implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand() == "sjsjsjs") {
                System.out.println("fjfhfhf");
            }
        }
    }

    private class AboutMenuAction implements ActionListener {

        public void actionPerformed(ActionEvent e) {}
    }
}

the problem is that when I click on a menuitems, it seems as if my actionlistener does not catch the click? It has to be a small logical error, and I can seem to put my finger on it :(

Tiny
  • 27,221
  • 105
  • 339
  • 599
user3026473
  • 23
  • 1
  • 6

1 Answers1

0

You never add any ActionListener to your menu items. So obviously, nothing happens when you click them.

On the other hand, you're adding a listener to each menu, but you shouldn't, because nothing should happen when clicking the menu (expect it should open itself, but it does that automatically).

The next steps will be

  • to avoid comparing strings with ==
  • to actually assign an action command to your menu items, since your listener uses that to know which item was clicked

I would suggest creating a single listener class for each item instead of using one listener class to handle several items.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I thought about that. I was following my professors examples. My professor only wants one inner class to handle the menu items. I would make multiple classes for each, but its what she wants, but it seems less organized. btw It works like a charm now. Thanks guys! – user3026473 Feb 22 '14 at 15:53