7 Replies - 1381 Views - Last Post: 08 December 2011 - 06:34 PM Rate Topic: -----

#1 meganats  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-December 11

Help with this java gui project please :/

Posted 08 December 2011 - 10:00 AM

Hi i am currently working on this java gui project and can not get fillbuttonlistener to give a message box saying DATA INPUT COMPLETE FOR QUIZ then wait for user to click OK.

I also can not figure out how to get jtable to print my dispbuttonlistener :/

thanks so much in advance

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Scanner;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.util.Random;

public class Final extends JFrame
 
{
    private int nS;
    private int nQ;
    private int[][] data = new int[nS][nQ];
    private int data1[];
    private int data2[];
	 int FIRSTSID = 75678;
	 int longID;
	 long[] sIDs;
	 int[][] sQScores;
 
    public Final() 
    {
 
        // CREATES BUTTONS
        JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 10));
        JButton Help = new JButton("HELP");
        JButton SetP = new JButton("SET PARAMETERS");
        JButton Fill = new JButton("FILL ARRAY");
        JButton Disp = new JButton("DISPLAY RESULTS");
        JButton Quit = new JButton("QUIT");
 
        // CREATES DESCRIPTIONS FOR BUTTONS
        Help.setToolTipText("Displays Instructions on how to use this program.");
        SetP.setToolTipText("Promps user for number of students and quizzes.");
        Fill.setToolTipText("Inserts Values pre-set in SET PAREMETERS.");
        Disp.setToolTipText("Displays the Results");
        Quit.setToolTipText("Close Program");
 
        // ADDS BUTTONS TO JFRAME
        p1.add(Help);
        p1.add(SetP);
        p1.add(Fill);
        p1.add(Disp);
        p1.add(Quit);
        p1.setBorder(new TitledBorder("Grade Compiling Program"));
 
        // ADJUSTS THE FONT AND BORDER OPTIONS FOR JFRAME
        Font largeFont = new Font("TimesRoman", Font.BOLD, 20);
        Border lineBorder = new LineBorder(Color.BLACK, 2);
 
        // ADDS ALL OF THE ABOVE INTO GUI FIELD
        add(p1);
 
        // REGISTERS EVENT LISTENERS WITH ALL 5 BUTTONS
        Help.addActionListener(new HelpButtonListener());
        SetP.addActionListener(new SetPButtonListener());
        Fill.addActionListener(new FillButtonListener());
        Disp.addActionListener(new DispButtonListener());
        Quit.addActionListener(new QuitButtonListener());
    }
    public static void main(String[] args) 
    {
        // CREATES JFRAME AND PAREMETERS FOR JFRAME
        JFrame frame = new Final();
        frame.setTitle("College Final Project");
        frame.setSize(750, 500);
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    // PROGRAMMING FOR WHEN THE "HELP BUTTON" IS PRESSED
    private class HelpButtonListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e)
        {
 
            System.out.println();
            String output = "\n______________________HELP MENU__________________________"
                    + // /This is the output that will be diplayed when the HELP
                        // button is pressed
                    "\n Welcome to the Help Menu, here you will find tips to using this program."
                    + "\n"
                    + "\n The first step is to click the 'SET PARAMETERS' button. Here you will   "
                    + "\n input the number of students (up to 50) and number of quizzes for each  "
                    + "\n student (up to 5.) "
                    + "\n "
                    + "\n Next you will select the 'FILL ARRAY' button. This button will open up"
                    + "\n the menu to select the ID numbers and quiz grades for each student and "
                    + "\n quiz, then store them within the paremeters set in 'SET PARAMETERS'."
                    + "\n "
                    + "\n Finally you will click the 'DISPLAY' button. This button will compile all"
                    + "\n the data you entered in the last two sections and display the results."
                    + "\n This program computes and displays the lowest, highest, average, and"
                    + "\n medium for each grade of each set of quizzes."
                    + "\n "
                    + "\n As an added note if you wish to quit this program at any time, simply"
                    + "\n press the 'QUIT' button and the program will close";
 
            JOptionPane.showMessageDialog(null, output); // Dispays the above
                                                            // information for
                                                            // the user in a
                                                            // seperate window
                                                            // that closes when
                                                            // OK is pressed.
        }
    }
    // PROGRAMMING FOR WHEN THE "SET PARAMETERS BUTTON" IS PRESSED
    private class SetPButtonListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
            String studentString = JOptionPane
                    .showInputDialog("Enter number of students (up to 50)");
            if (studentString != null) 
            {
                nS = Integer.parseInt(studentString);
					 //nS = numberOfStudents;
            }
 
            String numString = JOptionPane
                    .showInputDialog("Enter number of quizzes for each student (up to 5)");
            if (numString != null) 
            {
                nQ = Integer.parseInt(numString);
					 //nQ = quizesPerStudent;
            }
        }
    }
    // PROGRAMMING FOR WHEN THE "FILL ARRAY BUTTON" IS PRESSED
    private class FillButtonListener implements ActionListener 
	 {
      public void actionPerformed(ActionEvent e) 
		{
			int i;
			int j;
			int FIRSTSID = 75678;

			for (i = 0; i < nS; i++) 
			{
				(sIDs)[i] = FIRSTSID + i;
				for (j = 0; j < nQ; j++)
				(sQScores)[i][j] = (int) (((float) rand()) / 324.5);
			}

		}

		private float rand() 
		{
			Random rand = new Random();
			int number = 0;
			{
				for (int counter = +1; counter <= 1; counter++) 
				{
				number = rand.nextInt(100);
				System.out.println(number);
				String output = "\n DATA INPUT COMPLETE FOR QUIZ ";
                JOptionPane.showMessageDialog(null, output);
				}
			}
			return number;
			
		}
	}
			
    // PROGRAMMING FOR WHEN THE "DISPLAY BUTTON" IS PRESSED
    private class DispButtonListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
            System.out.println();
            String output = "\n Students and Grades\n "
                    + data[nS][nQ];
            JOptionPane.showMessageDialog(null, output);
        }
    }
    // PROGRAMMING FOR WHEN THE "QUIT BUTTON" IS PRESSED
    private class QuitButtonListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
            System.out.println();
            String output = "\n Bye";
            JOptionPane.showMessageDialog(null, output);
            System.exit(0);
        }
    }
}


is there something wrong with this line?
(sIDs)[i] = FIRSTSID + i;

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Final$FillButtonListener.actionPerformed(Final.java:141)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.window.dispatchEventImpl(window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Is This A Good Question/Topic? 0
  • +

Replies To: Help with this java gui project please :/

#2 meganats  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-December 11

Re: Help with this java gui project please :/

Posted 08 December 2011 - 10:19 AM

what i am trying to accomplish is: FillButtonListener class must be invoked after SetButtonListener. the program will check for this and if the option is exercised before SetButtonListener, the program must provide informational warning to the user, wait for the user to read the message, and the allow the user to click OK, where the program goes back to the gui menu. once option SetButtonListener is exercised, the program will fill the array with the following constraints: Student ID is a five digit integer value starting at 75678 and increments by one for each student in the class, and the scores in each quiz is an integer number, randomly generated by the program in ranges 0 to 100. once the task is completed, the program will issue the message "DATA INPUT COMPLETE FOR QUIZ.." and allows the user to read the message and click OK

trying to be as descriptive as possible :/ any help would be great. Thanks!
Was This Post Helpful? 0
  • +
  • -

#3 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: Help with this java gui project please :/

Posted 08 December 2011 - 12:42 PM

From what I know, you can't surround an array name with parenthesis. It should look like this:
arrayExample[j][k] = something

Was This Post Helpful? 0
  • +
  • -

#4 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Help with this java gui project please :/

Posted 08 December 2011 - 12:59 PM

Actually You CAN do this (arrayName)[index]. It seems unusual and rather pointless, though. As for your error, you don't initialize sIDs anywhere that I can see.
Was This Post Helpful? 0
  • +
  • -

#5 meganats  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-December 11

Re: Help with this java gui project please :/

Posted 08 December 2011 - 01:04 PM

View PostSheph, on 08 December 2011 - 01:59 PM, said:

As for your error, you don't initialize sIDs anywhere that I can see.


hmm im confused now is it not initialized when: (sIDs)[i] = FIRSTSID + i; ?
Was This Post Helpful? 0
  • +
  • -

#6 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Help with this java gui project please :/

Posted 08 December 2011 - 01:26 PM

The array is not initialized. You need to tell it how much memory to reserve. sIDs = new int[20]. Reserve enough bytes for 20 integers.

Woops, you had longs, so sIDs = new long[20];

This post has been edited by Sheph: 08 December 2011 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10809
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Help with this java gui project please :/

Posted 08 December 2011 - 06:21 PM

Let's take a step back for a minute. It looks like you are using parallel arrays to manage the students and their grades. Design a Student class and manage a Student[] rather than using parallel arrays.

Now when dealing with JTables, you want to focus on a TableModel to organize the data and tell the JTable what goes in each field. The TableModel should be responsible for managing the Student[], and telling the JTable which cell each value goes in. I have a blog entry that illustrates more on TableModels.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Help with this java gui project please :/

Posted 08 December 2011 - 06:34 PM

View PostThe_Programmer-, on 08 December 2011 - 03:42 PM, said:

From what I know, you can't surround an array name with parenthesis. It should look like this:
arrayExample[j][k] = something

ackward and useless syntax but (array)[index] = ... is perfectly legal
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1