My first code here is for a class I created.
package PERT_Project;
public class PERT_SETUP
{
String activity;
String description;
int earliestStart;
int duration;
int earliestFinish;
String successor1;
String successor2;
String successor3;
String successor4;
public PERT_SETUP()
{
activity = "";
description = "";
duration = 0;
successor1 = "";
successor2 = "";
successor3 = "";
successor4 = "";
}
public PERT_SETUP(String act, String descrip, int time, String success1, String success2,
String success3, String success4)
{
setActivity(act);
setDescription(descrip);
setDuration(time);
setSuccessor1(success1);
setSuccessor2(success2);
setSuccessor3(success3);
setSuccessor4(success4);
}
public void setActivity(String act)
{
activity = act;
}
public void setDescription(String descrip)
{
description = descrip;
}
public void setDuration(int time)
{
duration = time;
}
public void setSuccessor1(String success1)
{
successor1 = success1;
}
public void setSuccessor2(String success2)
{
successor2 = success2;
}
public void setSuccessor3(String success3)
{
successor3 = success3;
}
public void setSuccessor4(String success4)
{
successor4 = success4;
}
public void setEarliestStart(int es)
{
earliestStart = es;
}
public void setEarliestFinish(int ef)
{
earliestFinish = ef;
}
}
This next code is where I am using this class to create the applet. The error occurs when I press the button and attempt to enter the data I wrote in the applets textfield into the array of this class I created at the top of this code:
It is in the actionPerformed() that the error is occurring. I know it must have something to do with me setting the get values to this class array.
You can ignore the public JPanel createPERT_Table() since I am not having problems with it.(Yet)
package PERT_Project;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
public class PERT_Data extends JApplet implements ActionListener
{
// Initialize Pert_SETUP to store info about each node of the graph.
public PERT_SETUP[] paths = new PERT_SETUP[26];
// Initialize Panels to go through the different screens of the applet.
// 1: PERT_GetData(): Get info about nodes from each user.
// 2: PERT_Table(): Print out JTable for user
// 3: PERT_Graph(): Print out Graph with nodes and lines.
private JPanel dataPanel;
private JPanel finalTablePanel;
private JPanel graphPanel;
// inputPERT_Data
private JLabel activityLabel;
private JTextField activityField;
private JLabel descriptionLabel;
private JTextField descriptionField;
private JLabel durationLabel;
private JTextField durationField;
private JLabel successorsLabel;
private JTextField successorField1;
private JTextField successorField2;
private JTextField successorField3;
private JTextField successorField4;
private JButton enterButton;
private JButton createTableButton;
// createPERT_Table
private JButton createGraphButton;
private JTable PERT_Table;
// Action Performed
int index = 0;
private boolean tableCreated = false;
private boolean criticalPathCreated = false;
// Initialize applet and construct the GUI.
public void init()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
tableCreated = false;
criticalPathCreated = false;
//Creates a box layout for the JPanels.
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
setSize(500, 500);
add(PERT_GetData());
}
});
}
catch(Exception e)
{
System.out.println("Can't create because of " + e);
}
}
// Initial GUI for User input data
public JPanel PERT_GetData()
{
// Set Layout
setLayout(new FlowLayout());
// Create a new JPanel to hold all elements.
dataPanel = new JPanel();
// Set Preferred Size of Panel
dataPanel.setPreferredSize(new Dimension(300, 300));
dataPanel.setOpaque(true);
dataPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
// Create Labels
activityLabel = new JLabel("Enter Activity Character: ");
descriptionLabel = new JLabel("Enter a short Description for this Activity.");
durationLabel = new JLabel("Enter the time required to perform this Activity.");
successorsLabel = new JLabel("Enter all of the successors of this Activity.");
// Create Text Fields.
activityField = new JTextField(5);
descriptionField = new JTextField(20);
durationField = new JTextField(15);
successorField1 = new JTextField(5);
successorField2 = new JTextField(5);
successorField3 = new JTextField(5);
successorField4 = new JTextField(5);
// Create Enter button.
enterButton = new JButton("Enter Activity Data");
// Create Table Button.
createTableButton = new JButton("Create Table");
// Add Action Listener to enter button.
enterButton.addActionListener(this);
// Add Action Listener to create Table Button.
createTableButton.addActionListener(this);
// Add the components to the JPanel.
dataPanel.add(activityLabel);
dataPanel.add(activityField);
dataPanel.add(descriptionLabel);
dataPanel.add(descriptionField);
dataPanel.add(durationLabel);
dataPanel.add(durationField);
dataPanel.add(successorsLabel);
dataPanel.add(successorField1);
dataPanel.add(successorField2);
dataPanel.add(successorField3);
dataPanel.add(successorField4);
dataPanel.add(enterButton);
dataPanel.add(createTableButton);
dataPanel.validate();
// Return panel holding data from user to the applet.
return dataPanel;
}
public JPanel createPERT_Table()
{
createGraphButton = new JButton("Create Graph");
createGraphButton.setMaximumSize(new Dimension(50, 25));
// Press to create graph
createGraphButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(createGraphButton, BorderLayout.CENTER);
JPanel headerArea = new JPanel();
headerArea.setLayout(new BorderLayout());
headerArea.add(PERT_Table.getTableHeader(), BorderLayout.LINE_START);
// Create area that holds entire Table Panel
// Initialize each panels place in this main panel.
JPanel tablePanel = new JPanel();
tablePanel.setLayout(new BorderLayout());
// Header goes at the top of tablePanel(NORTH)
tablePanel.add(headerArea, BorderLayout.PAGE_START);
// The actual Table(PERT_Table) goes to the EAST.
tablePanel.add(PERT_Table, BorderLayout.LINE_START);
// Button goes to the SOUTH.
tablePanel.add(buttonPanel, BorderLayout.PAGE_END);
JPanel finalTablePanel = new JPanel();
finalTablePanel.setLayout(new BorderLayout());
finalTablePanel.add(tablePanel, BorderLayout.LINE_START);
finalTablePanel.validate();
// Put panel holding table onto the applet.
return finalTablePanel;
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Enter Activity Data"))
{
String tempAct = activityField.getText();
System.out.println(tempAct);
// Set Activity
paths[index].setActivity(tempAct);
activityField.setText("");
// Set Description
paths[index].setDescription(descriptionField.getText());
descriptionField.setText("");
// Set Duration
paths[index].setDuration(Integer.parseInt(durationField.getText()));
durationField.setText("");
// Set Successors
paths[index].setSuccessor1(successorField1.getText());
paths[index].setSuccessor2(successorField2.getText());
paths[index].setSuccessor3(successorField3.getText());
paths[index].setSuccessor4(successorField4.getText());
// Clear any info in text Fields
successorField1.setText("");
successorField2.setText("");
successorField3.setText("");
successorField4.setText("");
index = index + 1;
}
else if(ae.getActionCommand().equals("Create Table"))
{
if(index > 0 && !tableCreated)
{
int rows = index + 1;
PERT_Table = new JTable(new PERT_TableModel(rows, paths));
add(createPERT_Table());
tableCreated = true;
remove(dataPanel);
}
}
}
}
Attached Files
Edited by John, 25 March 2009 - 10:26 PM.


Sign In
Create Account


Back to top









