/*
Chapter 8:
Course Substitutions
Programmer:
Brad Shedd
Date:
September 5, 2006
Program Name:
Transfer.java
*/
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();
JPanel thirdRow =
new
JPanel();
JPanel fourthRow =
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(15);
JLabel StudentLabel =
new
JLabel("Student ID:");
JTextField
Student =
new
JTextField(10);
JLabel TransferLabel =
new
JLabel("Transfer Course Number:
");
JTextField
Transfer =
new
JTextField(10);
JLabel CourseLabel =
new
JLabel("Local Course Number:
");
JTextField
Course =
new
JTextField(20);
//Construct
button
JButton
submitButton =
new
JButton("submit");
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(200,200);
f.setVisible(true);
}
public
Transfer()
{
Container c
= getContentPane();
c.setLayout((new
BorderLayout()));
fieldPanel.setLayout(new
GridLayout(8,1));
FlowLayout
rowSetup =
new
FlowLayout(FlowLayout.LEFT,5,3);
firstRow.setLayout(rowSetup);
secondRow.setLayout(rowSetup);
thirdRow.setLayout(rowSetup);
fourthRow.setLayout(rowSetup);
buttonPanel.setLayout(new
FlowLayout(FlowLayout.CENTER));
//Add fields
to rows
firstRow.add(NameLabel);
secondRow.add(StudentLabel);
thirdRow.add(TransferLabel);
fourthRow.add(CourseLabel);
//Add rows
to panel
fieldPanel.add(firstRow);
fieldPanel.add(secondRow);
fieldPanel.add(thirdRow);
fieldPanel.add(fourthRow);
//Add button
to panel
buttonPanel.add(submitButton);
//Add panels
to frame
c.add(fieldPanel, BorderLayout.CENTER);
c.add(buttonPanel,
BorderLayout.SOUTH);
//Add
functionality to buttons
submitButton.addActionListener(this);
//Get the
current date adn open the file
Date today =
new
Date();
SimpleDateFormat myFormat =
new
SimpleDateFormat("MMddyyyy");
String filename =
"payments"
+ myFormat.format(today);
try
{
output =
new
DataOutputStream(new
FileOutputStream(filename));
}
catch(IOException
io)
{
JOptionPane.showMessageDialog(null,
"The program
could not create a storage location.
Please check the disk drive and then run the program again.",
"Error",JOptionPane.INFORMATION_MESSAGE);
System.exit(1);
}
addWindowListener(
new
WindowAdapter()
{
public
void windowlosing(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
(checkFields())
{
try
{
output.writeUTF(Name.getText());
output.writeUTF(Student.getText());
output.writeUTF(Transfer.getText());
output.writeUTF(Course.getText());
JOptionPane.showMessageDialog(null,"The
payment information has been saved.",
"Submission Successful",JOptionPane.INFORMATION_MESSAGE);
}
catch(IOException
c)
{
System.exit(1);
}
clearFields();
}
}
public
boolean checkFields()
{
if
((Name.getText().compareTo("")<1)
||
(Student.getText().compareTo("")<1)
||
(Transfer.getText().compareTo("")<1)
||
(Course.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("");
Student.setText("");
Transfer.setText("");
Course.setText("");
}
}