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

My assignment is to create an Editable JComboBox to allow the user to either sel

ID: 3771070 • Letter: M

Question

My assignment is to create an Editable JComboBox to allow the user to either select a song from the dropdown list or type the title of a song to purchase. If the song title typed in is not on the list, an error message should be displayed. The code compiles, the JComboBox looks OK and it is editable, but I’m having a problem getting the error message to generate. Please modify my code, so that it will generate the error message when a typed entry is not on the list.  

PLEASE ONLY MODIFY MY CODE TO GENERATE THE ERROR MESSAGE.

Here's the code:

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

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 == null)

      {

         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

      }

}    

     

Explanation / Answer

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

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.isValid())

      {

         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