Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is Java GUI help. I am looking for help adding a GUI in this DVD sorting pr

ID: 3807602 • Letter: T

Question

This is Java GUI help.

I am looking for help adding a GUI in this DVD sorting programing, I have the logic of the program completely done but I could use help with the GUI part.

What I am looking for is to have the GUI show the DVDs that are in the Array unsorted and then have a button that the user can click and then it will show the updated DVDs after the sort.

This is what the assignment requires.

1. It should be still be graphical with a frame and panel

Thanks for the help. My code follows below.

DVD.java

import java.text.NumberFormat;

public class DVD implements Comparable{
  
   private String tile, director;
   private int year;
   private double cost;
   private boolean bluRay;
   private String title;

   public DVD (String title, String director, int year, double cost, boolean BluRay){
       this.title=title;
       this.director=director;
       this.cost=cost;
       this.bluRay=BluRay;
   }
  
   public String getTitle(){
       return title;
   }
  
   public String toString(){
   NumberFormat fmt = NumberFormat.getCurrencyInstance();

   String description;

   description = fmt.format(cost) + " " + year + " ";
   description += title + " " + director;

   if (bluRay)
   description += " " + "Blu-Ray";

   return description;
   }
  
   public int compareTo(Object otherObject){
       DVD otherDvd=(DVD)otherObject;
       return getTitle().compareTo(otherDvd.getTitle());
   }
}

Moives.java


public class Moives {
  
   public static void main(String[] args) {
      
       DVD[] movies=new DVD[7];

       movies[0]= new DVD ("The Godfather", "Francis Ford Coppola", 1972, 24.95, true);
       movies[1]= new DVD ("District 9", "Neill Blokamp", 2009, 19.95, false);
       movies[2]= new DVD ("Iron Man", "Jon Favreau", 2008, 15.95, false);
       movies[3]= new DVD ("All About Eve", "Joseph Mankiewicz", 1950, 17.50, false);
       movies[4]= new DVD ("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true);
       movies[5]= new DVD ("Iron Man 2", "Jon Favreau", 2010, 22.99, false);
       movies[6]= new DVD ("Casablanca", "Michael Curtiz", 1942, 19.95, false);
      
       Sorting.selectionSort(movies);
      
       for(DVD dvd:movies)
           System.out.println(dvd);
   }
}

Sorting.java
public class Sorting {

   public static void selectionSort(Comparable[] list){
  
       int min;
       Comparable temp;
      
       for(int index=0; index            min=index;
           for (int scan = index+1; scan < list.length; scan++)
               if (list[scan].compareTo(list[min])<0)
                   min=scan;
                   temp=list[min];
                   list[min]=list[index];
                   list[index]=temp;
       }
   }
}

Explanation / Answer

//Modified code with GUI .There is only changes in Movies.java file

//Sorting.java

public class Sorting
{

   public static void selectionSort(Comparable[] list)
   {

       int min;
       Comparable temp;
    
       for(int index=0; index<list.length;index++)   
          {    min=index;
           for (int scan = index+1; scan < list.length; scan++)
               if (list[scan].compareTo(list[min])<0)
                   min=scan;
                temp=list[min];
                list[min]=list[index];
                list[index]=temp;
       }
   }
}

//DVD.java

import java.text.NumberFormat;

public class DVD implements Comparable
{

   private String tile, director;
   private int year;
   private double cost;
   private boolean bluRay;
   private String title;

   public DVD (String title, String director, int year, double cost, boolean bluray)
   {
       this.title=title;
       this.director=director;
       this.cost=cost;
       this.year=year;
       this.bluRay=bluray;
   }

   public String getTitle()
   {
       return title;
   }

   public String toString()
   {
    NumberFormat fmt = NumberFormat.getCurrencyInstance();
    String description;

   description = fmt.format(cost) + " " + year + " ";
   description += title + " " + director;

   if (bluRay)
   description += " " + "Blu-Ray";

   return description;
   }

   public int compareTo(Object otherObject)
   {
       DVD otherDvd=(DVD)otherObject;
       return getTitle().compareTo(otherDvd.getTitle());
   }
}

//Movies.java


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class Movies extends JFrame
{
    private static DVD movies[];
    private static JButton sort = new JButton("Perform Sorting");
    private static JTextArea textarea = new JTextArea(20, 35);
    public Movies()
    {

//Adding listener to sort button
        sort.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
      //Performing sorting
                       Sorting.selectionSort(movies);
                   //Displaying result in textarea

   display();
                    
                     }
    });
    setLayout(new FlowLayout());

//Adding scroll bar to textarea
    add(new JScrollPane(textarea));
    add(sort);

    }
    public static void setFrame(final JFrame frame, final int width, final int height) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
frame.setTitle(frame.getClass().getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
       }
     });
   }
   public static void display()
   {
      String ans="";
       for(DVD dvd:movies)
        ans+=dvd+" ";
      textarea.setText(ans);
   }
   public static void main(String[] args)
   {
    
       //DVD[] movies=new DVD[7];
        movies=new DVD[7];

       movies[0]= new DVD ("The Godfather", "Francis Ford Coppola", 1972, 24.95, true);
       movies[1]= new DVD ("District 9", "Neill Blokamp", 2009, 19.95, false);
       movies[2]= new DVD ("Iron Man", "Jon Favreau", 2008, 15.95, false);
       movies[3]= new DVD ("All About Eve", "Joseph Mankiewicz", 1950, 17.50, false);
       movies[4]= new DVD ("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true);
       movies[5]= new DVD ("Iron Man 2", "Jon Favreau", 2010, 22.99, false);
       movies[6]= new DVD ("Casablanca", "Michael Curtiz", 1942, 19.95, false);

       setFrame(new Movies(), 300, 150);
      display();
   }
}

//Output :

//Before sorting :

//After Sorting

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote