mainGUI:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class GUImain extends JFrame implements ActionListener{
private final int WIDTH = 550;
private final int HEIGHT = 300;
private Student student;
private JMenuBar menuBar;
private JMenu fileMenu, editMenu, helpMenu;
private JMenuItem newItem, openItem, saveItem, exitItem;
private JPanel panel, panel2;
private JLabel fName1, lName1, id1, major1;
protected JTextField fName2, lName2, id2, major2;
private JButton previous, next, add, delete, exit;
private AddNewWindow addWindow;
private int position = 0;
private Student startup;
private static ArrayList<Student> students;
private static ReadFile rFile;
//GUI constructor
public GUImain() {
//sets parameters for the window
setTitle("Final Project");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setLayout(new BorderLayout());
setResizable(false);
//adds an exit button to a panel
exit = new JButton("Exit");
panel = new JPanel();
panel.add(exit);
//adds the exit button panel and sets it to the center
add(panel, BorderLayout.CENTER);
//calls the buildPanel method
buildPanel();
//sets the text fields text to the first student
fName2.setText(startup.getFirstName());
lName2.setText(startup.getLastName());
id2.setText(startup.getID());
major2.setText(startup.getMajor());
//sets the window to visible
setVisible(true);
}
//buildPanel method
public void buildPanel() {
//instantiates startup with the first student
startup = new Student(students.get(0));
//instantiates panel2 and sets the layout to a gridlayout
panel2 = new JPanel();
panel2.setLayout(new GridLayout(7, 2));
//instantiates all the buttons, labels, and textfields in the window
fName1 = new JLabel("First Name");
fName2 = new JTextField();
lName1 = new JLabel("Last Name");
lName2 = new JTextField();
id1 = new JLabel("Student ID");
id2 = new JTextField();
major1 = new JLabel("Major");
major2 = new JTextField();
previous = new JButton("Previous");
next = new JButton("Next");
add = new JButton("Add New");
delete = new JButton("Delete");
//instantiates a menubar
menuBar = new JMenuBar();
//instantiates all menus and menuitems
fileMenu = new JMenu("File");
editMenu = new JMenu("Edit");
helpMenu = new JMenu("Help");
newItem = new JMenuItem("New");
openItem = new JMenuItem("Open");
saveItem = new JMenuItem("Save");
exitItem = new JMenuItem("Close");
//adds the newitem, openitem, separator and exititem to the filemenu
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
//adds menus to the filebar
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
//adds the menubar to the window
setJMenuBar(menuBar);
//makes the display text areas not editable
fName2.setEditable(false);
lName2.setEditable(false);
id2.setEditable(false);
major2.setEditable(false);
previous.setEnabled(false);
//adds all the components to panel2
panel2.add(fName1);
panel2.add(fName2);
panel2.add(lName1);
panel2.add(lName2);
panel2.add(id1);
panel2.add(id2);
panel2.add(major1);
panel2.add(major2);
panel2.add(previous);
panel2.add(next);
panel2.add(add);
panel2.add(delete);
//adds action listeners to the buttons
add.addActionListener(this);
exit.addActionListener(this);
next.addActionListener(this);
previous.addActionListener(this);
delete.addActionListener(this);
//adds the panel to the content pane and sets it to north
add(panel2, BorderLayout.NORTH);
}
//actionlistener
public void actionPerformed(ActionEvent e) {
//action listener for the add button
if(e.getSource() == add) {
Student temp;
//creates a new add window object
addWindow = new AddNewWindow(students);
System.out.println(students);
System.out.println(students.size());
if(students.size() == 0) {
System.out.println(students);
}
}
if(e.getSource() == previous) {
Student temp;
if(position > 0) {
try {
if(position == 1) {
previous.setEnabled(false);
}
next.setEnabled(true);
position--;
temp = students.get(position);
fName2.setText(temp.getFirstName());
lName2.setText(temp.getLastName());
id2.setText(temp.getID());
major2.setText(temp.getMajor());
}
catch(IndexOutOfBoundsException ex) {
}
}
else {
previous.setEnabled(false);
}
}
if(e.getSource() == next) {
Student temp;
if(position < students.size()-1) {
try {
if(position == students.size()-2) {
next.setEnabled(false);
}
previous.setEnabled(true);
position++;
temp = students.get(position);
fName2.setText(temp.getFirstName());
lName2.setText(temp.getLastName());
id2.setText(temp.getID());
major2.setText(temp.getMajor());
}
catch(IndexOutOfBoundsException ex) {
}
}
else {
next.setEnabled(false);
}
}
//deletes a file from the arraylist
if(e.getSource() == delete) {
Student temp;
try {
temp = students.get(0);
}
catch(IndexOutOfBoundsException ex) {
JOptionPane.showMessageDialog(null, "No records exist to delete");
}
try {
temp = students.get(position);
if(students.size() == 2) {
previous.setEnabled(false);
next.setEnabled(false);
}
if(students.equals(temp)) {
}
students.remove(position);
temp = students.get(position);
fName2.setText(temp.getFirstName());
lName2.setText(temp.getLastName());
id2.setText(temp.getID());
major2.setText(temp.getMajor());
}
catch(IndexOutOfBoundsException ex) {
if(position > 0) {
position--;
temp = students.get(position);
fName2.setText(temp.getFirstName());
lName2.setText(temp.getLastName());
id2.setText(temp.getID());
major2.setText(temp.getMajor());
}
else if(position == 0) {
fName2.setText("");
lName2.setText("");
id2.setText("");
major2.setText("");
}
else {
JOptionPane.showMessageDialog(null, "No more records to delete");
}
}
}
if(e.getSource() == exit) {
System.exit(0);
}
}
public static void main(String[] args) {
//sets up the arraylist, reads the students_raw file and writes it to the arraylist
students = new ArrayList<Student>();
rFile = new ReadFile("students_raw.csv");
students = rFile.readTextFile();
GUImain window = new GUImain();
}
}
addNewWindow:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class AddNewWindow extends JDialog implements ActionListener{
private JLabel fName1, lName1, id1, major1;
private JTextField afName2, alName2, aid2, amajor2;
private JPanel panel1, panel2, panel3, panel4, buttonPanel;
private JButton enter, cancel;
private ArrayList<Student> list;
private Student temp;
public AddNewWindow(ArrayList<Student> s) {
//performs a shallow copy
list = s;
//sets default settings
setTitle("Add new Student");
setSize(500, 400);
setLayout(new GridLayout(5, 2));
setResizable(false);
temp = new Student();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
buttonPanel = new JPanel();
fName1 = new JLabel("First Name");
afName2 = new JTextField(10);
lName1 = new JLabel("Last Name");
alName2 = new JTextField(10);
id1 = new JLabel("Student ID");
aid2 = new JTextField(10);
major1 = new JLabel("Major");
amajor2 = new JTextField(10);
enter = new JButton("Enter");
cancel = new JButton("Cancel");
//places components on panels
panel1.setLayout(new GridLayout(1, 2));
panel2.setLayout(new GridLayout(1, 2));
panel3.setLayout(new GridLayout(1, 2));
panel4.setLayout(new GridLayout(1, 2));
buttonPanel.setLayout(new GridLayout(1, 2));
//adds the action listener to buttons
enter.addActionListener(this);
cancel.addActionListener(this);
//adds components to panels
panel1.add(fName1);
panel1.add(afName2);
panel2.add(lName1);
panel2.add(alName2);
panel3.add(id1);
panel3.add(aid2);
panel4.add(major1);
panel4.add(amajor2);
buttonPanel.add(enter);
buttonPanel.add(cancel);
//adds panels to the content pane
add(panel1);
add(panel2);
add(panel3);
add(panel4);
add(buttonPanel);
pack();
//sets visible
setVisible(true);
}
public String getFName() {
return temp.getFirstName();
}
public String getLName() {
return temp.getLastName();
}
public String getID() {
return temp.getID();
}
public String getMajor() {
return temp.getMajor();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == enter) {
//adds new student to the arraylist
temp.setFirstName(afName2.getText());
temp.setLastName(alName2.getText());
temp.setID(aid2.getText());
temp.setMajor(amajor2.getText());
list.add(temp);
this.setVisible(false);
//what should happen if the list only has one item in it
if(list.size() == 1) {
lName2.setText(temp.getFirstName());
fName2.setText(temp.getLastName());
id2.setText(temp.getID());
major2.setText(temp.getMajor());
}
}
if(e.getSource() == cancel) {
//closes the window and doesn't save
this.setVisible(false);
}
}
}

New Topic/Question
Reply



MultiQuote







|