Pledge Tracker – Java Code

12-1-2013 4-20-49 PM

/**

* This GUI-based program displays a window to enter contact information.
* When to user hits the add button the entered data is added to a table.
* The program creates a file from the entered data.
* Application exits when user clicks the exit button.
*
* @author Arutyun
*/

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
import javax.swing.table.DefaultTableColumnModel.*;
import java.io.*;
import java.util.*;

public class PledgeTracker extends JFrame
{
// Variables declared
private final int WINDOW_WIDTH = 1000; // Window width in pixels
private final int WINDOW_HEIGHT = 400; // Window height in pixels
private JButton submitButton;
private JButton exitButton;
private JTextField fnameTextField;
private JTextField lnameTextField;
private JTextField charityTextField;
private JTextField pledgeTextField;
private JLabel fnameLabel;
private JLabel lnameLabel;
private JLabel charityLabel;
private JLabel pledgeLabel;
private DefaultTableModel model;
private JTable table;
private JScrollPane scroll;

// Constructor
public PledgeTracker (){
super(“PledgeTracker by Team B”); // Window name
setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Set the size of the window.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
setLayout (new BorderLayout());
}

private void buildPanel()
{
// Create the components
fnameLabel = new JLabel (“First Name:”);
lnameLabel = new JLabel (“Last Name:”);
charityLabel = new JLabel (“Charity Name:”);
pledgeLabel = new JLabel (“Pledge Amount:”);
submitButton = new JButton (“Submit”);
exitButton = new JButton (“Exit”);
fnameTextField = new JTextField(10);
lnameTextField = new JTextField(13);
charityTextField = new JTextField(20);
pledgeTextField = new JTextField (5);
model = new DefaultTableModel();
table = new JTable(model);

model.addColumn(“FirstName”);
model.addColumn(“LastName”);
model.addColumn(“Charity”);
model.addColumn(“PledgeAmount”);

scroll = new JScrollPane
(table,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
table.setPreferredScrollableViewportSize
(new Dimension (700,200));
TableColumn column = null;
for (int i = 0; i < 4; i++)
{
column = table.getColumnModel
().getColumn(i);
if (i==0)
{
column.setPreferredWidth
(30);
}if (i==1 || i==4)
{
column.setPreferredWidth
(30);
}else
{
column.setPreferredWidth
(30);
}}

submitButton.addActionListener(new PledgeTracker.AddButtonListener());
exitButton.addActionListener(new PledgeTracker.ExitButtonListener());

// Create the panels to hold the components
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();

panel1.add(fnameLabel);
panel1.add(fnameTextField);
panel1.add(lnameLabel);
panel1.add(lnameTextField);
panel1.add(charityLabel);
panel1.add(charityTextField);
panel1.add(pledgeLabel);
panel1.add(pledgeTextField);
panel3.add(submitButton);
panel3.add(exitButton);
panel2.add(scroll);

add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);

//Display window
setVisible(true);
}
private class AddButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//Calculate pay
FileOutputStream out;
PrintStream P;
String [] data = {fnameTextField.getText(),
lnameTextField.getText(),
charityTextField.getText(),
pledgeTextField.getText()};

try{

if (fnameTextField.getText().isEmpty() || lnameTextField.getText().isEmpty()||
charityTextField.getText().isEmpty()|| pledgeTextField.getText().isEmpty())
throw new IllegalArgumentException();

else model.addRow(new Object[]{fnameTextField.getText(),
lnameTextField.getText(), charityTextField.getText(),pledgeTextField.getText()});
}

catch (IllegalArgumentException empty) {
JOptionPane.showMessageDialog(null, “Some required files are missing”,
“Error”, JOptionPane.ERROR_MESSAGE);}

try{
out = new FileOutputStream(“PledgeReport.txt”);
P = new PrintStream(out);
P.println(data); //Text to write to the file.
P.println(Arrays.toString(data));

System.out.println(“Entered data is written to PledgeReport.txt”); // The Text is written to file.txt.

P.close();

} catch (FileNotFoundException error) {
System.err.println(“Error writing to file”);

}
}
}

private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0); // Close the program
}
}

public static void main (String[] args)
{

PledgeTracker calculator = new PledgeTracker();

}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>