import java.awt.Cursor; import javax.swing.JOptionPane; /* * BeanTester.java * * Created on June 30, 2006, 1:49 PM */ /** * * @author richardallenbair */ public class YahooNewsDemo extends javax.swing.JFrame { /** Creates new form BeanTester */ public YahooNewsDemo() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // //GEN-BEGIN:initComponents private void initComponents() { javax.swing.JLabel jLabel1; javax.swing.JScrollPane jScrollPane1; yahooNews = new org.jdesktop.swingx.ws.yahoo.rss.YahooNews(); worker = new org.jdesktop.swingx.BackgroundWorker(); jScrollPane1 = new javax.swing.JScrollPane(); textArea = new javax.swing.JTextArea(); executeButton = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); feedNameBox = new javax.swing.JComboBox(); worker.addBackgroundListener(new org.jdesktop.swingx.event.BackgroundListener() { public void process(org.jdesktop.swingx.event.BackgroundEvent evt) { workerProcess(evt); } public void started(org.jdesktop.swingx.event.BackgroundEvent evt) { workerStarted(evt); } public void done(org.jdesktop.swingx.event.BackgroundEvent evt) { workerDone(evt); } public void doInBackground(org.jdesktop.swingx.event.BackgroundEvent evt) { workerDoInBackground(evt); } }); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); textArea.setColumns(20); textArea.setRows(5); jScrollPane1.setViewportView(textArea); executeButton.setText("Refresh"); executeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { executeButtonActionPerformed(evt); } }); jLabel1.setText("Search"); feedNameBox.setEditable(true); feedNameBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "movies", "music", "topstories", "us", "world" })); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(feedNameBox, 0, 492, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(executeButton))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(executeButton) .addComponent(feedNameBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE) .addContainerGap()) ); pack(); }// //GEN-END:initComponents private void workerStarted(org.jdesktop.swingx.event.BackgroundEvent evt) {//GEN-FIRST:event_workerStarted executeButton.setEnabled(false); setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); }//GEN-LAST:event_workerStarted private void workerProcess(org.jdesktop.swingx.event.BackgroundEvent evt) {//GEN-FIRST:event_workerProcess textArea.setText("" + evt.getData()[0]); }//GEN-LAST:event_workerProcess private void workerDone(org.jdesktop.swingx.event.BackgroundEvent evt) {//GEN-FIRST:event_workerDone executeButton.setEnabled(true); setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); }//GEN-LAST:event_workerDone private void workerDoInBackground(org.jdesktop.swingx.event.BackgroundEvent evt) {//GEN-FIRST:event_workerDoInBackground try { evt.getWorker().publish(yahooNews.readFeed()); } catch (Exception e) { JOptionPane.showInternalMessageDialog(this, e, "Failed to show feed", JOptionPane.ERROR_MESSAGE); } }//GEN-LAST:event_workerDoInBackground private void executeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_executeButtonActionPerformed yahooNews.setFeedName("" + feedNameBox.getSelectedItem()); worker.execute(); }//GEN-LAST:event_executeButtonActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new YahooNewsDemo().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton executeButton; private javax.swing.JComboBox feedNameBox; private javax.swing.JTextArea textArea; private org.jdesktop.swingx.BackgroundWorker worker; private org.jdesktop.swingx.ws.yahoo.rss.YahooNews yahooNews; // End of variables declaration//GEN-END:variables }