TweetWallFX – Part 5

In TweetWallFX – Part 2 we talked about the wall creation. In TweetWallFX – Part 4 we had a demonstration of the chart API provided in JavaFX. Now we’re going to put everything together so that when we create a wall, the statistics tab will be created and updated in real time. This is pretty straight forward to do because we already have every needed piece to make it work.

Oh my! It works

Remember our TweetWallFXController class that was creating our wall. We had the startTweetWall() method that was creating a tab containing the wall. Remember also I was writing that we just have to make a task that every x seconds should look for new tweets. So basically we’re going to write this task, and it will create the wall and the stats.

public class TweetRefresher extends TimerTask {
  private List<Tweet> pendingTweets;
  private TweetDAO tweetDAO;
  private WallController wallController;
  private StatisticsController statsController;

  public TweetRefresher(WallController wall, StatisticsController stats) {
    this.wallController = wall;
    this.statsController = stats;
    this.tweetDAO = new FooTweetDAO();
    this.pendingTweets = new ArrayList<Tweet>();

  public void run() {
    Platform.runLater(new Runnable() {

    public void run() {

      int nbTweetsToRetrieve = 10;
      pendingTweets.addAll(tweetDAO.getTweetForHashtag(hashtag, nbTweetsToRetrieve));

      if (pendingTweets != null && !pendingTweets.isEmpty()) {                  

This is pretty easy to understand. We will now update our startTweetWall() method.

@FXML private void startTweetWall(ActionEvent event) {
  Tab wallTab = null, statsTab = null;
  WallController wallController = null;
  StatisticsController statsController = null;

  try {
    FXMLLoader fxmlLoader = new FXMLLoader(FXMLLibrary.getFXMLUrl(FXMLLibrary.WALL_TAB_FXML));
    wallTab = (Tab) fxmlLoader.load();
    wallController = (WallController) fxmlLoader.getController();

    // Loading the stats
    statsTab = (Tab) fxmlLoader.load();
    statsController = (StatisticsController) fxmlLoader.getController();
  } catch (IOException ex) {
    Logger.getLogger(TweetWallFXController.class.getName()).log(Level.SEVERE, null, ex);

  if (wallTab != null && statsTab != null) {
    wallTab.setText(String.format("#%1$s", this.hashtagTextField.getText()));
    statsTab.setText(String.format("Stats #%1$s", this.hashtagTextField.getText()));

    // Also adding the stats tab

    // Create and schedule the task
    TweetRefresher task = new TweetRefresher();
    Timer timer = new Timer();
    timer.schedule(task, 0, 5000);

That’s it, the wall and the stats will be updated every 5 seconds. To summarize, some screenshots:


In this eerie of articles I hope I could give a quick overview of JavaFX. But you can do much more with it, like playing sounds and movies, display data into really rich tables and so on. And if you have some designer skills (definitively not like me) you can do pretty beautiful UI. And with JavaFX 2.2 there’s more to come! Like I said in previous articles, I really think that JFX is an amazing and promising technology and that every Java developer should have a look at it. I also would like to share really few links about JFX:

I will also continue to post some articles about JavaFX capabilities, and believe it or not, there’s a lot to talk about!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: