import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class SongDetailsDialog extends JDialog { // This is a pointer to the email buddy that is being edited private Song theSong; DialogClient theDialogClient; // These are the components of the dialog box private JLabel aLabel; //reuseable label variable private JTextField nameField; //name of the song private JTextField artistField; //artist of the song private JTextField ratingField; //rating of the song private JTextField priceField; //price of the song private JTextField yearField; //year of the song private JButton okButton; private JButton cancelButton; public SongDetailsDialog(Frame owner, DialogClient aClient, String title, boolean modal, Song aSong){ super(owner,title,modal); //Store the client and model variables theDialogClient = aClient; theSong = aSong; // Put all the components onto the window and given them initial values buildDialogWindow(theSong); // Add listeners for the Ok and Cancel buttons as well as window closing okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event){ okButtonClicked(); }}); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event){ cancelButtonClicked(); }}); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { cancelButtonClicked(); }}); setSize(400, 250); } // This code adds the necessary components to the interface private void buildDialogWindow(Song aSong) { GridBagLayout layout = new GridBagLayout(); GridBagConstraints lc = new GridBagConstraints(); getContentPane().setLayout(layout); lc.anchor = GridBagConstraints.EAST; lc.insets = new Insets(5, 5, 5, 5); aLabel = new JLabel("Title"); lc.gridx = 0; lc.gridy = 0; lc.gridwidth = 1; lc.gridheight = 1; lc.weightx = 0.0; lc.weighty = 0.0; layout.setConstraints(aLabel, lc); getContentPane().add(aLabel); aLabel = new JLabel("Artist"); lc.gridx = 0; lc.gridy = 1; lc.gridwidth = 1; lc.gridheight = 1; lc.weightx = 0.0; lc.weighty = 0.0; layout.setConstraints(aLabel, lc); getContentPane().add(aLabel); aLabel = new JLabel("Year"); lc.gridx = 0; lc.gridy = 2; lc.gridwidth = 1; lc.gridheight = 1; lc.weightx = 0.0; lc.weighty = 0.0; layout.setConstraints(aLabel, lc); getContentPane().add(aLabel); aLabel = new JLabel("Price"); lc.gridx = 0; lc.gridy = 3; lc.gridwidth = 1; lc.gridheight = 1; lc.weightx = 0.0; lc.weighty = 0.0; layout.setConstraints(aLabel, lc); getContentPane().add(aLabel); aLabel = new JLabel("Rating"); lc.gridx = 0; lc.gridy = 4; lc.gridwidth = 1; lc.gridheight = 1; lc.weightx = 0.0; lc.weighty = 0.0; layout.setConstraints(aLabel, lc); getContentPane().add(aLabel); aLabel = new JLabel(" "); //blank label lc.gridx = 0; lc.gridy = 5; lc.gridwidth = 3; lc.gridheight = 1; lc.weightx = 0.0; lc.weighty = 0.0; layout.setConstraints(aLabel, lc); getContentPane().add(aLabel); aLabel = new JLabel(" "); //blank label lc.gridx = 1; lc.gridy = 6; lc.gridwidth = 1; lc.gridheight = 1; lc.weightx = 1.0; lc.weighty = 0.0; layout.setConstraints(aLabel, lc); getContentPane().add(aLabel); // Add the name field nameField = new JTextField(aSong.getTitle()); lc.gridx = 1; lc.gridy = 0; lc.gridwidth = 3; lc.gridheight = 1; lc.fill = GridBagConstraints.BOTH; lc.weightx = 1.0; lc.weighty = 0.0; layout.setConstraints(nameField, lc); getContentPane().add(nameField); // Add the address field artistField = new JTextField(aSong.getArtist()); lc.gridx = 1; lc.gridy = 1; lc.gridwidth = 3; lc.gridheight = 1; lc.fill = GridBagConstraints.BOTH; lc.weightx = 1.0; lc.weighty = 0.0; layout.setConstraints(artistField, lc); getContentPane().add(artistField); // Add the year field yearField = new JTextField(""+ aSong.getYear()); lc.gridx = 1; lc.gridy = 2; lc.gridwidth = 3; lc.gridheight = 1; lc.fill = GridBagConstraints.BOTH; lc.weightx = 1.0; lc.weighty = 0.0; layout.setConstraints(yearField, lc); getContentPane().add(yearField); // Add the price field priceField = new JTextField("$"+ aSong.getPrice()); lc.gridx = 1; lc.gridy = 3; lc.gridwidth = 3; lc.gridheight = 1; lc.fill = GridBagConstraints.BOTH; lc.weightx = 1.0; lc.weighty = 0.0; layout.setConstraints(priceField, lc); getContentPane().add(priceField); // Add the rating field ratingField = new JTextField(aSong.getRating()); lc.gridx = 1; lc.gridy = 4; lc.gridwidth = 3; lc.gridheight = 1; lc.fill = GridBagConstraints.BOTH; lc.weightx = 1.0; lc.weighty = 0.0; layout.setConstraints(ratingField, lc); getContentPane().add(ratingField); // Add the Ok button okButton = new JButton("Ok"); lc.gridx = 2; lc.gridy = 6; lc.gridwidth = 1; lc.gridheight = 1; lc.weightx = 0.0; lc.weighty = 0.0; layout.setConstraints(okButton, lc); getContentPane().add(okButton); // Add the Cancel button cancelButton = new JButton("Cancel"); lc.gridx = 3; lc.gridy = 6; lc.gridwidth = 1; lc.gridheight = 1; lc.weightx = 0.0; lc.weighty = 0.0; layout.setConstraints(cancelButton, lc); getContentPane().add(cancelButton); } private void okButtonClicked(){ //MISSING CODE //Code needs to be added for Problem 2 //1)Get details about the song from the various text fields //and set the proper values in the song being edited. //Note //----- // to convert the text in the yearField to an integer use: //Integer.parseInt(yearField.getText()); //To convert the text in the priceField to a float use and strip off the $ sign use: //Float.parseFloat(priceField.getText().substring(1)) //2)Inform the dialog client that the dialog finised //3)Make the dialog go away } private void cancelButtonClicked(){ //MISSING CODE //Code needs to be added for Problem 2 //The dialog user as chosen to cancel the dialog //1)Discard the information that was being edited //2)Inform the dialog client that the dialog was cancelled //3)Make the dialog go away } }