Question

java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

java programming

how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...)

and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location.

///main

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
ArrayList<Student>students = new ArrayList<>();
int choice;
while(true)
{
displayMenu();
choice = in.nextInt();
switch(choice)
{
case 1:
in.nextLine();
System.out.println("Enter Student Name");
String name = in.nextLine();
  
System.out.println("Enter Course Name");
String course = in.nextLine();
Course c = new Course();
c.setcourse(course);
  
System.out.println("Enter Instructor Name");
String instructor = in.nextLine();
Instructor i = new Instructor();
i.setinstructor(instructor);
  
System.out.println("Enter Location Name");
String location = in.nextLine();
Location l = new Location();
l.setlocation(location);
  
students.add(new Student(name,c,i,l));
break;

case 2:
in.nextLine();
System.out.println("Enter student name to delete");
String studentName = in.nextLine();
boolean found = false;
for (int j = 0; j<students.size(); j++){
if(students.get(j).getName().equalsIgnoreCase(studentName))
{
} else {
students.remove(j);
System.out.println("Student Removed");
found=true;
}
}
if(!found)
{
System.out.println("Student not found");
}
break;

case 3:
in.nextLine();
boolean found1=false;
System.out.println("Search for:");
String searchStudent = in.nextLine();
for(Student s:students){
if(s.getName().equalsIgnoreCase(searchStudent)){
System.out.print("Student information: \n"+ s);
found1 = true;
}
}
if(!found1)
System.out.println("Student not found");
break;

case 4:
System.exit(0);
break;

}
}
}
  
public static void displayMenu()
{
System.out.print("\nPlease select an option:");
System.out.println("\n1.Add Student \n2.Delete Student \n3.Search for Student \n4.Exit");
  
}

}

///course

class Course {

    String course;

    public String getcourse() {
        return course;
    }
    public void setcourse(String course) {
        this.course = course;
    }
}

///insturctor

class Instructor {

    String instructor;

    public String getinstructor() {
        return instructor;
    }

    public void setinstructor(String instructor) {
        this.instructor = instructor;
    }

}

///location

class Location {

    String location;

    public String getlocation() {
        return location;
    }

    public void setlocation(String location) {
        this.location = location;
    }
}

/////student

public class Student {

    private String name;
    private Course c;
    private Instructor i;
    private Location l;

    public Student(String name ,Course c,Instructor i, Location l)
    {
        this.name = name;
        this.c = c;
        this.i = i;
        this.l = l;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return name + ":" + c.getcourse() + "," + l.getlocation() + ", Proffesor "
                + i.getinstructor();
    }

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. Course , Student, Location and instructor classes remain the same.

Added pointers to modify the code to add GUI for menu based system.

We will build GUI using Swings, by importing

import javax.swing.*;

Step 1: Create a container using the JFrame component.

In main function, replace scanner initialization with frame

//Scanner in = new Scanner(System.in); // This code initializes scanner for getting inputs

JFrame frame = new JFrame("My Form"); // Initalize the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);

Step 2: Create a menu bar, you can add options as per the need, adding a basic instructions for now.

//Creating the MenuBar and adding components
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("FILE");
JMenu m2 = new JMenu("Help");
JMenuItem m11 = new JMenuItem("Add Student");
JMenuItem m12 = new JMenuItem("Delete Student");
JMenuItem m13 = new JMenuItem("Search for Student");
JMenuItem m14 = new JMenuItem("Print Students");
mb.add(m1);
mb.add(m2);
m1.add(m11);

m1.add(m12);

m1.add(m13);

m1.add(m14);

Step 3: Create the panel and add it to the frame

 JPanel panel = new JPanel(); // the panel is not visible in output
//Adding Components to the frame.
frame.getContentPane().add(BorderLayout.SOUTH, panel); // Using border layout to place the components
frame.getContentPane().add(BorderLayout.NORTH, mb);

Step 4: Create Text fields and labels and buttons and add them to panel

JLabel label1 = new JLabel("Enter Student Name");
JTextField tf1 = new JTextField(100); 

String name = tf1.getText();

JLabel label2 = new JLabel("Enter Course Name");
JTextField tf2 = new JTextField(100);

String course = tf2.getText();
Course c = new Course();
c.setcourse(course);

JLabel label3 = new JLabel("Enter Instructor Name");
JTextField tf3 = new JTextField(100);

String instructor =tf3 .getText();
Instructor i = new Instructor();
i.setinstructor(instructor);

JLabel label4 = new JLabel("Enter Location Name");
JTextField tf4 = new JTextField(100);

String location = tf4.getText();
Location l = new Location();
l.setlocation(location);

// add the above options to the panel

JButton submit = new JButton("Submit"); // Create the buttons and add it to the panel
JButton reset = new JButton("Reset");
panel.add(label1); // Components Added using default Layout
panel.add(label2); 
panel.add(label3); 
panel.add(label4); 
panel.add(tf1);
panel.add(tf2);
panel.add(tf3);
panel.add(tf4);
panel.add(submit); // submit button
panel.add(reset); // Reset form

// On the similar lines, create panel for all the cases separately and all the components to different panels

Step 5: Add listeners to menu items to switch the panels in the frame, below is example for student menu item

m11.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            addStudent(evt);
        }
    });

// define Function to switch the panel in frame.

private void addStudent(java.awt.event.ActionEvent evt) { 
//Jpanel "Student"
}

Step 6: Add action listeners to button to submit and save or remove the data to arraylist.

Step 7: Set frame to visible

frame.setVisible(true);

To create print panel use a grid layout for the panel and add the data or content as labels

Add a comment
Know the answer?
Add Answer to:
java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • Java Programming Question

    After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • MERGESORT ASSIGNMENT FOLLOW THE FORMAT Modify the MergeSort code provided in class (if you find a...

    MERGESORT ASSIGNMENT FOLLOW THE FORMAT Modify the MergeSort code provided in class (if you find a mistake with it extra credit) (https://repl.it/@ashokbasawapatna/MergeSortExample) to work with Students (No need for generic types, you will only write Main.java). Then use the compareTo method for strings in Java to sort the students in Alphabetical order via MergeSort. 1) Create an array of 10 students by hand. Make sure it's not in alphabetical order. ONLY USE LOWER CASES FOR NAMES (I'll only test with...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • PLEASE READ ALL Modify the MergeSort code provided in class (if you find a mistake with...

    PLEASE READ ALL Modify the MergeSort code provided in class (if you find a mistake with it extra credit) (https://repl.it/@ashokbasawapatna/MergeSortExample) to work with Students (No need for generic types, you will only write Main.java). Then use the compareTo method for strings in Java to sort the students in Alphabetical order via MergeSort. 1) Create an array of 10 students by hand. Make sure it's not in alphabetical order. ONLY USE LOWER CASES FOR NAMES (I'll only test with that). 2)...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • JAVA: How do I output all the data included for each employee? I can only get...

    JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...

  • This is the question: These are in Java format. Comments are required on these two Classes...

    This is the question: These are in Java format. Comments are required on these two Classes (Student.java and StudentDemo.java) all over the coding: Provide proper comments all over the codings. --------------------------------------------------------------------------------------------------------------- import java.io.*; import java.util.*; class Student {    private String name;    private double gradePointAverage;    public Student(String n , double a){    name = n;    gradePointAverage = a;    }    public String getName(){ return name;    }    public double getGradePointAverage(){ return gradePointAverage;    }   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT