/******************************* * Name: Huy Nguyen * * Lab #2 * * Fall 1998, ICS 52 * ********************************/ import java.awt.*; import java.awt.event.*; // A dialog box which allows the user to enter course info public class AddCourseDialog extends Dialog { Label label1, label2, label3, title; TextField textField1, textField3; Choice choice; Button okButton; Button cancelButton; String courseNumber, quarter, grade; boolean cancelled; Panel OKCancel; GridBagLayout gb; GridBagConstraints gc; Schedule schedule; public AddCourseDialog(Frame parent, Schedule schedule) { super( parent, true ); // true == modal this.schedule = schedule; gb = new GridBagLayout(); setLayout(gb); gc = new GridBagConstraints(); title = new Label( "ADDING-COURSES DIALOG BOX"); gc.fill = GridBagConstraints.BOTH; addComponent( title, gb, gc, 0, 0, 1, 4 ); gc.fill = GridBagConstraints.HORIZONTAL; label1 = new Label("Course Number: "); addComponent( label1, gb, gc, 3, 0, 1, 2 ); textField1 = new TextField("", 7); addComponent( textField1, gb, gc, 3, 2, 1, 2 ); label2 = new Label("Quarter: "); addComponent( label2, gb, gc, 5, 0, 1, 2 ); choice = new Choice(); choice.addItem("Fall, 1997"); choice.addItem("Winter, 1997"); choice.addItem("Spring, 1997"); choice.addItem("Summer, 1997"); choice.addItem("Fall, 1998"); choice.addItem("Winter, 1998"); choice.addItem("Spring, 1998"); choice.addItem("Summer, 1998"); choice.addItem("Fall, 1999"); choice.addItem("Winter, 1999"); choice.addItem("Spring, 1999"); choice.addItem("Summer, 1999"); addComponent( choice, gb, gc, 5, 2, 1, 2 ); label3 = new Label("Grade: "); addComponent( label3, gb, gc, 7, 0, 1, 2 ); textField3 = new TextField("", 3); addComponent( textField3, gb, gc, 7, 2, 1, 2 ); OKCancel = new Panel(); OKCancel.setLayout( new FlowLayout(FlowLayout.CENTER)); okButton = new Button(" OK "); cancelButton = new Button("Cancel"); OKCancel.add(okButton); OKCancel.add(cancelButton); addComponent( OKCancel, gb, gc, 10, 0, 1, 4 ); setTitle( "Add Course To Schedule" ); setSize(300,150); this.addWindowListener( new MyWindowAdapter() ); ButtonListener buttonListener = new ButtonListener(); cancelButton.addActionListener(buttonListener); okButton.addActionListener(buttonListener); setVisible( true ); } private void addComponent( Component c, GridBagLayout g, GridBagConstraints gc, int row, int column, int height, int width ) { gc.gridx = column; gc.gridy = row; gc.gridwidth = width; gc.gridheight = height; g.setConstraints(c, gc); add(c); } public String getCourseNumber() { return courseNumber; } public String getQuarter() { return quarter; } String getGrade() { return grade; } boolean wasNotCancelled() { return !cancelled; } // The following two classes are inner classes. class MyWindowAdapter extends WindowAdapter { public void windowClosing(WindowEvent event) { dispose(); } } // Since this class listens for events from two classes (buttons), // it uses the getSource() method to find out which button // sent the event message. class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { Object object = event.getSource(); if (object == cancelButton) { // remove the dialog from the screen cancelled = true; AddCourseDialog.this.dispose(); } else { courseNumber = AddCourseDialog.this.textField1.getText(); quarter = AddCourseDialog.this.choice.getSelectedItem(); grade = AddCourseDialog.this.textField3.getText(); //schedule.addCourse( courseNumber, quarter, grade ); AddCourseDialog.this.dispose(); cancelled = false; } } } }