Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hashmap;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Font;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Insets;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.HashMap;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- public class UpdateForm extends JFrame implements ActionListener {
- private JLabel lblTitle, lblStudentID, lblForename, lblSurname, lblSubject, lblResult, lblMessage;
- private JTextField txtStudentID, txtForename, txtSurname, txtResult;
- private JComboBox cmbSubject;
- private JButton btnFindStudent, btnUpdate, btnExit;
- private Container cn;
- private static HashMap<String, Student> privateMap;
- static Student foundItem;
- public UpdateForm(HashMap localMap)
- {
- privateMap = localMap;
- cn = getContentPane();
- cn.setLayout(new GridBagLayout());
- lblTitle = new JLabel("Student Results", JLabel.CENTER);
- lblTitle.setFont(new Font("SansSerif", Font.BOLD,17));
- lblTitle.setForeground(Color.decode("#00000"));
- lblStudentID = new JLabel("Student ID", JLabel.RIGHT);
- lblStudentID.setFont(new Font("SansSerif", Font.BOLD,17));
- lblStudentID.setForeground(Color.decode("#00000"));
- lblForename = new JLabel("Forename", JLabel.RIGHT);
- lblForename.setFont(new Font("SansSerif", Font.BOLD,17));
- lblForename.setForeground(Color.decode("#00000"));
- lblSurname = new JLabel("Surname", JLabel.RIGHT);
- lblSurname.setFont(new Font("SansSerif", Font.BOLD,17));
- lblSurname.setForeground(Color.decode("#00000"));
- lblSubject = new JLabel("Subject", JLabel.RIGHT);
- lblSubject.setFont(new Font("SansSerif", Font.BOLD,17));
- lblSubject.setForeground(Color.decode("#00000"));
- lblResult = new JLabel("Result", JLabel.RIGHT);
- lblResult.setFont(new Font("SansSerif", Font.BOLD,17));
- lblResult.setForeground(Color.decode("#00000"));
- lblMessage = new JLabel("Message will appear...", JLabel.RIGHT);
- lblMessage.setFont(new Font("SansSerif", Font.BOLD,17));
- lblMessage.setForeground(Color.decode("#00000"));
- txtStudentID= new JTextField(10);
- txtStudentID.setFont(new Font("SansSerif", Font.BOLD,14));
- txtForename= new JTextField(10);
- txtForename.setFont(new Font("SansSerif", Font.BOLD,14));
- txtSurname= new JTextField(10);
- txtSurname.setFont(new Font("SansSerif", Font.BOLD,14));
- String[] sub = {"Computing", "Math", "English", "History"};
- cmbSubject = new JComboBox(sub);
- cmbSubject.setFont(new Font("SansSerif", Font.PLAIN,16));
- cmbSubject.addActionListener(this);
- txtResult= new JTextField(10);
- txtResult.setFont(new Font("SansSerif", Font.BOLD,14));
- btnFindStudent = new JButton ("Find Student");
- btnFindStudent.setFont(new Font("SansSerif", Font.BOLD,17));
- btnFindStudent.addActionListener(this);
- btnUpdate = new JButton ("Update");
- btnUpdate.setFont(new Font("SansSerif", Font.BOLD,17));
- btnUpdate.addActionListener(this);
- btnExit = new JButton ("Exit");
- btnExit.setFont(new Font("SansSerif", Font.BOLD,17));
- btnExit.addActionListener(this);
- getContentPane().setBackground(Color.decode("#EEEEEE"));
- //disable
- txtForename.setEnabled(false);
- txtSurname.setEnabled(false);
- cmbSubject.setEnabled(false);
- txtResult.setEnabled(false);
- btnUpdate.setEnabled(false);
- addComp(lblTitle, 1, 0, 1, 1, 0, 0);
- addComp(lblStudentID, 0, 1, 1, 1, 0, 0);
- addComp(lblForename, 0, 2, 1, 1, 0, 0);
- addComp(lblSurname, 0, 3, 1, 1, 0, 0);
- addComp(lblSubject, 0, 4, 1, 1, 0, 0);
- addComp(lblResult, 0, 5, 1, 1, 0, 0);
- addComp(lblMessage, 0, 6, 1, 1, 0, 0);
- addComp(txtStudentID, 1, 1, 2, 1, 0, 0);
- addComp(txtForename, 1, 2, 2, 1, 0, 0);
- addComp(txtSurname, 1, 3, 2, 1, 0, 0);
- addComp(cmbSubject, 1, 4, 1, 1, 0, 0);
- addComp(txtResult, 1, 5, 2, 1, 0, 0);
- addComp(btnFindStudent, 0, 7, 2, 1, 0, 0);
- addComp(btnUpdate, 2, 7, 1, 1, 0, 0);
- addComp(btnExit, 3, 7, 1, 1, 0, 0);
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- UpdateForm u = new UpdateForm(privateMap);
- u.setTitle("Update Student");
- u.setSize(600, 500);
- u.setLocation(900, 500);
- u.setVisible(true);
- u.setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- private void addComp(Component c,
- int column, int row, int numColumns,
- int numRows, int weightx, int weighty)
- {
- GridBagConstraints gbConstraints = new GridBagConstraints();
- //set parameters
- gbConstraints.fill = GridBagConstraints.BOTH;
- gbConstraints.anchor = GridBagConstraints.CENTER;
- gbConstraints.gridx = column;
- gbConstraints.gridy = row;
- gbConstraints.gridwidth = numColumns;
- gbConstraints.gridheight = numRows;
- gbConstraints.weightx = weightx;
- gbConstraints.weighty = weighty;
- gbConstraints.insets = new Insets(5,5,5,5);
- //add to container
- cn.add(c, gbConstraints);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- if(e.getSource() == btnExit)
- {
- setVisible(false);
- }
- if(e.getSource() == btnFindStudent)
- {
- String findStudentID = "";
- findStudentID = txtStudentID.getText().trim();
- foundItem = privateMap.get(findStudentID);
- if(foundItem != null)
- {
- txtStudentID.setEnabled(false);
- btnUpdate.setEnabled(true);
- txtForename.setText(foundItem.getForename());
- txtSurname.setText(foundItem.getSurname());
- cmbSubject.setSelectedItem(foundItem.getSubject());
- String r = Integer.toString(foundItem.getResult());
- txtResult.setText(r);
- txtForename.setEnabled(true);
- txtSurname.setEnabled(true);
- cmbSubject.setEnabled(true);
- txtResult.setEnabled(true);
- btnUpdate.setEnabled(true);
- }
- }
- if(e.getSource() == btnUpdate)
- {
- foundItem.setForename(txtForename.getText());
- foundItem.setSurname(txtSurname.getText());
- foundItem.setSubject(cmbSubject.getSelectedItem().toString());
- int sR = Integer.parseInt(txtResult.getText());
- foundItem.setResult(sR);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement