Macca Blog

My life experiences with code and design

JavaFX and RSS

JavaFX is known to be a rich media platform, for use in creating a rich interface for the end user, but when you think of rich media you tend to think of streaming video, animations, graphics etc… what you tend to quickly forget about is RSS (and Atom for that matter).

It has become such a standard detail of nearly every piece of the web these days, that it has been taken for granted, which is why I thought I would post a quick blog entry about the RSS support in JavaFX.

Firstly, RSS is treated as a first citizen of information for use in any RIA (Rich Internet Application, what ever that means these days!). You get a massive amount of control and help while building any interaction with RSS.

The javafx.data.feed.rss package contains all of the classes that you need, but it ultimately boils down to the RssTask class. This class has hooks for the parsing of the feed, the time interval of the reloading of the feed, exception handling, etc…

There are also classes that represent the different items of the feed too, such as the Item class, which represents each individual RSS item (containing the properties such as title, link, comments, description, etc…).

Here is a quick code snippet that gets the RSS feed and fires a function when it parses each RSS item:

var rssTask = RssTask {
interval: 30s
location: "http://feeds.feedburner.com/javaposse"
onStart: function() {
println("loading RSS feed...");
}
onItem: function(rssItem:Item) {
//process each item
}
onException:function(e:Exception){
println("There was an error: {e.getMessage()}");
}
onDone: function(){ println("done reading RSS"); }
};
rssTask.start();

This RssTask will fetch the RSS feed for the popular JavaPosse podcast (which I highly recommend by the way, easily the best podcast around).

So I decided to whip up a little desktop app that parses the feed and will just display a simple little list of items using the title, etc…

package rssfeedexample;

import javafx.scene.Scene;
import javafx.data.feed.rss.RssTask;
import javafx.data.feed.rss.Item;
import javafx.stage.Stage;
import java.lang.Exception;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.Hyperlink;
import javafx.stage.Alert;

/**
* @author Mark Macumber
*/

var theRssItems:RssRectangleItem[] = [];

var rssTask = RssTask {
interval: 30s
location: "http://feeds.feedburner.com/javaposse"
onStart: function() {
println("loading RSS feed...");
}
onItem: function(rssItem:Item) {
insert RssRectangleItem{title: rssItem.title; linkAddress: rssItem.link} into theRssItems;
}
onException:function(e:Exception){
println("There was an error: {e.getMessage()}");
}
onDone: function(){ println("done reading RSS"); scrollBar.disable = false; }
};
rssTask.start();

var scrollBar : ScrollBar = ScrollBar {
translateX: bind (435 - scrollBar.width)
translateY: 0
vertical: true
height: 415
blockIncrement: 415
unitIncrement: 30
min: 0
max: 415
focusTraversable: false
blocksMouse: true
disable: true
};

class RssRectangleItem extends CustomNode{
public var title:String;
public var linkAddress:String;
var cont:Rectangle;
var i = sizeof theRssItems;
override function create():Node {
cont = Rectangle {
width: 250
height: 60
stroke: Color.BLACK
fill: Color.WHITE
arcHeight: 12
arcWidth: 12
x: 55
y: bind (i * 65) - (scrollBar.value);
};

var img = ImageView {
x: bind cont.x + 5
y: bind cont.y + 15
image:
Image {
url:"{__DIR__}feedIcon.png"
}
};

var titleText = Text {
font : Font { size: 10 }
x: bind img.x + 45,
y: bind img.y
content: bind title
wrappingWidth: 200
};

var link = Hyperlink{
text: "[link...]";
layoutX: bind titleText.x;
layoutY: bind titleText.y + 20;
action: function():Void{
Alert.inform("Link Alert", "take me to the link...{linkAddress}");
}
};

Group {
content: [cont, img, titleText, link]
}
}
}

Stage {
title: "JavaFX RSS Example"
width: 450
height: 450
scene: Scene {
content: bind [ theRssItems,scrollBar ]
}
}

The only thing you will need to do to get this code to run, is to get the RSS image (posted below)

Here is a quick screen shot too:

And this is the RSS image that I have used:

It is also at this point that I would like to make sure I don’t forget about the Atom support in JavaFX, there is pretty much the same amount of support for easily getting Atom working quickly within JavaFX.

I will hopefully be uploading this example to the JFXtras website so that people can download the code, and run an example through JNLP, so watch this space!

If you have any issues running the code, please let me know.

Until next time,

Good Coding!

This entry was posted in JavaFX, RSS. Bookmark the permalink.

2 Responses to JavaFX and RSS

  1. Chandrasekar

    Hi,

    I have a web application using Struts with the front end being JSP.
    I am a newbie, and I wish to use JavaFX instead of the Jsp to create a better UI.

    Can you kindly mail me a sample Struts application that uses JavaFX as the front end.

    My mail id:
    s.its.chandru@gmail.com

    I tried to browse through the net but in vain.

    Thanks for your help,
    Chandrasekar V.

  2. Mark

    Hello Chandrasekar, I cannot email you a sample application, but using JavaFX in JSP is very simple.

    You only need to add in an Applet. Are you familiar with these?

    See here: http://javafx.com/docs/tutorials/deploy-applet/ for a nice tutorial on deploying a JavaFX application into you website.

    Good luck

    Mark

Leave a Comment

Your email address will not be published. Required fields are marked *


*