Skip to main content

Simple Yahoo! News Reader

Posted by rbair on June 30, 2006 at 3:52 PM PDT

My last few blogs have been on using web services in Swing. This time I've created a simple Yahoo! News RSS reader JavaBean you can use in your own apps. And yes, this time I went all the way and wrote a JNLP. But beware, it is only running on 1.6 for now. I didn't even know I was developing on 1.6 until I tested the deploy, and now I'm headed out on vacation.

Here's a screenshot, for those you can't or won't run the demo:

It is a very ugly demo. Again, vacation and all that. But, here's basically what it does. You enter the name of the feed you'd like to see in the combo box on the top. Or you can pick one of the predefined ones, its up to you. Then click the "Refresh" button. On a background thread (there ya go, vtech!) I get the feed results, parse them, and then back on the UI thread I load them into the JTextArea. The results are raw output from the ROME RSS reader which I use to do all the heavy lifting.


The point here isn't to show off some slick looking application -- its the work that went into creating it. Or the lack thereof. Read the source code yourself. You can also download the sources and rebuild the project (but good luck getting it to load up in NetBeans. Sorry no time to make it pretty, vacation calls).

For the impatient, here's the source code boiled down. First, I created a YahooNews bean (well, after creating the UI). I then created a BackgroundWorker bean called "worker". I then filled in the BackgroundWorker's event handlers, and wrote an action listener for the "Refresh" button. Here's all the magic:

    private void workerStarted(org.jdesktop.swingx.event.BackgroundEvent evt) {                               
        executeButton.setEnabled(false);
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    }                             

    private void workerProcess(org.jdesktop.swingx.event.BackgroundEvent evt) {                              
        textArea.setText("" + evt.getData()[0]);
    }                             

    private void workerDone(org.jdesktop.swingx.event.BackgroundEvent evt) {                           
        executeButton.setEnabled(true);
        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }                          

    private void workerDoInBackground(org.jdesktop.swingx.event.BackgroundEvent evt) {                                     
        try {
            evt.getWorker().publish(yahooNews.readFeed());
        } catch (Exception e) {
            JOptionPane.showInternalMessageDialog(this, e, "Failed to show feed", JOptionPane.ERROR_MESSAGE);
        }
    }                                    

    private void executeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        yahooNews.setFeedName("" + feedNameBox.getSelectedItem());
        worker.execute();
    }                                            

Related Topics >>