I must write a interface to enter course information for transfer students. The created data file should be sequential in nature, with the student's name, his/her ID number, the course number from the previous college, and the accepted course number at the current college. The code I wrote so far is as follows:

/*
	Chapter 8:	Programming Assignment 8
	Programmer:	T. du Preez
	Date:		September 20, 2009
	Filename:	Transfer.java
	Purpose:	To enter course information for transfer students.
*/

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;

public class Transfer extends JFrame implements ActionListener
{
	//Declare output stream
	DataOutputStream output;

	//Construct a panel for each row
	JPanel firstRow = new JPanel();
	JPanel secondRow = new JPanel();

	//Construct a panel for the fields and buttons
	JPanel fieldPanel = new JPanel();
	JPanel buttonPanel = new JPanel();

	//Construct labels and text boxes
	JLabel nameLabel = new JLabel("Name:");
		JTextField name = new JTextField(25);
	JLabel studentIDLabel = new JLabel("\nStudent ID:");
		JTextField studentID = new JTextField(25);
	JLabel transfercourseNumberLabel = new JLabel("\nTransfer Course Number:");
		JTextField transfercourseNumber = new JTextField(25);
	JLabel localcourseNumberLabel = new JLabel("\nLocal Course Number:");
		JTextField localcourseNumber = new JTextField(25);

	//Construct button
	JButton submitButton = new JButton("Submit");
	JButton exitButton = new JButton("Exit");

	public static void main(String[] args)
	{
		//set the look and feel of the interface
		try
		{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null,"The UIManager could not set the Look and Feel for this application.","Error",JOptionPane.INFORMATION_MESSAGE);
		}

		Transfer f = new Transfer();
	    f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE );
		f.setSize(450,300);
	    f.setTitle("Transfer Course Substitutions");
	    f.setResizable(false);
	    f.setLocation(300,200);
	    f.setVisible(true);
		}

	public Transfer()
	{
		Container c = getContentPane();
		c.setLayout((new BorderLayout()));
		fieldPanel.setLayout(new GridLayout(4,2));
		FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,4,2);
			firstRow.setLayout(rowSetup);
			secondRow.setLayout(rowSetup);
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

		//Add fields to screen
		firstRow.add(nameLabel);
		firstRow.add(studentIDLabel);
		firstRow.add(transfercourseNumberLabel);
		firstRow.add(localcourseNumberLabel);

		//Add rows to panel
		fieldPanel.add(firstRow);
		fieldPanel.add(secondRow);

		//Add button to panel
		buttonPanel.add(submitButton);
		buttonPanel.add(exitButton);

		//Add panels to frame
		c.add(fieldPanel, BorderLayout.CENTER);
		c.add(buttonPanel, BorderLayout.SOUTH);

		//Add functionality to buttons
		submitButton.addActionListener(this);
		exitButton.addActionListener(this);

		try
		{
			output = new DataOutputStream(new FileOutputStream("Transfer.dat"));
		}

		catch(IOException io)
		{
			System.exit(1);
		}

		addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit and submit the file?", "File Submission", JOptionPane.YES_NO_OPTION);
					if (answer == JOptionPane.YES_OPTION);
					System.exit(0);
				}
			}
		);

	}

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

		if (arg == "Submit")
		{
			try
			{
				output.writeUTF(name.getText());
				output.writeUTF(studentID.getText());
				output.writeUTF(transfercourseNumber.getText());
				output.writeUTF(localcourseNumber.getText());
			}
			catch(IOException c)
			{
				System.exit(1);
			}
			clearFields();
		}

		else  //code to execute if the user clicks Exit
		{
			try
			{
				output.close();
			}
			catch(IOException c)
			{
				System.exit(0);
			}
		}
	}

	public boolean checkFields()
	{
      	if ((name.getText().compareTo("")<1) 			    	||
            (studentID.getText().compareTo("")<1)	      		||
            (transfercourseNumber.getText().compareTo("")<1) 	||
            (localcourseNumber.getText().compareTo("")<1))
      	{
			JOptionPane.showMessageDialog(null,"You must complete all fields.","Data Entry Error",JOptionPane.WARNING_MESSAGE);
			return false;
      	}
      	else
      	{
		  	return true;
	  	}
  	}

	public void clearFields()
	{
		//Clear fields and reset the focus
		name.setText("");
		studentID.setText("");
		transfercourseNumber.setText("");
		localcourseNumber.setText("");
	}
}

I am battleling to insert spaces where the people can insert information and also for the last line "Local Course Number:" to show on the application.

Please tell me what I am doing wrong.

Recommended Answers

All 12 Replies

I can't see where you add your fields to secondRow.
One small point: when you catch an IOException you just exit. This will leave you with no information about what went wrong or where. You should do a printStackTrace for the exception before exiting so you know what happened.

I included the secondRow information, however it's still not working.

/*
    Chapter 8:  Programming Assignment 8
    Programmer: T. du Preez
    Date:       September 20, 2009
    Filename:                   Transfer.java
    Purpose:                    To enter course information for transfer students.
*/

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;

public class Transfer extends JFrame implements ActionListener
{
    //Declare output stream
    DataOutputStream output;

    //Construct a panel for each row
    JPanel firstRow = new JPanel();
    JPanel secondRow = new JPanel();

    //Construct a panel for the fields and buttons
    JPanel fieldPanel = new JPanel();
    JPanel buttonPanel = new JPanel();

    //Construct labels and text boxes
    JLabel nameLabel = new JLabel("Name:");
        JTextField name = new JTextField(25);
    JLabel studentIDLabel = new JLabel("Student ID:");
        JTextField studentID = new JTextField(25);
    JLabel transfercourseNumberLabel = new JLabel("Transfer Course Number:");
        JTextField transfercourseNumber = new JTextField(25);
    JLabel localcourseNumberLabel = new JLabel("Local Course Number:");
        JTextField localcourseNumber = new JTextField(25);

    //Construct button
    JButton submitButton = new JButton("Submit");
    JButton exitButton = new JButton("Exit");

    public static void main(String[] args)
    {
        //set the look and feel of the interface
        try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null,"The UIManager could not set the Look and Feel for this application.","Error",JOptionPane.INFORMATION_MESSAGE);
        }

        Transfer f = new Transfer();
        f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE );
        f.setSize(450,300);
        f.setTitle("Transfer Course Substitutions");
        f.setResizable(false);
        f.setLocation(300,200);
        f.setVisible(true);
        }

    public Transfer()
    {
        Container c = getContentPane();
        c.setLayout((new BorderLayout()));
        fieldPanel.setLayout(new GridLayout(4,2));
        FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,4,2);
            firstRow.setLayout(rowSetup);
            secondRow.setLayout(rowSetup);
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

        //Add fields to screen
        firstRow.add(nameLabel);
        secondRow.add(studentIDLabel);
        secondRow.add(transfercourseNumberLabel);
        secondRow.add(localcourseNumberLabel);

        //Add rows to panel
        fieldPanel.add(firstRow);
        fieldPanel.add(secondRow);

        //Add button to panel
        buttonPanel.add(submitButton);
        buttonPanel.add(exitButton);

        //Add panels to frame
        c.add(fieldPanel, BorderLayout.CENTER);
        c.add(buttonPanel, BorderLayout.SOUTH);

        //Add functionality to buttons
        submitButton.addActionListener(this);
        exitButton.addActionListener(this);

        try
        {
            output = new DataOutputStream(new FileOutputStream("Transfer.dat"));
        }

        catch(IOException io)
        {
            System.exit(1);
        }

        addWindowListener(
            new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit and submit the file?", "File Submission", JOptionPane.YES_NO_OPTION);
                    if (answer == JOptionPane.YES_OPTION);
                    System.exit(0);
                }
            }
        );

    }

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

        if (arg == "Submit")
        {
            try
            {
                output.writeUTF(name.getText());
                output.writeUTF(studentID.getText());
                output.writeUTF(transfercourseNumber.getText());
                output.writeUTF(localcourseNumber.getText());
            }
            catch(IOException c)
            {
                System.exit(1);
            }
            clearFields();
        }

        else  //code to execute if the user clicks Exit
        {
            try
            {
                output.close();
            }
            catch(IOException c)
            {
                System.exit(0);
            }
        }
    }

    public boolean checkFields()
    {
        if ((name.getText().compareTo("")<1)                     ||
            (studentID.getText().compareTo("")<1)                ||
            (transfercourseNumber.getText().compareTo("")<1)     ||
            (localcourseNumber.getText().compareTo("")<1))
        {
            JOptionPane.showMessageDialog(null,"You must complete all fields.","Data Entry Error",JOptionPane.WARNING_MESSAGE);
            return false;
        }
        else
        {
            return true;
        }
    }

    public void clearFields()
    {
        //Clear fields and reset the focus
        name.setText("");
        studentID.setText("");
        transfercourseNumber.setText("");
        localcourseNumber.setText("");
    }
}

Please help. I need to send my assignment in and I am really battleling. My assignment needs to be in by Monday..

1. "still not working" tells us nothing. You MUST explain exactly what errors you get, or in what way the result differs from the expected result.
2. Post your code with tags so we can read it.[code=java] tags so we can read it.

I am not receiving any errors when compiling, however when I "Run Java Application" it runs but its not showing the way it should. The Name, Student ID, Transfer Course Number and Local Course Number must show under each other and there must be spaces for the people to insert text. I tried everything I know but I cannot see where I made a mistake. Please help.....

As JamesCherrill said, we will not read your code the way you posted it. Use code tags. When you create a new post click the code button and put your code between the tags

... and don't forget to change the first tag to [CODE=JAVA]

Here is the code that I am battleling with. Please help me...

/*
    Chapter 8:  Programming Assignment 8
    Programmer: T. du Preez
    Date:       September 20, 2009
    Filename:   Transfer.java
    Purpose:    To enter course information for transfer students.
*/

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;

public class Transfer extends JFrame implements ActionListener
{
    //Declare output stream
    DataOutputStream output;

    //Construct a panel for each row
    JPanel firstRow = new JPanel();
    JPanel secondRow = new JPanel();

    //Construct a panel for the fields and buttons
    JPanel fieldPanel = new JPanel();
    JPanel buttonPanel = new JPanel();

    //Construct labels and text boxes
    JLabel nameLabel = new JLabel("Name:                                          ");
        JTextField name = new JTextField(35);
    JLabel studentIDLabel = new JLabel("Student ID:                                                    ");
        JTextField studentID = new JTextField(40);
    JLabel transfercourseNumberLabel = new JLabel("Transfer Course Number:                      ");
        JTextField transfercourseNumber = new JTextField(40);
    JLabel localcourseNumberLabel = new JLabel("Local Course Number:                             ");
        JTextField localcourseNumber = new JTextField(45);

    //Construct button
    JButton submitButton = new JButton("Submit");
    JButton exitButton = new JButton("Exit");

    public static void main(String[] args)
    {
        //set the look and feel of the interface
        try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null,"The UIManager could not set the Look and Feel for this application.","Error",JOptionPane.INFORMATION_MESSAGE);
        }

        Transfer f = new Transfer();
        f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE );
        f.setSize(450,300);
        f.setTitle("Transfer Course Substitutions");
        f.setResizable(false);
        f.setLocation(300,200);
        f.setVisible(true);
        }

    public Transfer()
    {
        Container c = getContentPane();
        c.setLayout((new BorderLayout()));
        fieldPanel.setLayout(new GridLayout(4,2));
        FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,4,2);
            firstRow.setLayout(rowSetup);
            secondRow.setLayout(rowSetup);
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

        //Add fields to rows
        firstRow.add(nameLabel);
        secondRow.add(studentIDLabel);
        secondRow.add(transfercourseNumberLabel);
        secondRow.add(localcourseNumberLabel);

        //Add rows to panel
        fieldPanel.add(firstRow);
        fieldPanel.add(secondRow);

        //Add button to panel
        buttonPanel.add(submitButton);
        buttonPanel.add(exitButton);

        //Add panels to frame
        c.add(fieldPanel, BorderLayout.CENTER);
        c.add(buttonPanel, BorderLayout.SOUTH);

        //Add functionality to buttons
        submitButton.addActionListener(this);
        exitButton.addActionListener(this);

        try
        {
            output = new DataOutputStream(new FileOutputStream("Transfer.dat"));
        }

        catch(IOException io)
        {
            System.exit(1);
        }

        addWindowListener(
            new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit and submit the file?", "File Submission", JOptionPane.YES_NO_OPTION);
                    if (answer == JOptionPane.YES_OPTION);
                    System.exit(0);
                }
            }
        );

    }

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

        if (arg == "Submit")
        {
            try
            {
                output.writeUTF(name.getText());
                output.writeUTF(studentID.getText());
                output.writeUTF(transfercourseNumber.getText());
                output.writeUTF(localcourseNumber.getText());

                JOptionPane.showMessageDialog(null, "The information has been saved.", "Submission Successful", JOptionPane.INFORMATION_MESSAGE);
            }
            catch(IOException c)
            {
                System.exit(1);
            }
            clearFields();
        }

        else  //code to execute if the user clicks Exit
        {
            try
            {
                output.close();
            }
            catch(IOException c)
            {
                System.exit(0);
            }
        }
    }

    public boolean checkFields()
    {
        if ((name.getText().compareTo("")<1)                     ||
            (studentID.getText().compareTo("")<1)                ||
            (transfercourseNumber.getText().compareTo("")<1)     ||
            (localcourseNumber.getText().compareTo("")<1))
        {
            JOptionPane.showMessageDialog(null,"You must complete all fields.","Data Entry Error",JOptionPane.WARNING_MESSAGE);
            return false;
        }
        else
        {
            return true;
        }
    }

    public void clearFields()
    {
        //Clear fields and reset the focus
        name.setText("");
        studentID.setText("");
        transfercourseNumber.setText("");
        localcourseNumber.setText("");
    }
}

Please quickly edit your post and use code tags to show code properly

/*
	Chapter 8:	Programming Assignment 8
	Programmer:	T. du Preez
	Date:		September 20, 2009
	Filename:	Transfer.java
	Purpose:	To enter course information for transfer students.
*/

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;

public class Transfer extends JFrame implements ActionListener
{
	//Declare output stream
	DataOutputStream output;

	//Construct a panel for each row
	JPanel firstRow = new JPanel();
	JPanel secondRow = new JPanel();

	//Construct a panel for the fields and buttons
	JPanel fieldPanel = new JPanel();
	JPanel buttonPanel = new JPanel();

	//Construct labels and text boxes
	JLabel nameLabel = new JLabel("Name:                                          ");
		JTextField name = new JTextField(35);
	JLabel studentIDLabel = new JLabel("Student ID:                                                    ");
		JTextField studentID = new JTextField(40);
	JLabel transfercourseNumberLabel = new JLabel("Transfer Course Number:                      ");
		JTextField transfercourseNumber = new JTextField(40);
	JLabel localcourseNumberLabel = new JLabel("Local Course Number:                             ");
		JTextField localcourseNumber = new JTextField(45);

	//Construct button
	JButton submitButton = new JButton("Submit");
	JButton exitButton = new JButton("Exit");

	public static void main(String[] args)
	{
		//set the look and feel of the interface
		try
		{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null,"The UIManager could not set the Look and Feel for this application.","Error",JOptionPane.INFORMATION_MESSAGE);
		}

		Transfer f = new Transfer();
	    f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE );
		f.setSize(450,300);
	    f.setTitle("Transfer Course Substitutions");
	    f.setResizable(false);
	    f.setLocation(300,200);
	    f.setVisible(true);
		}

	public Transfer()
	{
		Container c = getContentPane();
		c.setLayout((new BorderLayout()));
		fieldPanel.setLayout(new GridLayout(4,2));
		FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,4,2);
			firstRow.setLayout(rowSetup);
			secondRow.setLayout(rowSetup);
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

		//Add fields to rows
		firstRow.add(nameLabel);
		secondRow.add(studentIDLabel);
		secondRow.add(transfercourseNumberLabel);
		secondRow.add(localcourseNumberLabel);

		//Add rows to panel
		fieldPanel.add(firstRow);
		fieldPanel.add(secondRow);

		//Add button to panel
		buttonPanel.add(submitButton);
		buttonPanel.add(exitButton);

		//Add panels to frame
		c.add(fieldPanel, BorderLayout.CENTER);
		c.add(buttonPanel, BorderLayout.SOUTH);

		//Add functionality to buttons
		submitButton.addActionListener(this);
		exitButton.addActionListener(this);

		try
		{
			output = new DataOutputStream(new FileOutputStream("Transfer.dat"));
		}

		catch(IOException io)
		{
			System.exit(1);
		}

		addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit and submit the file?", "File Submission", JOptionPane.YES_NO_OPTION);
					if (answer == JOptionPane.YES_OPTION);
					System.exit(0);
				}
			}
		);

	}

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

		if (arg == "Submit")
		{
			try
			{
				output.writeUTF(name.getText());
				output.writeUTF(studentID.getText());
				output.writeUTF(transfercourseNumber.getText());
				output.writeUTF(localcourseNumber.getText());

				JOptionPane.showMessageDialog(null, "The information has been saved.", "Submission Successful", JOptionPane.INFORMATION_MESSAGE);
			}
			catch(IOException c)
			{
				System.exit(1);
			}
			clearFields();
		}

		else  //code to execute if the user clicks Exit
		{
			try
			{
				output.close();
			}
			catch(IOException c)
			{
				System.exit(0);
			}
		}
	}

	public boolean checkFields()
	{
      	if ((name.getText().compareTo("")<1) 			    	||
            (studentID.getText().compareTo("")<1)	      		||
            (transfercourseNumber.getText().compareTo("")<1) 	||
            (localcourseNumber.getText().compareTo("")<1))
      	{
			JOptionPane.showMessageDialog(null,"You must complete all fields.","Data Entry Error",JOptionPane.WARNING_MESSAGE);
			return false;
      	}
      	else
      	{
		  	return true;
	  	}
  	}

	public void clearFields()
	{
		//Clear fields and reset the focus
		name.setText("");
		studentID.setText("");
		transfercourseNumber.setText("");
		localcourseNumber.setText("");
	}
}

Once again:
Your code is huge, we are not your teacher to read the whole thing.
1) What errors do you get?
2) At which line?
3) What does it do, and what it is supposed to do?

As mentioned previously. The program run successfully, however it is not showing as it's suppose to show. The Name, Student ID, Transfer Course Number and Local Course Number must show under each other and there must be spaces for the people to insert text. I tried everything I know but I cannot see where I made a mistake. Please help.....

After running the code, I noticed that you declared the JTextFileds but you didn't add them to any JPanel

//Add fields to rows
		firstRow.add(nameLabel);
		secondRow.add(studentIDLabel);
		secondRow.add(transfercourseNumberLabel);
		secondRow.add(localcourseNumberLabel);

		//Add rows to panel
		fieldPanel.add(firstRow);
		fieldPanel.add(secondRow);

You add the labels but I didn't see any code the does the same for:

JTextField name = new JTextField(35);
	JTextField studentID = new JTextField(40);
		JTextField transfercourseNumber = new JTextField(40);
		JTextField localcourseNumber = new JTextField(45);
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.