trying to figure out how to make the menus work except exit and help

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 702 Views - Last Post: 03 December 2017 - 10:47 PM Rate Topic: -----

#16 Mike1874  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 10:37 AM

Just save both in a same folder file and run the code then you'll see what I'm talking about 😉
Was This Post Helpful? 0
  • +
  • -

#17 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3486
  • View blog
  • Posts: 15,763
  • Joined: 20-September 08

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 11:11 AM

Some of your menu items are JMenu instead of JMenuItem. isSelected will never return true, you need to get the event source and compare it with your menu items
Was This Post Helpful? 0
  • +
  • -

#18 Mike1874  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 05:52 PM

public void actionPerformed(ActionEvent e)
      
      {
      
         if(e.getActionCommand().equals(" createItem "))
         
            ra.createArray();
         
            if(e.getActionCommand().equals("sortItem "))
            
               ra.sortArray();
         
            if(e.getActionCommand().equals("searchItem"))            
               ra.binarySearch();
         
              else if(e.getActionCommand().equals("displayItem"))
            
               ra.displayArray();
             
         
         }
      
      }





I changed it but now I get an error message when I try to run it
Menu.java:290: error: class, interface, or enum expected


Was This Post Helpful? 0
  • +
  • -

#19 Mike1874  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 06:13 PM

I figured it out but nothing is still displayed when I run it.
Was This Post Helpful? 0
  • +
  • -

#20 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12209
  • View blog
  • Posts: 45,287
  • Joined: 27-December 08

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 07:25 PM

Did you add the JComponents to the JFrame? Make sure you are doing that.

If you are still encountering difficulties, please post your revised code as well as a clear description of your problems/errors. We're happy to help, but it's not worthwhile for anyone for us to sit and guess at what you have done.
Was This Post Helpful? 0
  • +
  • -

#21 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3486
  • View blog
  • Posts: 15,763
  • Joined: 20-September 08

Re: trying to figure out how to make the menus work except exit and help

Posted 03 December 2017 - 05:06 AM

Quote

please post your revised code as well as a clear description of your problems/errors.

Amen to that. Both classes please
Was This Post Helpful? 0
  • +
  • -

#22 Mike1874  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

Re: trying to figure out how to make the menus work except exit and help

Posted 03 December 2017 - 10:11 PM

so this is my assignment
1. Help- Brief explanation of program
2. Create an Array - Create an array of size 100 and fill it up with 100 numbers from 1 to 100 using random class.
3. Sort- Sort the array in assenting order
4. Search – Use binary Search a value in your array.
5. Display - Display the content of array
6. Quit- Quit the Program

the first class which is called RandomArray.java

import java.util.Random;

import javax.swing.JOptionPane;

import java.util.List;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Scanner;

public class RandomArray

{

   private final int NUM_ELEMENTS = 5;//Size of the array

   private int[] numbers = new int[NUM_ELEMENTS];//Array of numbers

   Scanner keyboard = new Scanner(System.in);

/**

The createArray method fill the array with random number to 100.

*/


   public void createArray()
   
   {
   
   //Creates a Random object
   
      Random rand = new Random();
   
   
   //Fills an array the size of 100 with random numbers 1-100
   
   
      for (int i = 0; i < numbers.length; i++)          
      {
      
         int n = rand.nextInt(NUM_ELEMENTS);
      
         numbers[i] = n;         
      }
   
      JOptionPane.showMessageDialog(null, "Create and filling array.");
   }

/**

The sortArray method sorts all the numbers in ascending order

*/

   public void sortArray()
   
   {
   
      int lastPos;//Marks the last position to compare
   
      int temp;//Used to swap to elements
   
   
      for( lastPos = numbers.length - 1; lastPos >= 0; lastPos--)
      {
      
      //The inner loop steps through the array, comparing each element to
      
      //compare during each pass through the array. Initially lastPos is the
      
      //index of the last element in the array. During each iteration, it is
      
      //decreased by one.
      
      
         for (int i = 0; i <= lastPos - 1; i++)
         
         {
         
         //Compare an element with its neighbors
         
         
            if(numbers[i] > numbers[i + 1])
            
            {
            
               temp = numbers[i];
            
               numbers[i] = numbers[i + 1];
            
               numbers[i + 1] = temp;
            
            }
         
         }
      
      }
   
      JOptionPane.showMessageDialog(null, "Sorting in ascending order.");
   
   //Displays array in ascending order.
   
      System.out.println("Sorted Order");
   
      System.out.println("*************");
   
   
      for (int i = 0; i < numbers.length; i++)
      
      {
      
         System.out.println("Position " + (i+1) + ":" + numbers[i]);
      
      }
   
   }

/**

The binarySearch method performs a binary search on the array. The array is 

searched for the number input by user.

*/


   public void binarySearch()
   {
   
      String input;
   
      int counter;//Counter variable      
   
      int item;//Number input by user
   
      int first;//First array element
   
      int last;//Last array element
   
      int middle;//Mid point of search
   
   //Get the number.
   
      input = JOptionPane.showInputDialog("Enter the search value:");
   
      item = Integer.parseInt(input);
   
   //Set the initial values
   
      first = 0;
   
      last = NUM_ELEMENTS - 1;
   
      middle = (first + last)/2;//Midpoint
   
   //Search for the value    
   
   
      while( first <= last )
      
      {
      
      
         if ( numbers[middle] < item )
         
            first = middle + 1;
         
         
         else if ( numbers[middle] == item )
         
         {
         
            JOptionPane.showMessageDialog(null, item + " found at location " + (middle + 1) + ".");
         
         
            break;
         
         }
         
         
         else
         
         {
         
            last = middle - 1;
         
         }
      
         middle = (first + last)/2;
      
      }
   
   
      if ( first > last )
      
         JOptionPane.showMessageDialog(null,item + " is not found.\n");
   }

/**

The display method displays the contents of the array

*/


   public void displayArray()
   
   {
   
      System.out.println("\nOriginal Order");
   
      System.out.println("****************");
   
   //Displays the original order of contents
   
   
      for (int i = 0; i < numbers.length; i++) 
      {
      
         System.out.println("Position " + (i+1) + " : " + numbers[i]);
      
      }
   
   }

}



the GUI which uses actionListener Method Class Menu.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

public class Menu extends JFrame
{
   private JLabel messageLabel; //Displays a message

   private final int LABEL_WIDTH = 200; //Label's width

   private final int LABEL_HEIGHT = 200; //Label's height

   private JMenuBar menuBar; //The menu bar

   private JMenuItem arrayMenu; //The Array menu

   private JMenuItem helpMenu; //The help menu

   private JMenuItem createItem; //To create

   private JMenuItem exitItem; //To exit

   private JMenuItem sortItem; //To sort

   private JMenuItem searchItem; //To search

   private JMenuItem displayItem; //To display

   private JMenuItem aboutItem; //To explain program

   RandomArray ra = new RandomArray();

    /**
        Constructor
    */

   public Menu()
   
   {
       //set the title
   
      setTitle("Array Menu");
   
      //Specify an action for the close button
   
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
      //Create the messageLabel label.
   
      messageLabel = new JLabel("Click the 'Array' menu to begin", SwingConstants.CENTER);
   
      //Set the labels size
   
      messageLabel.setPreferredSize( new Dimension(LABEL_WIDTH, LABEL_HEIGHT));
   
      //Add the label to content pane
   
      add(messageLabel);
   
      //Build the menu bar.
   
      buildMenuBar();
   
      //Pack and display
   
      pack();
   
      setVisible(true);
   
   }

    /**
      The buildMenuBar builds the menu bar
    */

   public void buildMenuBar()
   
   {
        //Create the menu bar
   
      menuBar = new JMenuBar();
   
        //Create the Array menu and Help menus.
   
      buildArrayMenu();
   
      buildHelpMenu();
   
        //Add the Array and Help menus to the menu bar.
   
      menuBar.add(arrayMenu);
   
      menuBar.add(helpMenu);
   
    //Set the windows menu bar
   
      setJMenuBar(menuBar);
   
   }

   private void buildArrayMenu()
   
   {
   
    //Create a createMenu item. Add action listener
      createItem = new JMenuItem("Create Array");
   
      createItem.setMnemonic(KeyEvent.VK_C);
   
      createItem.addActionListener(new ArrayListener());
   
    //Create a sort item. Add action listener
   
      sortItem = new JMenuItem("Sort");
   
      sortItem.setMnemonic(KeyEvent.VK_S);
   
      sortItem.addActionListener(new ArrayListener());
   
    //Create a searchMenu item. Add action listener
      searchItem = new JMenuItem("Binary Search");
   
      searchItem.setMnemonic(KeyEvent.VK_B)/>;
   
      searchItem.addActionListener(new ArrayListener());
   
    //Create a displayMenu item. Add action listener
   
      displayItem = new JMenuItem("Display content");
   
      displayItem.setMnemonic(KeyEvent.VK_D);
   
      displayItem.addActionListener(new ArrayListener());
   
    //Create a exitMenu item. Add action listener
   
      exitItem = new JMenuItem("Exit");
   
      exitItem.setMnemonic(KeyEvent.VK_X);
   
      exitItem.addActionListener(new ExitListener());
   
    //Create a JMenu object for the Array menu
   
      arrayMenu = new JMenu("Array");
   
      arrayMenu.setMnemonic(KeyEvent.VK_R);
     
   
    //Add the menu items to the Array menu
   
      arrayMenu.add(createItem);
   
      arrayMenu.add(sortItem);
   
      arrayMenu.add(searchItem);
   
      arrayMenu.add(displayItem);
   
      arrayMenu.add(exitItem);
   
   }

     /**
     The buildHelpMenu builds the help menu
     */

   private void buildHelpMenu()
   
   {
   //Create about menu item
   
      aboutItem = new JMenuItem("About");
   
      aboutItem.setMnemonic(KeyEvent.VK_A);
   
      aboutItem.addActionListener(new HelpListener());
   
   //Create a JMenu objector for the Help menu
   
      helpMenu = new JMenu("Help");
   
      helpMenu.setMnemonic(KeyEvent.VK_H);
   
   //Add the about menu item to the Help menu
   
      helpMenu.add(aboutItem);
   
   }

/**
Private inner class that handles the event that is generated when the user selects

Exit from the array menu.
*/

   private class ExitListener implements ActionListener
   
   {
   
      public void actionPerformed(ActionEvent e)
      
      {
      
         System.exit(0);
      
      }
   
   }

/**
Private inner class that handles the event that is generated when the user selects 

a choice from the array menu
*/

   private class ArrayListener implements ActionListener
   
   {
   
      public void actionPerformed(ActionEvent e)
      
      {
      
      
         if (createItem.isSelected())
         
            ra.createArray();
         
         
         else if (sortItem.isSelected()) 
         
            ra.sortArray();
         
         else if (searchItem.isSelected())
         
            ra.binarySearch();
         
         
         else if (displayItem.isSelected())
         
            ra.displayArray();      
      
      }
      
   }
   /**
   Private inner class that handles the event that is generated when the user selects a choice form the help menu.
   */
   
   private class HelpListener implements ActionListener
      
   {
      
      public void actionPerformed(ActionEvent e)
      {
         
         JOptionPane.showMessageDialog(null, "This program will fill an array the size of 100 with random"
               + " numbers 1-100. To begin the process first click the Array menu. It is important that the "
               
               + "steps be executed in the correct order. \n\n1. 'Create Array' is the first step. This will "
               
               + "create and fill the array. \n\n2. 'Sort' is the second step. This will will organize the "
               
               + "elements in the array in ascending order. \n\n3. 'Binary Search' is the next step. This "
               
               + "allows you to enter a number that will then be search for in the array. The numbers "
               
               + "position will then be given if it is found. \n\n4. 'Display Content' is the final step "
               
               + "in this program. This option will display the content in the array.");
         
      }
      
   }
   
   /**
          The main method creates an instance of the Menu classs, which displays its window
   */
   
   
   public static void main(String[] args)
      
   {
      Menu me = new Menu();
      
   }
}


I know that the menu had arrows going to the submenu so I changed that thanks Goose but as for the isSelected I don't know what to do tried looking in my book for a clue but nothing tried googling it nothing so I'm lost the program is still unresponsive as for the JFrame maco it has it I believe.
Was This Post Helpful? 0
  • +
  • -

#23 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12209
  • View blog
  • Posts: 45,287
  • Joined: 27-December 08

Re: trying to figure out how to make the menus work except exit and help

Posted 03 December 2017 - 10:17 PM

If you don't mind my asking, is a GUI part of the assignment, or is this something you are pursuing as an extra? Based on the assignment, I am guessing this is for an Intro to Java class? If so, the expectation is usually that students will use either a Scanner or JOptionPane window to interact with the user, not build a custom GUI. The Scanner and JOptionPane routes will be far easier than building your own GUI.

Quote

I know that the menu had arrows going to the submenu so I changed that thanks Goose but as for the isSelected I don't know what to do tried looking in my book for a clue but nothing tried googling it nothing so I'm lost the program is still unresponsive as for the JFrame maco it has it I believe.


I don't mean to be rude, but your paragraph here is one long run-on sentence. It is hard to parse out what you are saying. Please slow down, use complete (and proper) sentences, and clearly articulate the problems you are encountering. What is the desired behavior? What is the behavior you are encountering? And how does the behavior you are encountering differ from the desired behavior?
Was This Post Helpful? 0
  • +
  • -

#24 Mike1874  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

Re: trying to figure out how to make the menus work except exit and help

Posted 03 December 2017 - 10:26 PM

The GUI is not required I made it cause it was something I felt more comfortable with, and yes its an intro class to java. As for creating another one i have till midnight, but I'm mentally drained as you can see with the long paragraphs.

The issue I'm having is it wont Create, Sort, Display, and Search the arrays I'm trying to create it will generate the menu to select the create , sort , display , and search but when i try to click on them nothing happens
Was This Post Helpful? 0
  • +
  • -

#25 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12209
  • View blog
  • Posts: 45,287
  • Joined: 27-December 08

Re: trying to figure out how to make the menus work except exit and help

Posted 03 December 2017 - 10:30 PM

Quote

As for creating another one i have till midnight


If you are already comfortable with using Scanner, it would honestly be easier to just use this. Your GUI code is long and a little hard to follow. It would take some time to debug it. If the assignment is due at midnight, then I would suggest finishing what is expected, then going back to the GUI when you have free time and no pressure of deadlines.

If you are intent on using your GUI, my immediate suggestion would be to examine e.getSource() in your actionPerformed() method. Perhaps that will tell you more than the isSelected() method.
Was This Post Helpful? 1
  • +
  • -

#26 Mike1874  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

Re: trying to figure out how to make the menus work except exit and help

Posted 03 December 2017 - 10:44 PM

Thank You so much I looked getsource for an example and it executes exactly how the assignment states it Again Thank You very much! anything i can do to help you? like comment something?
Was This Post Helpful? 0
  • +
  • -

#27 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12209
  • View blog
  • Posts: 45,287
  • Joined: 27-December 08

Re: trying to figure out how to make the menus work except exit and help

Posted 03 December 2017 - 10:47 PM

I'm glad I could help and that you got it working! :)
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2