4.3. A Java Swing Application Client

Our first client for the Music EJB is a stand-alone application with a Java Swing graphical user interface.

Information Source

The Java Swing API is outside the scope of this text. For more information, consult Sun's Java web site at http://java.sun.com/.


This program includes Swing components to control screen layout with multiple panels. It reads the Music Collection database and places recording titles into a Swing JList component for viewing and selection by the user. The program also uses a JTextArea Swing component. When the user selects the “View Tracks” button, the textarea component shows additional information about the recording and the track list. A JTextField component displays status and error messages. Figure 4-3 shows the program after the user selects the recording “Graceland.”

Figure 4-3. Music Client Application After Selecting Recording “Graceland”


Appendix A contains the complete source code for the Swing client application, MusicApp.java. We'll just show you snippets of the code here. Here's the MusicApp constructor, which performs the setup work for the application.

public MusicApp() {
  // set up Swing components
  . . .
  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");
  . . .
}

After setting up the Swing components and creating the Music EJB, the constructor calls getRecordings(). This private method reads the Music Collection database with the Music EJB business method getMusicList() and returns a collection of recordings. The getRecordings() method also puts the recording titles into the JList Swing component for the user to select.

The Java Swing event handler mechanism invokes valuechanged() and actionPerformed() to process user input. It calls valuechanged() when a user changes a title selection in the JList component.

public void valueChanged(ListSelectionEvent e) {

  // Event handler for ListSelectionEvent
  // (user changes selection in musicTitle
  // JList component)

  if (!e.getValueIsAdjusting()) {
    . . .

    // 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());
    . . .
  }
}

This method fetches a RecordingVO object from the recording collection indicated by the selected title and displays the recording information in the JText-Area component.

The event handler invokes actionPerformed() when the user selects the “View Tracks” JButton.

public void actionPerformed(ActionEvent e) {

  // Event handler for ActionEvent
  // (user clicks "View Tracks" button)
  . . .

       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()));
        . . .
}

For a selected title, this method gets the recording tracks by calling getTracks(). The private getTracks() method invokes the Music EJB business method getTrackList(), which in turn reads the Music Collection database. Method getTracks() also displays recording track information in the JTextArea Swing component. Figure 4-4 shows the result after the user clicks the “View Tracks” button for recording selection “Graceland.”

Figure 4-4. Music Client Application After Selecting “View Tracks” for Recording “Graceland”


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.144.194