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.