Thursday Aug 15, 2013

Receiving Tweets in the NetBeans IDE Notifications Window

NetBeans IDE 7.4 provides a Notifications window as a centralized mechanism for displaying all application-wide messages.

These messages are not related to specific projects or files, that's what the Action Items window is for. For example, if the IDE is taking a long time to do something, you'll get a message in the Notifications window inviting you to file an issue.

But how about using it as a Twitter Viewer as well? I'm on Twitter a lot and all I really care about are Tweets that relate to NetBeans IDE. So, below I have integrated Twitter4J and now, after the IDE's main window is showing, a Twitter search is done in the background every 10 seconds for tweets containing the text "netbeans", with the results automatically displayed in the Notifications window, because all calls to org.openide.awt.NotificationDisplayer cause, aside from a balloon message, an entry to be created in the Notifications window.

Click to enlarge the image. Handily, new Tweets in the list are marked in bold, until I select them, at which point they're no longer bold because I have read them. I have no need to actually write my tweets in the IDE, but I do find it handy to know when there's a Tweet out there that I might like to respond to. Then I click the link in the panel to the right of the list and the browser opens at the Tweet that I'd like to do something with.

This really helps my productivity significantly since I no longer need to go to the browser to search for "netbeans" on Twitter. Instead, every 10 seconds while I'm programming in the IDE, which is most of the day, I get new Tweets delivered directly into my IDE. And then I can decide what to do with them. Since all of them relate directly to NetBeans, this really enhances my workflow quite a lot.

The code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Set;
import org.openide.awt.HtmlBrowser.URLDisplayer;
import org.openide.awt.NotificationDisplayer;
import org.openide.awt.StatusDisplayer;
import org.openide.util.Exceptions;
import org.openide.util.ImageUtilities;
import org.openide.util.RequestProcessor;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

public class OnShowingFindTweets implements Runnable {

    private static RequestProcessor RP;
    private final RequestProcessor.Task REFRESHER;
    private static int REFRESH_DELAY;
    private final Set<Status> statusHistory;

    public OnShowingFindTweets() {
        RP = new RequestProcessor(OnShowingFindTweets.class);
        REFRESHER = RP.create(this);
        REFRESH_DELAY = 10000;
        statusHistory = new HashSet<Status>();
    public void run() {
        StatusDisplayer.getDefault().setStatusText("Looking for more Tweets...");

    public void refreshTweets() {
        ConfigurationBuilder configBuilder = new ConfigurationBuilder();
        TwitterFactory tf = new TwitterFactory(;
        Twitter twitter = tf.getInstance();
        Query query = new Query("netbeans");
        QueryResult result;
        try {
            result =;
            for (final Status status : result.getTweets()) {
                if (!statusHistory.contains(status)) {
                    final String url = "" + 
                            status.getUser().getScreenName() + "/status/" + status.getId();
                            "@" + status.getUser().getScreenName() + ": " + status.getText(),
                            ImageUtilities.loadImageIcon("org/demo/icon.png", false),
                            //action listener:
                            new ActionListener() {
                                public void actionPerformed(ActionEvent e) {
                                    try {
                                        new URL(url));
                                    } catch (MalformedURLException eee) {
        } catch (TwitterException ex) {


On Twitter4J, the following proved very helpful:


Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« August 2013 »