Question

*Use Java to create this program* For this assignment, you will be building a Favorite Songs...

*Use Java to create this program*

For this assignment, you will be building a Favorite Songs application. The application should have a list with the items displayed, a textbox for adding new items to the list, and four buttons: Add, Remove, Load, and Save. The Add button takes the contents of the text field (textbox) and adds the item in it to the list. Your code must trim the whitespace in front of or at the end of the input in the text field. Infix whitespace is okay (e.g., “happy birthday” – the trim method will not remove the space between the words “happy” and “birthday”.) Also, the text in the textbox should be converted to a lowercase representation (e.g., “Awake and Alive” is converted to “awake and alive”). The Remove button removes a selected item from the list. The list should allow the user to select more than one item at a time. If no items are selected when the user clicks this remove button, then the application should pop up a message box saying, “Please select an item to delete!” The Load button loads the previously saved song list into the list widget on your interface. The Save button saves a copy of the song list to a file, named songs.txt. If the list.txt file doesn’t exist, this operation creates it. If it does exist, it will simply overwrite it. You should produce a user-friendly interface of your own design.

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

COMMENT IF YOU HAVE DOUBTS AND LIKE THE ANSWER.

//-------- SongManager.java ---------

import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.MatteBorder;
import javax.swing.border.TitledBorder;

import java.io.*;
import java.util.*;;

public class SongManager extends JFrame implements ActionListener {

   JPanel leftPanel;
   JPanel rightPanel;
   JButton add, remove, load, save;
   JTextField songName;
   JList<String> songsList;

   SongManager() {
       setLayout(null);
       setSize(850, 500);
       add = new JButton("Add Song");
       add.setBounds(50, 100, 120, 30);
       remove = new JButton("Remove Song");
       remove.setBounds(180, 100, 120, 30);

       load = new JButton("Load Songs");
       load.setBounds(50, 150, 120, 30);

       save = new JButton("Save Songs");
       save.setBounds(180, 150, 120, 30);

       JLabel label = new JLabel("Enter Song Name: ");
       label.setBounds(20, 40, 130, 30);

       songName = new JTextField(20);
       songName.setBounds(170, 40, 200, 30);

       leftPanel = new JPanel(null);
       leftPanel.setBorder(new TitledBorder("Song Management"));
       leftPanel.setBounds(10, 10, 400, 300);
       leftPanel.add(label);
       leftPanel.add(songName);
       leftPanel.add(add);
       leftPanel.add(remove);
       leftPanel.add(load);
       leftPanel.add(save);

       rightPanel = new JPanel();
       rightPanel.setBorder(new TitledBorder("Songs List"));

       songsList = new JList<String>(new DefaultListModel<String>());

       songsList.setBounds(430, 10, 350, 400);
       songsList.setFixedCellWidth(320);

       rightPanel.add(songsList);
       rightPanel.setBounds(430, 10, 350, 400);
       add(leftPanel);
       add(rightPanel);
       add.addActionListener(this);
       remove.addActionListener(this);
       load.addActionListener(this);
       save.addActionListener(this);
   }

   public void actionPerformed(ActionEvent ae) {
       if (ae.getSource() == add) {
           String song = songName.getText().trim().toLowerCase();
           if (song.equals("")) {
               JOptionPane.showMessageDialog(this, "Warning enter some text in input box and then click Add",
                       "Warning", JOptionPane.WARNING_MESSAGE);
               return;
           }
           ((DefaultListModel) songsList.getModel()).addElement(song);
           songName.setText("");
       } else if (ae.getSource() == remove) {
           DefaultListModel dlm = (DefaultListModel) songsList.getModel();
           int count = songsList.getSelectedIndices().length;
           if(count == 0)
           {
               JOptionPane.showMessageDialog(this, "Please select an item to delete!", "Warning",
                       JOptionPane.WARNING_MESSAGE);
               return;
           }
           for (int i = 0; i < count; i++) {
               dlm.removeElementAt(songsList.getSelectedIndex());
           }
       } else if (ae.getSource() == load) {
           List<String> songs = loadSongs("songs.txt");
           if (songs.isEmpty()) {
               JOptionPane.showMessageDialog(this, "No songs in the file songs.txt", "Information",
                       JOptionPane.INFORMATION_MESSAGE);
               return;
           }

           else
           {
               JOptionPane.showMessageDialog(this, "Loaded " +songs.size() +" songs from songs.txt", "Success",
                       JOptionPane.INFORMATION_MESSAGE);
           }
           for (String song : songs) {
               if (!song.equals("")) {
                   ((DefaultListModel) songsList.getModel()).addElement(song);
               }
           }
       } else if (ae.getSource() == save) {
           saveSongsList("songs.txt");
       }

   }

   public void saveSongsList(String fileName) {
       try {
          
           BufferedWriter bw = new BufferedWriter(new FileWriter(new File(fileName)));
           for (int i = 0; i < songsList.getModel().getSize(); i++) {
               bw.write(songsList.getModel().getElementAt(i) + "\n");
           }
           bw.close();
           JOptionPane.showMessageDialog(this, "Added " + songsList.getModel().getSize() +" songs in songs.txt", "Success",
                   JOptionPane.INFORMATION_MESSAGE);
       } catch (Exception e) {
           JOptionPane.showMessageDialog(this, "Error occured while loading songs in file: " + fileName, "Error",
                   JOptionPane.ERROR_MESSAGE);
       }

   }

   public List<String> loadSongs(String fileName) {
       List<String> list = new ArrayList<>();
       try {

           BufferedReader br = new BufferedReader(new FileReader(new File(fileName)));
           String line = br.readLine();
           int i = 0;
           while (line != null) {
               line = line.trim().toLowerCase();
               list.add(line);
               line = br.readLine();
               i++;
           }
           br.close();

       } catch (Exception e) {
           JOptionPane.showMessageDialog(this, "Error occured while loading songs in file: " + fileName, "Error",
                   JOptionPane.ERROR_MESSAGE);
       }
       return list;
   }

   public static void main(String[] args) {
       SongManager manager = new SongManager();
       manager.setTitle("Song Manger");
       manager.setVisible(true);
       manager.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

}

Add a comment
Know the answer?
Add Answer to:
*Use Java to create this program* For this assignment, you will be building a Favorite Songs...
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
  • Subject: Advance application development Visual studio exercise The project is to create a simple Notepad style Tex...

    Subject: Advance application development Visual studio exercise The project is to create a simple Notepad style Text Editor. This will demonstrate the C# language basics. user interface controls and how to handle user events. Controls can be found in the Toolbox pane in its default location on the left side of the IDE, with the Control Properties pane on the right side of the IDE. Include the items on the following list in the program Create a C# Windows Forms...

  • I need help building code in python for this: Create a program that: Creates a sales...

    I need help building code in python for this: Create a program that: Creates a sales receipt, displays the receipt entries and totals, and saves the receipt entries to a file Prompt the user to enter the Item Name Item Quantity Item Price Display the item name, the quantity, and item price, and the extended price (Item Quantity multiplied by Item Price) after the entry is made Save the item name, quantity, item price, and extended price to a file...

  • Java Inventory Management Code Question

    Inventory ManagementObjectives:Use inheritance to create base and child classesUtilize multiple classes in the same programPerform standard input validationImplement a solution that uses polymorphismProblem:A small electronics company has hired you to write an application to manage their inventory. The company requested a role-based access control (RBAC) to increase the security around using the new application. The company also requested that the application menu must be flexible enough to allow adding new menu items to the menu with minimal changes. This includes...

  • Program using visual basic.net You will create a very simple two numbers calculator with save options;...

    Program using visual basic.net You will create a very simple two numbers calculator with save options; here is the specifications for the application. Create a form divided vertically in two halves with right and left panels 1- In the left panel you will create the following control, the label or the value of the control will be in "" and the type of the control will in [] a- "First Number" [textbox] b- "Second number" [texbox] c- "Result" [textbox] -...

  • Java Thank you!! In this assignment you will be developing an image manipulation program. The remaining...

    Java Thank you!! In this assignment you will be developing an image manipulation program. The remaining laboratory assignments will build on this one, allowing you to improve your initial submission based on feedback from your instructor. The program itself will be capable of reading and writing multiple image formats including jpg, png, tiff, and a custom format: msoe files. The program will also be able to apply simple transformations to the image like: Converting the image to grayscale . Producing...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

  • Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by...

    Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by implementing Person and Student classes. Once you are sure you can serialize and deserialize and ArrayList of Students to and from a file, move on to building the GUI application. Person: The Person class should implement serializable interface. It contains the following: Person's first name (String) Person's last name (String) Person's id number Person's date of birth (Date) public String toString(): This method method...

  • This homework problem has me pulling my hair out. We are working with text files in Java using Ne...

    This homework problem has me pulling my hair out. We are working with text files in Java using NetBeans GUI application. We're suppose to make a movie list and be able to pull up movies already in the text file (which I can do) and also be able to add and delete movies to and from the file (which is what I'm having trouble with). I've attached the specifications and the movie list if you need it. Any help would...

  • Python Subject Output in Thonny ot IDLE Description Create a program that keeps track of the...

    Python Subject Output in Thonny ot IDLE Description Create a program that keeps track of the items that a wizard can carry. Sample Output (Your output should be similar to the text in the following box) The Wizard Inventory program COMMAND MENU walk - Walk down the path show - Show all items drop - Drop an item exit - Exit program Command: walk While walking down a path, you see a scroll of uncursing. Do you want to grab...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

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