I have the program working but with using ActionListener and ActionPerformed. Instructor wants it changed since it is not the clicking the button that triggers the application but entering in '-1' as a grade. I am lost on trying to change it as nothing seems to work for me so any suggestions would be great.

import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import javax.swing.text.*;
   import java.text.DecimalFormat;

   public class test extends JFrame implements ActionListener
   {
  	 boolean firstRun = true;
     int arrayLength;
     double[] numbers = new double[50];
     JTextPane displayPane = new JTextPane();
     DecimalFormat format = new DecimalFormat("######.##");

     //Begin constructor
     public test()
     {
  	   super("test");
     }

    //Begin container
    public Container createContentPane()
    {
  	  JPanel mainPanel = new JPanel();
      mainPanel.setLayout(new FlowLayout());
      displayPane = addTextToTextPane();
      JScrollPane scrollPane = new JScrollPane(displayPane);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      scrollPane.setWheelScrollingEnabled(true);
      scrollPane.setPreferredSize(new Dimension(190, 300));
      mainPanel.add(scrollPane);
      Container c = getContentPane();
      c.setLayout(new FlowLayout());
      c.add(mainPanel);

      return c;
     }

   //Begin JTextPane TextToTextPane
   public JTextPane addTextToTextPane()
   {
  	 Document doc = displayPane.getDocument();

     try
     {
  	   StyleContext style = StyleContext.getDefaultStyleContext();
       AttributeSet set = style.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.ALIGN_LEFT,style);
       displayPane.setParagraphAttributes(set,false);
       doc.remove(0,doc.getLength());

      for(int i = 0; i < arrayLength; i++)
        {doc.insertString(doc.getLength(), String.valueOf(numbers[i])+ "\n" ,set);}

      if(firstRun)
        System.out.println("Program Started.");

      else
        {double average = average();
        doc.insertString(doc.getLength(),"Average: " + String.valueOf(format.format(average))+ "\n",set);}
        firstRun = false;
      }

      catch(BadLocationException ble)
        {System.err.println("Couldn't insert text.");}

      return displayPane;
   }

  // Begin JMenuBar
  public JMenuBar createMenuBar()
  {
  	// Create an instance of the menu
    JMenuBar mnuBar = new JMenuBar();
    setJMenuBar(mnuBar);

  	//Construct and add the File menu
    JMenu mnuFile = new JMenu("File");
    mnuFile.setMnemonic(KeyEvent.VK_F);
    mnuFile.setDisplayedMnemonicIndex(0);
    mnuBar.add(mnuFile);

  	//Construct and add the menu command to add grades
    JMenuItem mnuFileNewGrades = new JMenuItem("Enter New Grades");
    mnuFileNewGrades.setMnemonic(KeyEvent.VK_W);
    mnuFileNewGrades.setDisplayedMnemonicIndex(0);
    mnuFile.add(mnuFileNewGrades);
    mnuFileNewGrades.setActionCommand("NewGrades");
    mnuFileNewGrades.addActionListener(this);

  	//Construct and add the Exit Command
    JMenuItem mnuFileExit = new JMenuItem("Exit");
    mnuFileExit.setMnemonic(KeyEvent.VK_X);
    mnuFileExit.setDisplayedMnemonicIndex(1);
    mnuFile.add(mnuFileExit);
    mnuFileExit.setActionCommand("Exit");
    mnuFileExit.addActionListener(this);

    return mnuBar;
  }

  // Begin actionPerformed(ActionEvent e)
 public void actionPerformed(ActionEvent e)
 {
 	String arg = e.getActionCommand();

    if(arg == "NewGrades")
    {
 	   for(int i = 0 ; i < numbers.length; i++)
       numbers[i] = 0.0;
       double grade = 0;
       int counter = 0;

      //Continue inputting grades until the user enters the sentinel value -1, or the array is completely filled
      while(grade != -1 && counter < 50)
      {
	      try
	      {
		      grade = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Grade", "Enter Value",JOptionPane.INFORMATION_MESSAGE));numbers[counter] = grade;

              if(grade != -1)
              counter++;
          }

         catch(NumberFormatException nfe) //User enters characters or nothing at all
         {
         System.out.println("Number Format Exception");
         JOptionPane.showMessageDialog(null, "You have entered an invalid number. If you are " + "finished, enter -1.", "Error", JOptionPane.INFORMATION_MESSAGE);
         }

         catch(NullPointerException npe) //User clicks "Cancel" instead of entering -1
         {
 		     System.out.println("Null Pointer Exception");
             JOptionPane.showMessageDialog(null, "If you are finished, enter -1.", "Error",JOptionPane.INFORMATION_MESSAGE);
         }
       }
       arrayLength = counter;
       sort();
       addTextToTextPane();
     }
     if(arg == "Exit")
     {
     	System.out.println("Program Terminated.");
        System.exit(0);
	 }
  }

  //Begin average()
  public double average()
  {
	  double sum = 0 , average;
      int totalPositiveCount = 0;
      for(int i = 0 ; i < arrayLength; i++)
      {
 	     if(numbers[i] > 0)
         {
   	      sum += numbers[i];
          totalPositiveCount++;
         }
      }
      average = sum / (double) totalPositiveCount;

      return average;
   }
  //Sorts the array using a bubble sort method
 public void sort()
 {
 	boolean change;
    do
    {
 	   change = false;

       for (int i = 0 ; i < arrayLength - 1 ; i++)
       {
 	      if(numbers[i] > numbers[i + 1])
          {
    	     swap(i);
             change = true;
          }
       }
     }
     while(change);
  }
  //Swaps two elements in the array
  public void swap(int index)
  {
 	 double temp = numbers[index];
     numbers[index] = numbers[index + 1];
     numbers[index + 1] = temp;
   }

  //Begin main(String[] args)
 public static void main(String[] args)
 {
 	AverageGrade m = new AverageGrade();
    m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    m.setJMenuBar( m.createMenuBar());
    m.setContentPane( m.createContentPane());
    m.setSize(200, 375);
    m.setVisible(true);
    JOptionPane.showMessageDialog(null, "To enter grades, select File and enter grades (0-100).\n" + "To end input, enter -1 in the grade value box.", "Welcome", JOptionPane .INFORMATION_MESSAGE);
  }
}

Recommended Answers

All 3 Replies

Your comparing a double with an int. Not often equal. Write a small test program to see what happens when comparing double with int.
Or add a println() of grade before testing if it == -1
Include the boolean expression: (grade == -1) in the println() to see if its true or false;

That is not the problem I am having, like I stated the program works fine. I just have to remove the ActionListener and not sure what coding I need to use.

remove the ActionListener

Remove the implements ActionListener and the addActionListener() method calls will do it.
I suspect that you meant to ask how to move the logic out of the actionPerformed() method to somewhere else.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.