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

This was a 2 part assignment. The first part worked with no problem. I seemed to

ID: 3771366 • Letter: T

Question

This was a 2 part assignment. The first part worked with no problem. I seemed to have made a mess of the 2nd part when I tried to modify it. Anyway, I need the code to generate an error message if the typed selection isn't on the list.  Can someone please fix this code?  Thanks!

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class JTunes3 extends JFrame implements ActionListener
{
   
   public static void main(String[] args)
   {
      final int FRAME_WIDTH = 300;
      final int FRAME_HEIGHT = 200;
      JTunes3 frame = new JTunes3 ();
      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); //Sets size
      frame.setVisible(true);   //Makes frame viewable
      JLabel selection = new JLabel("Select song from list or type title.");
      JButton OK = new JButton("OK");
      Font myFont = new Font("Arial", Font.BOLD, 12);
      JTextField title = new JTextField(40);
      JLabel response = new JLabel("");
   }
  
  
   public JTunes3 ()
   {
      //Frame title.
      super("Purchase Songs");   //Frame Title
     
      //Prepares interface
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   ///Ends application when frame closes.
      setLayout(new FlowLayout());    //Configures layout
      JComboBox<String> song = new JComboBox<String>();
      song.setEditable(true);    //Make interface editable
      selection.setFont(myFont);
      response.setFont(myFont);
      add(selection);
      add(title);
      add(response);
      add(OK);
      OK.addActionListener(this);
      
     
     
      song.addItem("Song 1");
      song.addItem("Song 2");
      song.addItem("Song 3");
      song.addItem("Song 4");
      song.addItem("Song 5");
      song.addItem("Song 6");
      song.addItem("song 7");
      add(song);

}

public void actionPerformed(ActionEvent e)
    {
       String song = response.getText();
       String answer = "Invalid selection. Try again";
       if (!song.isValid())
       {
          return response.setText(answer);
       }
       else
       {
           return song("");
       }
      
    }
}

Explanation / Answer

import java.awt.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class JTunes3 extends JFrame

{

public JTunes3 ()

{

super("Purchase music");

  

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   

setLayout(new FlowLayout());   

JComboBox<String> music = new JComboBox<String>();

music.setEditable(true);   

  

JOptionPane.showMessageDialog( null, music, "Select or type a value.", JOptionPane.QUESTION_MESSAGE);
  
if (music.getItemCount()==0)
{
JOptionPane.showMessageDialog(null, "Invalid selection. Try again.");  

}
music.addItem("music 1");

music.addItem("music 2");

music.addItem("music 3");

music.addItem("music 4");

music.addItem("music 5");

add(music);

}

public static void main(String[] arguments)

{

final int FRAME_WIDTH = 250;

final int FRAME_HEIGHT = 200;

JTunes3 frame = new JTunes3 ();

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); //Sets size

frame.setVisible(true); //Makes frame viewable

}

}

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