Listing A.1 contains the source for MusicApp.java, the Java Swing application client presented in Chapter 4 (see “A Java Swing Application Client” on page 106). Also refer to Figure 4-3 on page 106 and Figure 4-4 on page 109 for screen shots captured during execution of this program.
// MusicApp.java // Swing application to read the Music Collection Database // using the Music EJB stateless session bean // Puts recording titles into a Swing JList component // so that user can select the title // Upon selection (ListSelectionEvent), // displays additional information about the recording // When user clicks "View Tracks" JButton (ActionEvent), // displays track list for the selected recording title. import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; public class MusicApp extends JFrame implements ActionListener, ListSelectionListener { private MusicHome musicHome; // Music EJB home interface private Music mymusic; // Music EJB remote interface // Swing components private JList musicTitle; // Display music titles private JTextArea data; // Display data/track titles private JPanel statusbar; // Hold status text field private JPanel display; // Hold music/track titles private JLabel helpline; // How to use the program private JPanel helpPanel; // Panel to hold helpline private JTextField status; // Status/error messages private JButton goButton; // Button to view tracks // Keep track of various lists private ArrayList albums; // RecordingVO objects private ArrayList tracks; // TrackVO objects private Vector titles; // Recording titles // Given a RecordingVO, get the tracklist private void getTracks(RecordingVO rec) { try { // access MusicEJB tracks = mymusic.getTrackList(rec); TrackVO t; Iterator i = tracks.iterator(); while (i.hasNext()) { t = (TrackVO)i.next(); // put track info into data JTextArea data.append(" " + t.getTrackNumber() + " " + t.getTrackLength() + " " + t.getTitle()); } // Check separately for NoTrackListException } catch (NoTrackListException ex) { status.setText("NoTrackListException Exception: " + ex.getMessage()); } catch (Exception ex) { status.setText("Unexpected Exception: " + ex.getMessage()); ex.printStackTrace(); } } // Get recording information private void getRecordings() { try { // access MusicEJB albums = mymusic.getMusicList(); } catch (Exception ex) { status.setText("Unexpected Exception: " + ex.getMessage()); ex.printStackTrace(); } RecordingVO r; Iterator i = albums.iterator(); while (i.hasNext()) { r = (RecordingVO)i.next(); // put Recording titles into titles Vector, // which is displayed in the JList Swing component titles.add(r.getTitle()); } } public MusicApp() { // Set up Swing components getContentPane().setLayout(new BorderLayout()); getContentPane().setBackground(Color.lightGray); setSize(800, 200); helpPanel = new JPanel(); helpPanel.setLayout(new FlowLayout( FlowLayout.CENTER,5,5)); // Put the help panel at the top getContentPane().add("North", helpPanel); helpline = new JLabel( "Select recording & click button to View Tracks"); helpPanel.add(helpline); display = new JPanel(); display.setLayout(new GridLayout(1,2,15,15)); // Set up the musicTitle JList // Initialize it with titles vector // Make 7 rows visible and add a scrollbar // Add the ListSelectionListener // to allow user to select titles titles = new Vector(); musicTitle = new JList(titles); musicTitle.setBackground(Color.white); musicTitle.setVisibleRowCount(7); musicTitle.addListSelectionListener(this); display.add( new JScrollPane( musicTitle )); // Set up the data JTextArea // Turn off editing and add a scrollbar data = new JTextArea(12,40); data.setBackground(Color.white); data.setEditable(false); display.add( new JScrollPane( data )); // Put the display JPanel in the Center getContentPane().add("Center", display); // Set up the statusbar JPanel // Put the statusbar JPanel at the bottom // Give it a flow layout // Add the goButton JButton and the status JTextField // Add the Action Listener to goButton statusbar = new JPanel(); statusbar.setLayout(new FlowLayout( FlowLayout.LEFT,5,5)); goButton = new JButton("View Tracks"); goButton.addActionListener(this); statusbar.add(goButton); status = new JTextField(60); status.setEditable(false); statusbar.add(status); getContentPane().add("South", statusbar); setTitle("Music Collection - Using EJB with JDBC"); try { // Create Music EJB Context initial = new InitialContext(); Object objref = initial.lookup( "java:comp/env/ejb/MyMusic"); musicHome = (MusicHome)PortableRemoteObject.narrow( objref, MusicHome.class); mymusic = musicHome.create(); } catch (Exception ex) { System.out.println("Unexpected Exception: " + ex.getMessage()); ex.printStackTrace(); } // Access the EJB to read the Music Collection database // and put the title names in the JList swing component getRecordings(); status.setText("There are " + albums.size() + " recordings"); pack(); show(); // Set up the WindowListener object this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); } public void windowClosed(WindowEvent e) { System.exit(0); } }); } public MusicApp(String title) { this(); setTitle(title); } public void actionPerformed(ActionEvent e) { // Event handler for ActionEvent // (user clicks "View Tracks" button) if (e.getSource() instanceof JButton) { // make sure the user has selected a music title if (musicTitle.getSelectedValue() == null) { status.setText( "You must select a recording title first."); } else { // Tell the user what title // we're getting track info for status.setText("Displaying track list for " + musicTitle.getSelectedValue()); // Using setText() wipes out any text previously // written to JTextArea data.setText("Track information for " + musicTitle.getSelectedValue()); // getTracks() calls Music EJB // business method getTrackList() // getTracks() puts track info into the // JTextArea component getTracks( (RecordingVO)albums.get( musicTitle.getSelectedIndex())); // In case there are more tracks than visible // lines, position caret to top of component data.setCaretPosition(0); } } } public void valueChanged(ListSelectionEvent e) { // Event handler for ListSelectionEvent // (user changes selection in musicTitle // JList component) if (!e.getValueIsAdjusting()) { // Tell user which title was selected // Method getSelectedValue() returns a String status.setText("You selected " + musicTitle.getSelectedValue()); // Access the RecordingVO object at the index // of the selected music title. // Method getSelectedIndex() returns the index of // the selected list item, // which corresponds to the same // index value in the albums ArrayList collection. RecordingVO r = (RecordingVO)albums.get( musicTitle.getSelectedIndex()); // Display information from the RecordingVO object // in the JTextArea. // Use method setText() to clear previously written // text, then use method append(). // Set caret position to top in case text causes // JTextArea to scroll. data.setText("RecordingID = " + r.getRecordID() + ", "); data.append("Title = " + r.getTitle() + ", "); data.append("Artist is " + r.getArtistName() + ", "); data.append("Music Category is " + r.getMusicCategory() + ", "); data.append("Label is " + r.getLabel() + ", "); data.append("Number of Tracks = " + r.getNumberOfTracks()); data.setCaretPosition(0); } } static public void main(String args[]) { MusicApp acs = new MusicApp(); } } // MusicApp |
3.133.123.34