Question

In: Computer Science

Q:Write a Java application which allows the user to enter student information The user will enter...

Q:Write a Java application which allows the user to enter student information

The user will enter full name, address, city, province, postal code, phone number and email in text field controls. The student’s major (Computer Science or Business) will be selected from two radio buttons.
A combo box will display the list of courses for each program whenever the user selects the desired program.
A course will be added to a list box whenever the user selects a course from the corresponding combo box. Make sure that the user cannot add a course several times.
Additional information about the student will be provided from a group of check boxes (such as involvement in various activities, etc).
All the information about the student will be displayed in a text area component. Use simple SWING layout managers, such as FlowLayout, BorderLayout, and GridLayout to create the SWING GUI of this application. Provide the titles for the data that will be displayed in the text area.

Note:I know how to do most of the things from question but i do not know how to implement layouts.

Solutions

Expert Solution

StudentInfoTest.java

import javax.swing.*;

public class StudentInfoTest {
    public static void main(String[] args) {
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        studentInfo.setResizable(false);
        studentInfo.setTitle("Student Application");
        studentInfo.setSize(770, 500);
        studentInfo.setVisible(true);
        studentInfo.setLocationRelativeTo(null);
    }
}

StudentInfo.java

import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;


public class StudentInfo extends JFrame{

    //Declaring the variables
    private JPanel masterPanel, panel1, panel2, panel3, panel4, panel5, panel6, panel7, panel8, panel9, panel10;

    private JLabel lblFullName, lblAddress, lblCity, lblProvince, lblPostalCode, lblPhoneNumber, lblEmail;

    private JTextField txtFullName, txtAddress, txtCity, txtProvince, txtPostalCode, txtPhoneNumber, txtEmail;

    private JButton btnDisplay;

    private JCheckBox checkBox1, checkBox2;

    private JRadioButton radioButton1, radioButton2;

    private JComboBox courseList;

    private JTextArea txtArea;

    private List<String> addedCourses = new ArrayList<>();

    private JList<String> selectedCourses = new JList<>();

    //Constructor
    public StudentInfo() {
        //Setting up the Panel
        setSize(400, 400);
        setLayout(null);

        //Creating masterPanel
        masterPanel = new JPanel();
        masterPanel.setLayout(null);
        masterPanel.setBounds(6,6,740, 450);
        add(masterPanel);

        //Creating panel 1
        panel1 = new JPanel();
        panel1.setLayout(null);
        panel1.setBounds(10,30, 400, 240);
        panel1.setBorder(BorderFactory.createTitledBorder("Personal Information"));
        masterPanel.add(panel1);

        //Creating panel 2
        panel2 = new JPanel();
        panel2.setLayout(null);
        panel2.setBounds(420,18, 150, 100);
        panel2.setBorder(BorderFactory.createTitledBorder("Extra Activities"));
        masterPanel.add(panel2);

        //Creating panel 3
        panel3 = new JPanel();
        panel3.setLayout(null);
        panel3.setBounds(280, 268, 180, 35);
        masterPanel.add(panel3);

        //Creating panel 4
        panel4 = new JPanel();
        panel4.setLayout(null);
        panel4.setBounds(580, 18, 150, 100);
        panel4.setBorder(BorderFactory.createTitledBorder("Student's Major"));
        masterPanel.add(panel4);

        //Creating panel 5
        panel5 = new JPanel();
        panel5.setLayout(null);
        panel5.setBounds(420, 128, 310, 130);
        panel5.setBorder(BorderFactory.createTitledBorder("Student's Courses"));
        masterPanel.add(panel5);

        //Creating panel 6
        panel6 = new JPanel();
        panel6.setLayout(null);
        panel6.setBounds(10, 300, 720, 140);
        panel6.setBorder(BorderFactory.createTitledBorder("Student's Courses"));
        masterPanel.add(panel6);

        //Creating panel 7
        panel7 = new JPanel(new GridLayout(7, 2, -180, 5));
        panel7.setBounds(10, 20, 380, 200);

        //Creating the labels
        lblFullName = new JLabel("Full Name: ");
        lblAddress = new JLabel("Address: ");
        lblCity = new JLabel("City: ");
        lblProvince = new JLabel("Province: ");
        lblPostalCode = new JLabel("Postal Code: ");
        lblPhoneNumber = new JLabel("Phone Number: ");
        lblEmail = new JLabel("Email: ");

        //Creating the text fields
        txtFullName = new JTextField();
        txtAddress = new JTextField();
        txtCity = new JTextField();
        txtProvince = new JTextField();
        txtPostalCode = new JTextField();
        txtPhoneNumber = new JTextField();
        txtEmail = new JTextField();

        //Adding the labels and text fields to the panel
        panel7.add(lblFullName);
        panel7.add(txtFullName);
        panel7.add(lblAddress);
        panel7.add(txtAddress);
        panel7.add(lblCity);
        panel7.add(txtCity);
        panel7.add(lblProvince);
        panel7.add(txtProvince);
        panel7.add(lblPostalCode);
        panel7.add(txtPostalCode);
        panel7.add(lblPhoneNumber);
        panel7.add(txtPhoneNumber);
        panel7.add(lblEmail);
        panel7.add(txtEmail);
        panel1.add(panel7);

        //Creating a layout manager to hold the check boxes
        panel8 = new JPanel(new GridLayout(2, 2, 10, -5));
        panel8.setBounds(5, 22, 150, 75);

        //Creating the check boxes
        checkBox1 = new JCheckBox("Student Council");
        checkBox2 = new JCheckBox("Volunteer Work");

        //Adding the check boxes to the panels
        panel8.add(checkBox1);
        panel8.add(checkBox2);
        panel2.add(panel8);

        //Creating a layout manager to hold the radio buttons
        panel9 = new JPanel(new GridLayout(2, 2, 10, -5));
        panel9.setBounds(5, 22, 150, 75);

        //Creating the radio buttons
        radioButton1 = new JRadioButton("Computer Science");
        radioButton1.setSelected(true);
        radioButton2 = new JRadioButton("Business");

        //When the radio button is clicked, the event is fired
        radioButton1.addActionListener(event -> radioClicked());
        radioButton2.addActionListener(event -> radioClicked());

        //Adding radio buttons to a group
        ButtonGroup btnGroup = new ButtonGroup();
        btnGroup.add(radioButton1);
        btnGroup.add(radioButton2);

        //Adding the radio buttons to a panel
        panel9.add(radioButton1);
        panel9.add(radioButton2);
        panel4.add(panel9);

        //Adding the panel to hold the combo box
        panel10 = new JPanel(new GridLayout(1, 1));
        panel10.setBounds(9, 22, 280, 25);

        courseList = new JComboBox();

        panel10.add(courseList);

        panel5.add(panel10);

        //When the user goes to the courseList, the event is fired to add a course
        courseList.addActionListener(event -> courseAdded());

        //Display button
        btnDisplay = new JButton("Display");
        btnDisplay.setBounds(60, 5, 80, 25);

        //When the event of clicking on Display button happens, the method displayClicked will be executed
        btnDisplay.addActionListener(event -> displayClicked());

        //Adding the button to a panel
        panel3.add(btnDisplay);

        //Creating the text area to display all the user's inputs
        txtArea = new JTextArea();
        txtArea.setLineWrap(true);

        //Including the txtArea on the scrollPane
        JScrollPane scrollPane = new JScrollPane(txtArea);
        scrollPane.setBounds(10, 20, 700, 110);
        txtArea.setEditable(false);

        //Adding the scrollPane to panel6
        panel6.add(scrollPane);

        //Set border for the box which will hold the courses selected by the user
        selectedCourses.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        //Including the selected courses to a scroll pane
        JScrollPane paneCourses = new JScrollPane(selectedCourses);
        paneCourses.setBounds(10, 60, 280, 60);
        panel5.add(paneCourses);
    }

    //Method to handle the events that happen when the Display button is clicked
    public void displayClicked() {
        //Getting the user's inputs on text fields
        String fullName = txtFullName.getText();
        String address = txtAddress.getText();
        String city = txtCity.getText();
        String province = txtProvince.getText();
        String postalCode = txtPostalCode.getText();
        String phoneNumber = txtPhoneNumber.getText();
        String email = txtEmail.getText();

        try {
            if(
                    fullName == null || fullName.isEmpty() ||
                            address == null || address.isEmpty() ||
                            city == null || city.isEmpty() ||
                            province == null || province.isEmpty() ||
                            postalCode == null || postalCode.isEmpty() ||
                            phoneNumber == null || phoneNumber.isEmpty() ||
                            email == null || email.isEmpty()
                    )
            {
                JOptionPane.showMessageDialog(null, "It is necessary to fill all the fields. ");
            }
            else
            {
                txtArea.append(
                        "Full Name: " + fullName + "\n" +
                                "Address: " + address + "\n" +
                                "City: " + city + "\n" +
                                "Province: " + province + "\n" +
                                "Postal Code: " + postalCode + "\n" +
                                "Phone Number: " + phoneNumber + "\n" +
                                "Email: " + email + "\n");

                if(checkBox1.isSelected()){
                    txtArea.append("Student Council: yes\n");
                }
                else {
                    txtArea.append("Student Council: no\n");
                }
                if(checkBox2.isSelected()){
                    txtArea.append("Volunteer Work: yes\n");
                }
                else {
                    txtArea.append("Volunteer Work: no\n");
                }
                txtArea.append("Courses: \n");

                for (String course : addedCourses){
                    txtArea.append(String.format("    - %s\n", course));
                }

            }


        } catch (Exception e){}

    }

    //Method to handle the events when the first/second radioButton is clicked
    public void radioClicked(){

        try {
            if(radioButton1.isSelected()){
                courseList.removeAllItems();
                courseList.addItem("");
                courseList.addItem("Java Programming Language");
                courseList.addItem("Python Programming Language");
                courseList.addItem("C# Programming Language");
            }
            if(radioButton2.isSelected()){
                courseList.removeAllItems();
                courseList.addItem("");
                courseList.addItem("Business 1");
                courseList.addItem("Business 2");
                courseList.addItem("Business 3");
            }

        }catch (Exception e){}

    }
    //Method to handle the events when a course is added
    public void courseAdded(){
        try {
            if(courseList.getSelectedItem()!=null){
                addedCourses.add(courseList.getSelectedItem().toString());
                }

            String[] arrayCourses = new String[addedCourses.size()];

            for (int i = 0; i < addedCourses.size(); i++) {
                arrayCourses[i] = addedCourses.get(i);
            }

            selectedCourses.setListData(arrayCourses);
        }catch (Exception e){}

    }
}


Related Solutions

Write an application that allows a user to enter any number of student quiz scores, as...
Write an application that allows a user to enter any number of student quiz scores, as integers, until the user enters 99. If the score entered is less than 0 or more than 10, display Score must be between 10 and 0 and do not use the score. After all the scores have been entered, display the number of valid scores entered, the highest score, the lowest score, and the arithmetic average.
Android Studio. Java. Please create an application that -> An activity that allows user to enter...
Android Studio. Java. Please create an application that -> An activity that allows user to enter name, gender, date of birth, state of residence (selected from a pre-defined list: CA, AZ, NV, OR), email address and favorite website. This activity has a button "Show Data" that displays detail entered
Write an application that allows a user to enter any number of student quiz scores until...
Write an application that allows a user to enter any number of student quiz scores until the user enters 99. If the score entered is less than 0 or more than 10, display an appropriate message and do not use the score. After all the scores have been entered, display the number of scores entered, the highest score, the lowest score, and the arithmetic average. Save the file as QuizScoreStatistics.java. I am struggling with looping in Java and do not...
Create an application that allows the user to enter the total for an order and the...
Create an application that allows the user to enter the total for an order and the name of the customer. If the order is less than 500 dollars, the customer gets no discount. If the order is greater than or equal to 500 and less than 1000 dollars, the customer gets a 5 percent discount. If the order is greater than or equal to 1000 dollars, the customer gets a 10 percent discount. The application should display the name of...
Create an application that allows the user to enter the total for an order and the...
Create an application that allows the user to enter the total for an order and the name of the customer. If the order is less than 500 dollars, the customer gets no discount. If the order is greater than or equal to 500 and less than 1000 dollars, the customer gets a 5 percent discount. If the order is greater than or equal to 1000 dollars, the customer gets a 10 percent discount. The application should display the name of...
Write an application that allows a user to enter the names and birthdates of up to...
Write an application that allows a user to enter the names and birthdates of up to 10 friends. Continue to prompt the user for names and birthdates until the user enters the sentinel value ZZZ for a name or has entered 10 names, whichever comes first. When the user is finished entering names, produce a count of how many names were entered, and then display the names. In a loop, continuously ask the user to type one of the names...
JAVA Write a Java console application that prompts the user to enter the radius of a...
JAVA Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Programmer Notes Write and document your program per class coding conventions. Add an instance variable double radius. Generate its get/set methods. Manually type the methods getDiameter(), getCircumference(), and getArea()....
Show: Create an application that allows the user to enter the number of calories and fat...
Show: Create an application that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. One gram of fat has 9 calories, so: Calories from fat = fat grams *9 The percentage of...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters a number, the program should print out the average of the last 3 numbers (or all numbers if 3 or less have been entered). I would like a detailed explanation so that a beginner level Java programmer could understand.
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT